diff --git a/internal/dinosaur/pkg/cmd/centrals/create.go b/internal/dinosaur/pkg/cmd/centrals/create.go index fa38ab2589..77b3bc362f 100644 --- a/internal/dinosaur/pkg/cmd/centrals/create.go +++ b/internal/dinosaur/pkg/cmd/centrals/create.go @@ -4,12 +4,11 @@ import ( "encoding/json" "fmt" - "github.com/golang/glog" "github.com/spf13/cobra" "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/api/public" - "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/cmd/cliflags" "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/cmd/fleetmanagerclient" "github.com/stackrox/acs-fleet-manager/pkg/client/fleetmanager" + "github.com/stackrox/acs-fleet-manager/pkg/flags" ) // NewCreateCommand creates a new command for creating centrals. @@ -18,8 +17,8 @@ func NewCreateCommand() *cobra.Command { Use: "create", Short: "Create a new central request", Long: "Create a new central request.", - Run: func(cmd *cobra.Command, args []string) { - runCreate(fleetmanagerclient.AuthenticatedClientWithOCM(), cmd, args) + RunE: func(cmd *cobra.Command, args []string) error { + return runCreate(fleetmanagerclient.AuthenticatedClientWithOCM(), cmd, args) }, } @@ -30,16 +29,16 @@ func NewCreateCommand() *cobra.Command { cmd.Flags().String(FlagClusterID, "000", "Central request cluster ID") cmd.Flags().Bool(FlagMultiAZ, true, "Whether Central request should be Multi AZ or not") cmd.Flags().String(FlagOrgID, "", "OCM org id") - cliflags.MarkFlagRequired(FlagName, cmd) - cliflags.MarkFlagRequired(FlagRegion, cmd) - cliflags.MarkFlagRequired(FlagProvider, cmd) + flags.MarkFlagRequired(FlagName, cmd) + flags.MarkFlagRequired(FlagRegion, cmd) + flags.MarkFlagRequired(FlagProvider, cmd) return cmd } -func runCreate(client *fleetmanager.Client, cmd *cobra.Command, _ []string) { - name := cliflags.MustGetDefinedString(FlagName, cmd) - region := cliflags.MustGetDefinedString(FlagRegion, cmd) - provider := cliflags.MustGetDefinedString(FlagProvider, cmd) +func runCreate(client *fleetmanager.Client, cmd *cobra.Command, _ []string) error { + name := flags.MustGetDefinedString(FlagName, cmd.Flags()) + region := flags.MustGetDefinedString(FlagRegion, cmd.Flags()) + provider := flags.MustGetDefinedString(FlagProvider, cmd.Flags()) request := public.CentralRequestPayload{ Region: region, @@ -51,14 +50,14 @@ func runCreate(client *fleetmanager.Client, cmd *cobra.Command, _ []string) { const async = true centralRequest, _, err := client.PublicAPI().CreateCentral(cmd.Context(), async, request) if err != nil { - glog.Errorf(apiErrorMsg, "create", err) - return + return fmt.Errorf(apiErrorMsg, "create", err) } centralJSON, err := json.Marshal(centralRequest) if err != nil { - glog.Errorf("Failed to marshal Central: %s", err) - return + return fmt.Errorf("Failed to marshal Central: %s", err) } fmt.Println(string(centralJSON)) + + return nil } diff --git a/internal/dinosaur/pkg/cmd/centrals/delete.go b/internal/dinosaur/pkg/cmd/centrals/delete.go index c1b3da4e94..149feeee42 100644 --- a/internal/dinosaur/pkg/cmd/centrals/delete.go +++ b/internal/dinosaur/pkg/cmd/centrals/delete.go @@ -5,9 +5,9 @@ import ( "github.com/golang/glog" "github.com/spf13/cobra" - "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/cmd/cliflags" "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/cmd/fleetmanagerclient" "github.com/stackrox/acs-fleet-manager/pkg/client/fleetmanager" + "github.com/stackrox/acs-fleet-manager/pkg/flags" ) // NewDeleteCommand command for deleting centrals. @@ -22,12 +22,12 @@ func NewDeleteCommand() *cobra.Command { } cmd.Flags().String(FlagID, "", "Central ID (required)") - cliflags.MarkFlagRequired(FlagID, cmd) + flags.MarkFlagRequired(FlagID, cmd) return cmd } func runDelete(client *fleetmanager.Client, cmd *cobra.Command, _ []string) { - id := cliflags.MustGetDefinedString(FlagID, cmd) + id := flags.MustGetDefinedString(FlagID, cmd.Flags()) const async = true resp, err := client.PublicAPI().DeleteCentralById(cmd.Context(), id, async) diff --git a/internal/dinosaur/pkg/cmd/centrals/get.go b/internal/dinosaur/pkg/cmd/centrals/get.go index 408208c501..a5c790417c 100644 --- a/internal/dinosaur/pkg/cmd/centrals/get.go +++ b/internal/dinosaur/pkg/cmd/centrals/get.go @@ -6,9 +6,9 @@ import ( "github.com/golang/glog" "github.com/spf13/cobra" - "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/cmd/cliflags" "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/cmd/fleetmanagerclient" "github.com/stackrox/acs-fleet-manager/pkg/client/fleetmanager" + "github.com/stackrox/acs-fleet-manager/pkg/flags" ) // NewGetCommand gets a new command for getting centrals. @@ -22,13 +22,13 @@ func NewGetCommand() *cobra.Command { }, } cmd.Flags().String(FlagID, "", "Central ID (required)") - cliflags.MarkFlagRequired(FlagID, cmd) + flags.MarkFlagRequired(FlagID, cmd) return cmd } func runGet(client *fleetmanager.Client, cmd *cobra.Command, _ []string) { - id := cliflags.MustGetDefinedString(FlagID, cmd) + id := flags.MustGetDefinedString(FlagID, cmd.Flags()) centralRequest, _, err := client.PublicAPI().GetCentralById(cmd.Context(), id) if err != nil { diff --git a/internal/dinosaur/pkg/cmd/cliflags/cliflags.go b/internal/dinosaur/pkg/cmd/cliflags/cliflags.go deleted file mode 100644 index 1a4e32970a..0000000000 --- a/internal/dinosaur/pkg/cmd/cliflags/cliflags.go +++ /dev/null @@ -1,51 +0,0 @@ -// Package cliflags defines util methods for flags used in acsfleetctl -package cliflags - -import ( - "fmt" - - "github.com/golang/glog" - "github.com/spf13/cobra" -) - -// MarkFlagRequired marks the given flag as required, panics if command has no flag with flagName -func MarkFlagRequired(flagName string, cmd *cobra.Command) { - if err := cmd.MarkFlagRequired(flagName); err != nil { - panic(err) - } -} - -// MustGetDefinedString attempts to get a non-empty string flag from the provided command or panic -func MustGetDefinedString(flagName string, cmd *cobra.Command) string { - flagVal := MustGetString(flagName, cmd) - if flagVal == "" { - panic(undefinedValueMessage(flagName)) - } - return flagVal -} - -// MustGetString attempts to get a string flag from the provided command or panic -func MustGetString(flagName string, cmd *cobra.Command) string { - flagVal, err := cmd.Flags().GetString(flagName) - if err != nil { - panic(notFoundMessage(flagName, err)) - } - return flagVal -} - -// MustGetBool attempts to get a boolean flag from the provided command or panic -func MustGetBool(flagName string, cmd *cobra.Command) bool { - flagVal, err := cmd.Flags().GetBool(flagName) - if err != nil { - glog.Fatalf(notFoundMessage(flagName, err)) - } - return flagVal -} - -func undefinedValueMessage(flagName string) string { - return fmt.Sprintf("flag %s has undefined value", flagName) -} - -func notFoundMessage(flagName string, err error) string { - return fmt.Sprintf("could not get flag %s from flag set: %s", flagName, err.Error()) -} diff --git a/pkg/flags/flags.go b/pkg/flags/flags.go index 7cdc7269ad..1356619280 100644 --- a/pkg/flags/flags.go +++ b/pkg/flags/flags.go @@ -4,37 +4,44 @@ package flags import ( "fmt" - "github.com/golang/glog" + "github.com/spf13/cobra" "github.com/spf13/pflag" ) -// MustGetDefinedString attempts to get a non-empty string flag from the provided flag set or os.Exits +// MustGetDefinedString attempts to get a non-empty string flag from the provided flag set or panic func MustGetDefinedString(flagName string, flags *pflag.FlagSet) string { flagVal := MustGetString(flagName, flags) if flagVal == "" { - glog.Fatal(undefinedValueMessage(flagName)) + panic(undefinedValueMessage(flagName)) } return flagVal } -// MustGetString attempts to get a string flag from the provided flag set or os.Exits +// MustGetString attempts to get a string flag from the provided flag set or panic func MustGetString(flagName string, flags *pflag.FlagSet) string { flagVal, err := flags.GetString(flagName) if err != nil { - glog.Fatalf(notFoundMessage(flagName, err)) + panic(notFoundMessage(flagName, err)) } return flagVal } -// MustGetBool attempts to get a boolean flag from the provided flag set or os.Exits +// MustGetBool attempts to get a boolean flag from the provided flag set or panic func MustGetBool(flagName string, flags *pflag.FlagSet) bool { flagVal, err := flags.GetBool(flagName) if err != nil { - glog.Fatalf(notFoundMessage(flagName, err)) + panic(notFoundMessage(flagName, err)) } return flagVal } +// MarkFlagRequired marks the given flag as required, panics if command has no flag with flagName +func MarkFlagRequired(flagName string, cmd *cobra.Command) { + if err := cmd.MarkFlagRequired(flagName); err != nil { + panic(err) + } +} + func undefinedValueMessage(flagName string) string { return fmt.Sprintf("flag %s has undefined value", flagName) }