Skip to content

Commit

Permalink
remove duplicated flags package
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes94 committed Jun 9, 2023
1 parent 532c373 commit 2822cdf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 79 deletions.
29 changes: 14 additions & 15 deletions internal/dinosaur/pkg/cmd/centrals/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
},
}

Expand All @@ -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,
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions internal/dinosaur/pkg/cmd/centrals/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/dinosaur/pkg/cmd/centrals/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
51 changes: 0 additions & 51 deletions internal/dinosaur/pkg/cmd/cliflags/cliflags.go

This file was deleted.

21 changes: 14 additions & 7 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 2822cdf

Please sign in to comment.