Skip to content

Commit

Permalink
Clean up error output in cobra (#1581)
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell authored Dec 6, 2024
1 parent c8b3229 commit 4c2de3c
Showing 15 changed files with 68 additions and 33 deletions.
6 changes: 4 additions & 2 deletions cmd/installer/cli/adminconsole_resetpassword.go
Original file line number Diff line number Diff line change
@@ -13,8 +13,10 @@ import (

func AdminConsoleResetPasswordCmd(ctx context.Context, name string) *cobra.Command {
cmd := &cobra.Command{
Use: "reset-password",
Short: fmt.Sprintf("Reset the %s Admin Console password", name),
Use: "reset-password",
Short: fmt.Sprintf("Reset the %s Admin Console password", name),
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return fmt.Errorf("reset-password command must be run as root")
6 changes: 4 additions & 2 deletions cmd/installer/cli/install.go
Original file line number Diff line number Diff line change
@@ -59,8 +59,10 @@ func InstallCmd(ctx context.Context, name string) *cobra.Command {
)

cmd := &cobra.Command{
Use: "install",
Short: fmt.Sprintf("Install %s", name),
Use: "install",
Short: fmt.Sprintf("Install %s", name),
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return fmt.Errorf("install command must be run as root")
15 changes: 11 additions & 4 deletions cmd/installer/cli/install2.go
Original file line number Diff line number Diff line change
@@ -55,10 +55,11 @@ func Install2Cmd(ctx context.Context, name string) *cobra.Command {
var flags Install2CmdFlags

cmd := &cobra.Command{
Use: "install2",
Short: fmt.Sprintf("Experimental installer for %s", name),
Hidden: true,
SilenceUsage: true,
Use: "install2",
Short: fmt.Sprintf("Experimental installer for %s", name),
Hidden: true,
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return fmt.Errorf("install command must be run as root")
@@ -195,6 +196,12 @@ func runInstall2(cmd *cobra.Command, args []string, name string, flags Install2C
IgnoreHostPreflights: flags.ignoreHostPreflights,
AssumeYes: flags.assumeYes,
}); err != nil {
if err == preflights.ErrPreflightsHaveFail {
// we exit and not return an error to prevent the error from being printed to stderr
// we already handled the output
os.Exit(1)
return nil
}
return fmt.Errorf("unable to prepare and run preflights: %w", err)
}

8 changes: 5 additions & 3 deletions cmd/installer/cli/join.go
Original file line number Diff line number Diff line change
@@ -42,9 +42,11 @@ func JoinCmd(ctx context.Context, name string) *cobra.Command {
)

cmd := &cobra.Command{
Use: "join <url> <token>",
Short: fmt.Sprintf("Join %s", name),
Args: cobra.ExactArgs(2),
Use: "join <url> <token>",
Short: fmt.Sprintf("Join %s", name),
Args: cobra.ExactArgs(2),
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return fmt.Errorf("join command must be run as root")
6 changes: 4 additions & 2 deletions cmd/installer/cli/join_runpreflights.go
Original file line number Diff line number Diff line change
@@ -23,8 +23,10 @@ func JoinRunPreflightsCmd(ctx context.Context, name string) *cobra.Command {
assumeYes bool
)
cmd := &cobra.Command{
Use: "run-preflights",
Short: fmt.Sprintf("Run join host preflights for %s", name),
Use: "run-preflights",
Short: fmt.Sprintf("Run join host preflights for %s", name),
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return fmt.Errorf("run-preflights command must be run as root")
6 changes: 4 additions & 2 deletions cmd/installer/cli/materialize.go
Original file line number Diff line number Diff line change
@@ -17,8 +17,10 @@ func MaterializeCmd(ctx context.Context, name string) *cobra.Command {
)

cmd := &cobra.Command{
Use: "materialize",
Short: "Materialize embedded assets into the data directory",
Use: "materialize",
Short: "Materialize embedded assets into the data directory",
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return fmt.Errorf("materialize command must be run as root")
6 changes: 4 additions & 2 deletions cmd/installer/cli/reset.go
Original file line number Diff line number Diff line change
@@ -49,8 +49,10 @@ func ResetCmd(ctx context.Context, name string) *cobra.Command {
)

cmd := &cobra.Command{
Use: "reset",
Short: "Remove %s from the current node",
Use: "reset",
Short: "Remove %s from the current node",
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return fmt.Errorf("reset command must be run as root")
6 changes: 4 additions & 2 deletions cmd/installer/cli/restore.go
Original file line number Diff line number Diff line change
@@ -93,8 +93,10 @@ func RestoreCmd(ctx context.Context, name string) *cobra.Command {
)

cmd := &cobra.Command{
Use: "restore",
Short: fmt.Sprintf("Restore a %s cluster", name),
Use: "restore",
Short: fmt.Sprintf("Restore a %s cluster", name),
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return fmt.Errorf("restore command must be run as root")
6 changes: 4 additions & 2 deletions cmd/installer/cli/shell.go
Original file line number Diff line number Diff line change
@@ -29,8 +29,10 @@ const welcome = `

func ShellCmd(ctx context.Context, name string) *cobra.Command {
cmd := &cobra.Command{
Use: "shell",
Short: "Start a shell with access to the cluster",
Use: "shell",
Short: "Start a shell with access to the cluster",
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return fmt.Errorf("shell command must be run as root")
6 changes: 4 additions & 2 deletions cmd/installer/cli/supportbundle.go
Original file line number Diff line number Diff line change
@@ -19,8 +19,10 @@ import (

func SupportBundleCmd(ctx context.Context, name string) *cobra.Command {
cmd := &cobra.Command{
Use: "support-bundle",
Short: "Generate a support bundle for the embedded-cluster",
Use: "support-bundle",
Short: "Generate a support bundle for the embedded-cluster",
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return fmt.Errorf("support-bundle command must be run as root")
6 changes: 4 additions & 2 deletions cmd/installer/cli/update.go
Original file line number Diff line number Diff line change
@@ -19,8 +19,10 @@ func UpdateCmd(ctx context.Context, name string) *cobra.Command {
)

cmd := &cobra.Command{
Use: "update",
Short: fmt.Sprintf("Update %s", name),
Use: "update",
Short: fmt.Sprintf("Update %s", name),
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return fmt.Errorf("update command must be run as root")
6 changes: 4 additions & 2 deletions cmd/installer/cli/version.go
Original file line number Diff line number Diff line change
@@ -17,8 +17,10 @@ import (

func VersionCmd(ctx context.Context, name string) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: fmt.Sprintf("Show the %s component versions", name),
Use: "version",
Short: fmt.Sprintf("Show the %s component versions", name),
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
applierVersions, err := addons.NewApplier(addons.WithoutPrompt(), addons.OnlyDefaults(), addons.Quiet()).Versions(config.AdditionalCharts())
if err != nil {
6 changes: 4 additions & 2 deletions cmd/installer/cli/version_embeddeddata.go
Original file line number Diff line number Diff line change
@@ -11,8 +11,10 @@ import (

func VersionEmbeddedDataCmd(ctx context.Context, name string) *cobra.Command {
cmd := &cobra.Command{
Use: "embedded-data",
Short: "Read the application data embedded in the cluster",
Use: "embedded-data",
Short: "Read the application data embedded in the cluster",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
// Application
app, err := release.GetApplication()
6 changes: 4 additions & 2 deletions cmd/installer/cli/version_listimages.go
Original file line number Diff line number Diff line change
@@ -14,8 +14,10 @@ func VersionListImagesCmd(ctx context.Context, name string) *cobra.Command {
)

cmd := &cobra.Command{
Use: "list-images",
Short: "List images embedded in the cluster",
Use: "list-images",
Short: "List images embedded in the cluster",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
k0sCfg := config.RenderK0sConfig()

6 changes: 4 additions & 2 deletions cmd/installer/cli/version_metadata.go
Original file line number Diff line number Diff line change
@@ -15,8 +15,10 @@ func VersionMetadataCmd(ctx context.Context, name string) *cobra.Command {
)

cmd := &cobra.Command{
Use: "metadata",
Short: "Print metadata about this release",
Use: "metadata",
Short: "Print metadata about this release",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
k0sCfg := config.RenderK0sConfig()

0 comments on commit 4c2de3c

Please sign in to comment.