From b2035d7e61646b86f65dfb2473a8be029d6d0627 Mon Sep 17 00:00:00 2001 From: Will Date: Sat, 24 Aug 2024 15:49:37 -0600 Subject: [PATCH] fix: migrate all flags, use exported vars --- cmd/hauler/cli/cli.go | 11 ++++------- cmd/hauler/cli/login.go | 18 +++--------------- cmd/hauler/cli/store.go | 26 +++++++++++++------------- cmd/hauler/cli/store/sync.go | 2 +- internal/flags/add.go | 6 +++--- internal/flags/cli.go | 5 +++++ internal/flags/copy.go | 2 +- internal/flags/extract.go | 2 +- internal/flags/info.go | 2 +- internal/flags/load.go | 2 +- internal/flags/login.go | 16 ++++++++++++++++ internal/flags/save.go | 2 +- internal/flags/serve.go | 4 ++-- internal/flags/{root.go => store.go} | 6 +++--- internal/flags/sync.go | 2 +- 15 files changed, 56 insertions(+), 50 deletions(-) create mode 100644 internal/flags/cli.go create mode 100644 internal/flags/login.go rename internal/flags/{root.go => store.go} (85%) diff --git a/cmd/hauler/cli/cli.go b/cmd/hauler/cli/cli.go index 0ae01902..5cc4a77f 100644 --- a/cmd/hauler/cli/cli.go +++ b/cmd/hauler/cli/cli.go @@ -3,14 +3,11 @@ package cli import ( "github.com/spf13/cobra" + "github.com/rancherfederal/hauler/internal/flags" "github.com/rancherfederal/hauler/pkg/log" ) -type rootOpts struct { - logLevel string -} - -var ro = &rootOpts{} +var ro = &flags.CliRootOpts{} func New() *cobra.Command { cmd := &cobra.Command{ @@ -18,7 +15,7 @@ func New() *cobra.Command { Short: "Airgap Swiss Army Knife", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { l := log.FromContext(cmd.Context()) - l.SetLevel(ro.logLevel) + l.SetLevel(ro.LogLevel) l.Debugf("running cli command [%s]", cmd.CommandPath()) return nil }, @@ -28,7 +25,7 @@ func New() *cobra.Command { } pf := cmd.PersistentFlags() - pf.StringVarP(&ro.logLevel, "log-level", "l", "info", "") + pf.StringVarP(&ro.LogLevel, "log-level", "l", "info", "") // Add subcommands addLogin(cmd) diff --git a/cmd/hauler/cli/login.go b/cmd/hauler/cli/login.go index 7ca6f872..7c3862cc 100644 --- a/cmd/hauler/cli/login.go +++ b/cmd/hauler/cli/login.go @@ -10,24 +10,12 @@ import ( "github.com/spf13/cobra" "oras.land/oras-go/pkg/content" + "github.com/rancherfederal/hauler/internal/flags" "github.com/rancherfederal/hauler/pkg/cosign" ) -type Opts struct { - Username string - Password string - PasswordStdin bool -} - -func (o *Opts) AddArgs(cmd *cobra.Command) { - f := cmd.Flags() - f.StringVarP(&o.Username, "username", "u", "", "Username to use for authentication") - f.StringVarP(&o.Password, "password", "p", "", "Password to use for authentication") - f.BoolVar(&o.PasswordStdin, "password-stdin", false, "Password to use for authentication (from stdin)") -} - func addLogin(parent *cobra.Command) { - o := &Opts{} + o := &flags.LoginOpts{} cmd := &cobra.Command{ Use: "login", @@ -60,7 +48,7 @@ hauler login reg.example.com -u bob -p haulin`, parent.AddCommand(cmd) } -func login(ctx context.Context, o *Opts, registry string) error { +func login(ctx context.Context, o *flags.LoginOpts, registry string) error { ropts := content.RegistryOptions{ Username: o.Username, Password: o.Password, diff --git a/cmd/hauler/cli/store.go b/cmd/hauler/cli/store.go index 1e3bd5bb..41a37ce6 100644 --- a/cmd/hauler/cli/store.go +++ b/cmd/hauler/cli/store.go @@ -10,7 +10,7 @@ import ( "github.com/rancherfederal/hauler/internal/flags" ) -var rootStoreOpts = &flags.RootOpts{} +var rootStoreOpts = &flags.StoreRootOpts{} func addStore(parent *cobra.Command) { cmd := &cobra.Command{ @@ -40,7 +40,7 @@ func addStore(parent *cobra.Command) { } func addStoreExtract() *cobra.Command { - o := &flags.ExtractOpts{RootOpts: rootStoreOpts} + o := &flags.ExtractOpts{StoreRootOpts: rootStoreOpts} cmd := &cobra.Command{ Use: "extract", @@ -64,7 +64,7 @@ func addStoreExtract() *cobra.Command { } func addStoreSync() *cobra.Command { - o := &flags.SyncOpts{RootOpts: rootStoreOpts} + o := &flags.SyncOpts{StoreRootOpts: rootStoreOpts} cmd := &cobra.Command{ Use: "sync", @@ -86,7 +86,7 @@ func addStoreSync() *cobra.Command { } func addStoreLoad() *cobra.Command { - o := &flags.LoadOpts{RootOpts: rootStoreOpts} + o := &flags.LoadOpts{StoreRootOpts: rootStoreOpts} cmd := &cobra.Command{ Use: "load", @@ -127,7 +127,7 @@ func addStoreServe() *cobra.Command { // RegistryCmd serves the embedded registry func addStoreServeRegistry() *cobra.Command { - o := &flags.ServeRegistryOpts{RootOpts: rootStoreOpts} + o := &flags.ServeRegistryOpts{StoreRootOpts: rootStoreOpts} cmd := &cobra.Command{ Use: "registry", Short: "Serve the embedded registry", @@ -150,7 +150,7 @@ func addStoreServeRegistry() *cobra.Command { // FileServerCmd serves the file server func addStoreServeFiles() *cobra.Command { - o := &flags.ServeFilesOpts{RootOpts: rootStoreOpts} + o := &flags.ServeFilesOpts{StoreRootOpts: rootStoreOpts} cmd := &cobra.Command{ Use: "fileserver", Short: "Serve the file server", @@ -172,7 +172,7 @@ func addStoreServeFiles() *cobra.Command { } func addStoreSave() *cobra.Command { - o := &flags.SaveOpts{RootOpts: rootStoreOpts} + o := &flags.SaveOpts{StoreRootOpts: rootStoreOpts} cmd := &cobra.Command{ Use: "save", @@ -196,7 +196,7 @@ func addStoreSave() *cobra.Command { } func addStoreInfo() *cobra.Command { - o := &flags.InfoOpts{RootOpts: rootStoreOpts} + o := &flags.InfoOpts{StoreRootOpts: rootStoreOpts} var allowedValues = []string{"image", "chart", "file", "sigs", "atts", "sbom", "all"} @@ -227,7 +227,7 @@ func addStoreInfo() *cobra.Command { } func addStoreCopy() *cobra.Command { - o := &flags.CopyOpts{RootOpts: rootStoreOpts} + o := &flags.CopyOpts{StoreRootOpts: rootStoreOpts} cmd := &cobra.Command{ Use: "copy", @@ -268,7 +268,7 @@ func addStoreAdd() *cobra.Command { } func addStoreAddFile() *cobra.Command { - o := &flags.AddFileOpts{RootOpts: rootStoreOpts} + o := &flags.AddFileOpts{StoreRootOpts: rootStoreOpts} cmd := &cobra.Command{ Use: "file", @@ -291,7 +291,7 @@ func addStoreAddFile() *cobra.Command { } func addStoreAddImage() *cobra.Command { - o := &flags.AddImageOpts{RootOpts: rootStoreOpts} + o := &flags.AddImageOpts{StoreRootOpts: rootStoreOpts} cmd := &cobra.Command{ Use: "image", @@ -315,8 +315,8 @@ func addStoreAddImage() *cobra.Command { func addStoreAddChart() *cobra.Command { o := &flags.AddChartOpts{ - RootOpts: rootStoreOpts, - ChartOpts: &action.ChartPathOptions{}, + StoreRootOpts: rootStoreOpts, + ChartOpts: &action.ChartPathOptions{}, } cmd := &cobra.Command{ diff --git a/cmd/hauler/cli/store/sync.go b/cmd/hauler/cli/store/sync.go index 6632749c..2763e6c6 100644 --- a/cmd/hauler/cli/store/sync.go +++ b/cmd/hauler/cli/store/sync.go @@ -49,7 +49,7 @@ func SyncCmd(ctx context.Context, o *flags.SyncOpts, s *store.Layout) error { if err != nil { return err } - err = ExtractCmd(ctx, &flags.ExtractOpts{RootOpts: o.RootOpts}, s, fmt.Sprintf("hauler/%s-manifest.yaml:%s", parts[0], tag)) + err = ExtractCmd(ctx, &flags.ExtractOpts{StoreRootOpts: o.StoreRootOpts}, s, fmt.Sprintf("hauler/%s-manifest.yaml:%s", parts[0], tag)) if err != nil { return err } diff --git a/internal/flags/add.go b/internal/flags/add.go index 0069d00d..f4fb138c 100644 --- a/internal/flags/add.go +++ b/internal/flags/add.go @@ -6,7 +6,7 @@ import ( ) type AddImageOpts struct { - *RootOpts + *StoreRootOpts Name string Key string Platform string @@ -19,7 +19,7 @@ func (o *AddImageOpts) AddFlags(cmd *cobra.Command) { } type AddFileOpts struct { - *RootOpts + *StoreRootOpts Name string } @@ -29,7 +29,7 @@ func (o *AddFileOpts) AddFlags(cmd *cobra.Command) { } type AddChartOpts struct { - *RootOpts + *StoreRootOpts ChartOpts *action.ChartPathOptions } diff --git a/internal/flags/cli.go b/internal/flags/cli.go new file mode 100644 index 00000000..54cb01f1 --- /dev/null +++ b/internal/flags/cli.go @@ -0,0 +1,5 @@ +package flags + +type CliRootOpts struct { + LogLevel string +} diff --git a/internal/flags/copy.go b/internal/flags/copy.go index 5b16494a..b6848cea 100644 --- a/internal/flags/copy.go +++ b/internal/flags/copy.go @@ -3,7 +3,7 @@ package flags import "github.com/spf13/cobra" type CopyOpts struct { - *RootOpts + *StoreRootOpts Username string Password string diff --git a/internal/flags/extract.go b/internal/flags/extract.go index 8688c188..c502d0f9 100644 --- a/internal/flags/extract.go +++ b/internal/flags/extract.go @@ -3,7 +3,7 @@ package flags import "github.com/spf13/cobra" type ExtractOpts struct { - *RootOpts + *StoreRootOpts DestinationDir string } diff --git a/internal/flags/info.go b/internal/flags/info.go index 6f711a64..68641d51 100644 --- a/internal/flags/info.go +++ b/internal/flags/info.go @@ -3,7 +3,7 @@ package flags import "github.com/spf13/cobra" type InfoOpts struct { - *RootOpts + *StoreRootOpts OutputFormat string TypeFilter string diff --git a/internal/flags/load.go b/internal/flags/load.go index a8584d7a..7c13c35a 100644 --- a/internal/flags/load.go +++ b/internal/flags/load.go @@ -3,7 +3,7 @@ package flags import "github.com/spf13/cobra" type LoadOpts struct { - *RootOpts + *StoreRootOpts TempOverride string } diff --git a/internal/flags/login.go b/internal/flags/login.go new file mode 100644 index 00000000..82c8f844 --- /dev/null +++ b/internal/flags/login.go @@ -0,0 +1,16 @@ +package flags + +import "github.com/spf13/cobra" + +type LoginOpts struct { + Username string + Password string + PasswordStdin bool +} + +func (o *LoginOpts) AddArgs(cmd *cobra.Command) { + f := cmd.Flags() + f.StringVarP(&o.Username, "username", "u", "", "Username to use for authentication") + f.StringVarP(&o.Password, "password", "p", "", "Password to use for authentication") + f.BoolVar(&o.PasswordStdin, "password-stdin", false, "Password to use for authentication (from stdin)") +} diff --git a/internal/flags/save.go b/internal/flags/save.go index 650d03dd..55edd988 100644 --- a/internal/flags/save.go +++ b/internal/flags/save.go @@ -3,7 +3,7 @@ package flags import "github.com/spf13/cobra" type SaveOpts struct { - *RootOpts + *StoreRootOpts FileName string } diff --git a/internal/flags/serve.go b/internal/flags/serve.go index 5a403b0e..fcbce8a9 100644 --- a/internal/flags/serve.go +++ b/internal/flags/serve.go @@ -9,7 +9,7 @@ import ( ) type ServeRegistryOpts struct { - *RootOpts + *StoreRootOpts Port int RootDir string @@ -63,7 +63,7 @@ func (o *ServeRegistryOpts) DefaultRegistryConfig() *configuration.Configuration } type ServeFilesOpts struct { - *RootOpts + *StoreRootOpts Port int Timeout int diff --git a/internal/flags/root.go b/internal/flags/store.go similarity index 85% rename from internal/flags/root.go rename to internal/flags/store.go index ed874a9b..fdc3aa5b 100644 --- a/internal/flags/root.go +++ b/internal/flags/store.go @@ -12,18 +12,18 @@ import ( "github.com/spf13/cobra" ) -type RootOpts struct { +type StoreRootOpts struct { StoreDir string CacheDir string } -func (o *RootOpts) AddArgs(cmd *cobra.Command) { +func (o *StoreRootOpts) AddArgs(cmd *cobra.Command) { pf := cmd.PersistentFlags() pf.StringVarP(&o.StoreDir, "store", "s", consts.DefaultStoreName, "Location to create store at") pf.StringVar(&o.CacheDir, "cache", "", "(deprecated flag and currently not used)") } -func (o *RootOpts) Store(ctx context.Context) (*store.Layout, error) { +func (o *StoreRootOpts) Store(ctx context.Context) (*store.Layout, error) { l := log.FromContext(ctx) dir := o.StoreDir diff --git a/internal/flags/sync.go b/internal/flags/sync.go index a94c4057..30bae395 100644 --- a/internal/flags/sync.go +++ b/internal/flags/sync.go @@ -3,7 +3,7 @@ package flags import "github.com/spf13/cobra" type SyncOpts struct { - *RootOpts + *StoreRootOpts ContentFiles []string Key string Products []string