Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce acsfleetctl binary #1074

Merged
merged 11 commits into from
Jun 13, 2023
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/fleet-manager
cmd/fleet-manager/fleet-manager
/fleetshard-sync

/acsfleetctl
# Ignore generated templates
/templates/*-template.json

Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,11 @@ probe:
GOOS="$(GOOS)" GOARCH="$(GOARCH)" $(GO) build $(GOARGS) -o probe/bin/probe ./probe/cmd/probe
.PHONY: probe

binary: fleet-manager fleetshard-sync probe
acsfleetctl:
GOOS="$(GOOS)" GOARCH="$(GOARCH)" $(GO) build $(GOARGS) -o acsfleetctl ./cmd/acsfleetctl
.PHONY: acsfleetctl

binary: fleet-manager fleetshard-sync probe acsfleetctl
.PHONY: binary

# Install
Expand Down
29 changes: 29 additions & 0 deletions cmd/acsfleetctl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// main package for acsfleetctl CLI
package main

import (
"os"

"github.com/spf13/cobra"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/cmd/admin"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/cmd/centrals"
)

func main() {
rootCmd := &cobra.Command{
Use: "acsfleetctl",
Long: "acsfleetctl is a CLI used to interact with the ACSCS fleet-manager API",
}

setupSubCommands(rootCmd)

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}

}

func setupSubCommands(rootCmd *cobra.Command) {
rootCmd.AddCommand(centrals.NewCentralsCommand())
rootCmd.AddCommand(admin.NewAdminCommand())
}
4 changes: 0 additions & 4 deletions cmd/fleet-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package main
import (
"flag"

"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/cmd/admin"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/cmd/centrals"
"github.com/stackrox/acs-fleet-manager/pkg/cmd/migrate"
"github.com/stackrox/acs-fleet-manager/pkg/cmd/serve"

Expand Down Expand Up @@ -48,8 +46,6 @@ func main() {

rootCmd.AddCommand(migrate.NewMigrateCommand(env))
rootCmd.AddCommand(serve.NewServeCommand(env))
rootCmd.AddCommand(centrals.NewCentralsCommand())
rootCmd.AddCommand(admin.NewAdminCommand())
// Unsupported CLI commands. Eventually some of them can be removed.
// rootCmd.AddCommand(cluster.NewClusterCommand(env))
// rootCmd.AddCommand(cloudprovider.NewCloudProviderCommand(env))
Expand Down
10 changes: 6 additions & 4 deletions internal/dinosaur/pkg/cmd/centrals/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ func NewCreateCommand() *cobra.Command {
},
}

cmd.Flags().String(FlagName, "", "Central request name")
cmd.Flags().String(FlagRegion, "us-east-1", "OCM region ID")
cmd.Flags().String(FlagProvider, "aws", "OCM provider ID")
cmd.Flags().String(FlagName, "", "Central request name (required)")
cmd.Flags().String(FlagRegion, "us-east-1", "OCM region ID (required)")
cmd.Flags().String(FlagProvider, "aws", "OCM provider ID (required)")
cmd.Flags().String(FlagOwner, "test-user", "User name")
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")

flags.MarkFlagRequired(FlagName, cmd)
flags.MarkFlagRequired(FlagRegion, cmd)
flags.MarkFlagRequired(FlagProvider, cmd)
return cmd
}

Expand Down
3 changes: 2 additions & 1 deletion internal/dinosaur/pkg/cmd/centrals/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func NewDeleteCommand() *cobra.Command {
},
}

cmd.Flags().String(FlagID, "", "Central ID")
cmd.Flags().String(FlagID, "", "Central ID (required)")
flags.MarkFlagRequired(FlagID, cmd)
return cmd
}

Expand Down
3 changes: 2 additions & 1 deletion internal/dinosaur/pkg/cmd/centrals/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func NewGetCommand() *cobra.Command {
runGet(fleetmanagerclient.AuthenticatedClientWithOCM(), cmd, args)
},
}
cmd.Flags().String(FlagID, "", "Central ID")
cmd.Flags().String(FlagID, "", "Central ID (required)")
flags.MarkFlagRequired(FlagID, cmd)

return cmd
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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 panic
func MustGetDefinedString(flagName string, flags *pflag.FlagSet) string {
flagVal := MustGetString(flagName, flags)
if flagVal == "" {
glog.Fatal(undefinedValueMessage(flagName))
panic(undefinedValueMessage(flagName))
}
return flagVal
}
Expand All @@ -21,7 +21,7 @@ func MustGetDefinedString(flagName string, flags *pflag.FlagSet) string {
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
}
Expand All @@ -30,11 +30,18 @@ func MustGetString(flagName string, flags *pflag.FlagSet) string {
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