mirror of
https://github.com/hauler-dev/hauler.git
synced 2026-02-14 18:09:51 +00:00
64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package flags
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/cobra"
|
|
"hauler.dev/go/hauler/pkg/consts"
|
|
"hauler.dev/go/hauler/pkg/log"
|
|
"hauler.dev/go/hauler/pkg/store"
|
|
)
|
|
|
|
type StoreRootOpts struct {
|
|
StoreDir string
|
|
Retries int
|
|
TempOverride string
|
|
}
|
|
|
|
func (o *StoreRootOpts) AddFlags(cmd *cobra.Command) {
|
|
pf := cmd.PersistentFlags()
|
|
pf.StringVarP(&o.StoreDir, "store", "s", "", "Set the directory to use for the content store")
|
|
pf.IntVarP(&o.Retries, "retries", "r", consts.DefaultRetries, "Set the number of retries for operations")
|
|
pf.StringVarP(&o.TempOverride, "tempdir", "t", "", "(Optional) Override the default temporary directory determined by the OS")
|
|
}
|
|
|
|
func (o *StoreRootOpts) Store(ctx context.Context) (*store.Layout, error) {
|
|
l := log.FromContext(ctx)
|
|
|
|
storeDir := o.StoreDir
|
|
|
|
if storeDir == "" {
|
|
storeDir = os.Getenv(consts.HaulerStoreDir)
|
|
}
|
|
|
|
if storeDir == "" {
|
|
storeDir = consts.DefaultStoreName
|
|
}
|
|
|
|
abs, err := filepath.Abs(storeDir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
o.StoreDir = abs
|
|
|
|
l.Debugf("using store at [%s]", abs)
|
|
|
|
if _, err := os.Stat(abs); errors.Is(err, os.ErrNotExist) {
|
|
if err := os.MkdirAll(abs, os.ModePerm); err != nil {
|
|
return nil, err
|
|
}
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s, err := store.NewLayout(abs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s, nil
|
|
}
|