diff --git a/otdfctl/cmd/auth/login.go b/otdfctl/cmd/auth/login.go index c39bbf05f7..d1618ebba8 100644 --- a/otdfctl/cmd/auth/login.go +++ b/otdfctl/cmd/auth/login.go @@ -14,13 +14,13 @@ import ( func codeLogin(cmd *cobra.Command, args []string) { c := cli.New(cmd, args) cp := common.InitProfile(c) - clientID := c.FlagHelper.GetRequiredString("client-id") - port := c.FlagHelper.GetOptionalString("port") + clientID := c.Flags.GetRequiredString("client-id") + port := c.Flags.GetOptionalString("port") tok, err := auth.LoginWithPKCE( cmd.Context(), cp.GetEndpoint(), clientID, - c.FlagHelper.GetOptionalBool("tls-no-verify"), + c.Flags.GetOptionalBool("tls-no-verify"), port, ) if err != nil { diff --git a/otdfctl/cmd/auth/logout.go b/otdfctl/cmd/auth/logout.go index dda2d0fc7a..1f20665716 100644 --- a/otdfctl/cmd/auth/logout.go +++ b/otdfctl/cmd/auth/logout.go @@ -23,7 +23,7 @@ func logout(cmd *cobra.Command, args []string) { cp.GetEndpoint(), creds.AccessToken.ClientID, creds.AccessToken.RefreshToken, - c.FlagHelper.GetOptionalBool("tls-no-verify"), + c.Flags.GetOptionalBool("tls-no-verify"), ); err != nil { c.ExitWithError("An error occurred while revoking the access token", err) } diff --git a/otdfctl/cmd/common/common.go b/otdfctl/cmd/common/common.go index f3532c1387..e7515a5a20 100644 --- a/otdfctl/cmd/common/common.go +++ b/otdfctl/cmd/common/common.go @@ -39,7 +39,7 @@ func applyOutputFormatPreference(c *cli.Cli, store *profiles.OtdfctlProfileStore // returns the profile and the current profile store func InitProfile(c *cli.Cli) *profiles.OtdfctlProfileStore { var err error - profileName := c.FlagHelper.GetOptionalString("profile") + profileName := c.Flags.GetOptionalString("profile") hasKeyringStore, err := osprofiles.HasGlobalStore(config.AppName, osprofiles.WithKeyringStore()) if err != nil { @@ -91,11 +91,11 @@ func NewHandler(c *cli.Cli) handlers.Handler { var cp *profiles.OtdfctlProfileStore // Non-profile flags - host := c.FlagHelper.GetOptionalString("host") - tlsNoVerify := c.FlagHelper.GetOptionalBool("tls-no-verify") - withClientCreds := c.FlagHelper.GetOptionalString("with-client-creds") - withClientCredsFile := c.FlagHelper.GetOptionalString("with-client-creds-file") - withAccessToken := c.FlagHelper.GetOptionalString("with-access-token") + host := c.Flags.GetOptionalString("host") + tlsNoVerify := c.Flags.GetOptionalBool("tls-no-verify") + withClientCreds := c.Flags.GetOptionalString("with-client-creds") + withClientCredsFile := c.Flags.GetOptionalString("with-client-creds-file") + withAccessToken := c.Flags.GetOptionalString("with-access-token") var inMemoryProfile bool authFlags := []string{"--with-access-token", "--with-client-creds", "--with-client-creds-file"} diff --git a/otdfctl/cmd/policy/attributeValues.go b/otdfctl/cmd/policy/attributeValues.go index b4b626476f..e1960394a7 100644 --- a/otdfctl/cmd/policy/attributeValues.go +++ b/otdfctl/cmd/policy/attributeValues.go @@ -22,9 +22,9 @@ func createAttributeValue(cmd *cobra.Command, args []string) { defer h.Close() ctx := cmd.Context() - attrID := c.FlagHelper.GetRequiredID("attribute-id") - value := c.FlagHelper.GetRequiredString("value") - metadataLabels = c.FlagHelper.GetStringSlice("label", metadataLabels, cli.FlagsStringSliceOptions{Min: 0}) + attrID := c.Flags.GetRequiredID("attribute-id") + value := c.Flags.GetRequiredString("value") + metadataLabels = c.Flags.GetStringSlice("label", metadataLabels, cli.FlagsStringSliceOptions{Min: 0}) attr, err := h.GetAttribute(ctx, attrID) if err != nil { @@ -44,7 +44,7 @@ func getAttributeValue(cmd *cobra.Command, args []string) { h := common.NewHandler(c) defer h.Close() - id := c.FlagHelper.GetRequiredID("id") + id := c.Flags.GetRequiredID("id") v, err := h.GetAttributeValue(cmd.Context(), id) if err != nil { @@ -105,7 +105,7 @@ func listAttributeValue(cmd *cobra.Command, args []string) { h := common.NewHandler(c) defer h.Close() - attrID := c.FlagHelper.GetRequiredID("attribute-id") + attrID := c.Flags.GetRequiredID("attribute-id") state := cli.GetState(cmd) limit := c.Flags.GetRequiredInt32("limit") offset := c.Flags.GetRequiredInt32("offset") diff --git a/otdfctl/cmd/policy/kasRegistry.go b/otdfctl/cmd/policy/kasRegistry.go index 4b5884cfa5..c3595d9380 100644 --- a/otdfctl/cmd/policy/kasRegistry.go +++ b/otdfctl/cmd/policy/kasRegistry.go @@ -19,7 +19,7 @@ func getKeyAccessRegistry(cmd *cobra.Command, args []string) { h := common.NewHandler(c) defer h.Close() - id := c.FlagHelper.GetRequiredID("id") + id := c.Flags.GetRequiredID("id") kas, err := h.GetKasRegistryEntry(cmd.Context(), handlers.KasIdentifier{ ID: id, diff --git a/otdfctl/cmd/profile.go b/otdfctl/cmd/profile.go index 883bfabcc4..e5e88e9251 100644 --- a/otdfctl/cmd/profile.go +++ b/otdfctl/cmd/profile.go @@ -59,7 +59,7 @@ func newProfilerFromCLI(c *cli.Cli) *osprofiles.Profiler { func getDriverTypeFromUser(c *cli.Cli) profiles.ProfileDriver { driverTypeStr := string(profiles.ProfileDriverDefault) - store := c.FlagHelper.GetOptionalString("store") + store := c.Flags.GetOptionalString("store") if len(store) > 0 { driverTypeStr = store } @@ -90,9 +90,9 @@ var profileCreateCmd = &cobra.Command{ profileName := args[0] endpoint := args[1] - setDefault := c.FlagHelper.GetOptionalBool("set-default") - tlsNoVerify := c.FlagHelper.GetOptionalBool("tls-no-verify") - outputFormat := c.FlagHelper.GetOptionalString("output-format") + setDefault := c.Flags.GetOptionalBool("set-default") + tlsNoVerify := c.Flags.GetOptionalBool("tls-no-verify") + outputFormat := c.Flags.GetOptionalString("output-format") if !profiles.IsValidOutputFormat(outputFormat) { c.ExitWithError("Output format must be either 'styled' or 'json'", nil) } diff --git a/otdfctl/pkg/cli/cli.go b/otdfctl/pkg/cli/cli.go index e6ffcdb3a9..fefe79a71e 100644 --- a/otdfctl/pkg/cli/cli.go +++ b/otdfctl/pkg/cli/cli.go @@ -11,9 +11,8 @@ type Cli struct { args []string // Helpers - Flags *flagHelper - FlagHelper *flagHelper - printer *Printer + Flags *Flags + printer *Printer } // New creates a new Cli object @@ -34,10 +33,7 @@ func New(cmd *cobra.Command, args []string, options ...cliVariadicOption) *Cli { ExitWithError("cli expects a command", ErrPrinterExpectsCommand) } - cli.Flags = newFlagHelper(cmd) - // Temp wrapper for FlagHelper until we can remove it - cli.FlagHelper = cli.Flags - + cli.Flags = newFlags(cmd) cli.printer = newPrinter(opts.printerJSON || cli.Flags.GetOptionalBool("json")) return cli diff --git a/otdfctl/pkg/cli/flagValues.go b/otdfctl/pkg/cli/flagValues.go index 5f5c5f76a9..9b3e2a097b 100644 --- a/otdfctl/pkg/cli/flagValues.go +++ b/otdfctl/pkg/cli/flagValues.go @@ -15,23 +15,27 @@ type FlagsStringSliceOptions struct { Max int } -type flagHelper struct { +type Flags struct { cmd *cobra.Command } -func newFlagHelper(cmd *cobra.Command) *flagHelper { - return &flagHelper{cmd: cmd} +func newFlags(cmd *cobra.Command) *Flags { + return &Flags{cmd: cmd} } -func (f flagHelper) GetRequiredString(flag string) string { - v := f.cmd.Flag(flag).Value.String() +func (f Flags) GetRequiredString(flag string) string { + p := f.cmd.Flag(flag) + if p == nil { + ExitWithError("Flag '--"+flag+"' is not registered", nil) + } + v := p.Value.String() if v == "" { ExitWithError("Flag '--"+flag+"' is required", nil) } return v } -func (f flagHelper) GetRequiredID(idFlag string) string { +func (f Flags) GetRequiredID(idFlag string) string { v := f.GetRequiredString(idFlag) id, err := uuid.Parse(v) if err != nil { @@ -40,7 +44,7 @@ func (f flagHelper) GetRequiredID(idFlag string) string { return id.String() } -func (f flagHelper) GetOptionalID(idFlag string) string { +func (f Flags) GetOptionalID(idFlag string) string { p := f.GetOptionalString(idFlag) if p == "" { return "" @@ -52,7 +56,7 @@ func (f flagHelper) GetOptionalID(idFlag string) string { return id.String() } -func (f flagHelper) GetOptionalString(flag string) string { +func (f Flags) GetOptionalString(flag string) string { p := f.cmd.Flag(flag) if p == nil { return "" @@ -60,7 +64,7 @@ func (f flagHelper) GetOptionalString(flag string) string { return p.Value.String() } -func (f flagHelper) GetStringSlice(flag string, v []string, opts FlagsStringSliceOptions) []string { +func (f Flags) GetStringSlice(flag string, v []string, opts FlagsStringSliceOptions) []string { if len(v) < opts.Min { ExitWithError(fmt.Sprintf("Flag '--%s' must have at least %d non-empty values", flag, opts.Min), nil) } @@ -70,7 +74,7 @@ func (f flagHelper) GetStringSlice(flag string, v []string, opts FlagsStringSlic return v } -func (f flagHelper) GetRequiredInt32(flag string) int32 { +func (f Flags) GetRequiredInt32(flag string) int32 { v, e := f.cmd.Flags().GetInt32(flag) if e != nil { ExitWithError("Flag '--"+flag+"' is required", nil) @@ -82,18 +86,18 @@ func (f flagHelper) GetRequiredInt32(flag string) int32 { return v } -func (f flagHelper) GetOptionalInt32(flag string) int32 { +func (f Flags) GetOptionalInt32(flag string) int32 { v, _ := f.cmd.Flags().GetInt32(flag) return v } -func (f flagHelper) GetOptionalBool(flag string) bool { +func (f Flags) GetOptionalBool(flag string) bool { v, _ := f.cmd.Flags().GetBool(flag) return v } // Returns nil when the flag is not explicitly set. -func (f flagHelper) GetOptionalBoolWrapper(flag string) *wrapperspb.BoolValue { +func (f Flags) GetOptionalBoolWrapper(flag string) *wrapperspb.BoolValue { if !f.cmd.Flags().Changed(flag) { return nil } @@ -101,7 +105,7 @@ func (f flagHelper) GetOptionalBoolWrapper(flag string) *wrapperspb.BoolValue { return wrapperspb.Bool(v) } -func (f flagHelper) GetRequiredBool(flag string) bool { +func (f Flags) GetRequiredBool(flag string) bool { v, e := f.cmd.Flags().GetBool(flag) if e != nil { ExitWithError("Flag '--"+flag+"' is required", nil) @@ -124,18 +128,6 @@ func GetState(cmd *cobra.Command) common.ActiveStateEnum { return state } -// func (f flagHelper) GetStructSlice(flag string, v []StructFlag[T], opts flagHelperStringSliceOptions) ([]StructFlag[T], err) { -// if len(v) < opts.Min { -// fmt.Println(ErrorMessage(fmt.Sprintf("Flag %s must have at least %d non-empty values", flag, opts.Min), nil)) -// os.Exit(1) -// } -// if opts.Max > 0 && len(v) > opts.Max { -// fmt.Println(ErrorMessage(fmt.Sprintf("Flag %s must have at most %d non-empty values", flag, opts.Max), nil)) -// os.Exit(1) -// } -// return v -// } - // type StructFlag[T any] struct { // Val T // }