From d9b482cc789fedaf3d343da7f96df2fffc5ed3d2 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Wed, 15 Jan 2025 17:11:17 +0000 Subject: [PATCH] Refactor legacy CLI invocation --- commands/completion.go | 40 +++++++--------------------------------- commands/list.go | 33 ++++++++------------------------- commands/root.go | 41 ++++++++++++++++++----------------------- 3 files changed, 33 insertions(+), 81 deletions(-) diff --git a/commands/completion.go b/commands/completion.go index 02dc0b97..267e19b0 100644 --- a/commands/completion.go +++ b/commands/completion.go @@ -2,18 +2,13 @@ package commands import ( "bytes" - "errors" "fmt" - "os" - "os/exec" - "path" + "path/filepath" "strings" "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/platformsh/cli/internal/config" - "github.com/platformsh/cli/internal/legacy" ) func newCompletionCommand(cnf *config.Config) *cobra.Command { @@ -28,42 +23,21 @@ func newCompletionCommand(cnf *config.Config) *cobra.Command { completionArgs = append(completionArgs, "--shell-type", args[0]) } var b bytes.Buffer - c := &legacy.CLIWrapper{ - Config: cnf, - Version: version, - CustomPharPath: viper.GetString("phar-path"), - Debug: viper.GetBool("debug"), - DebugLogFunc: debugLog, - DisableInteraction: viper.GetBool("no-interaction"), - Stdout: &b, - Stderr: cmd.ErrOrStderr(), - Stdin: cmd.InOrStdin(), - } - - if err := c.Init(); err != nil { - debugLog(err.Error()) - os.Exit(1) - return - } + c := makeLegacyCLIWrapper(cnf, &b, cmd.ErrOrStderr(), cmd.InOrStdin()) if err := c.Exec(cmd.Context(), completionArgs...); err != nil { - debugLog(err.Error()) - exitCode := 1 - var execErr *exec.ExitError - if errors.As(err, &execErr) { - exitCode = execErr.ExitCode() - } - os.Exit(exitCode) - return + exitWithError(err) } + pharPath := c.PharPath() + completions := strings.ReplaceAll( strings.ReplaceAll( b.String(), - c.PharPath(), + pharPath, cnf.Application.Executable, ), - path.Base(c.PharPath()), + filepath.Base(pharPath), cnf.Application.Executable, ) fmt.Fprintln(cmd.OutOrStdout(), "#compdef "+cnf.Application.Executable) diff --git a/commands/list.go b/commands/list.go index 6835a37e..ba9304e8 100644 --- a/commands/list.go +++ b/commands/list.go @@ -9,7 +9,6 @@ import ( "github.com/spf13/viper" "github.com/platformsh/cli/internal/config" - "github.com/platformsh/cli/internal/legacy" ) func newListCommand(cnf *config.Config) *cobra.Command { @@ -18,23 +17,6 @@ func newListCommand(cnf *config.Config) *cobra.Command { Short: "Lists commands", Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - var b bytes.Buffer - c := &legacy.CLIWrapper{ - Config: cnf, - Version: version, - CustomPharPath: viper.GetString("phar-path"), - Debug: viper.GetBool("debug"), - DebugLogFunc: debugLog, - DisableInteraction: viper.GetBool("no-interaction"), - Stdout: &b, - Stderr: cmd.ErrOrStderr(), - Stdin: cmd.InOrStdin(), - } - if err := c.Init(); err != nil { - exitWithError(cmd, err) - return - } - arguments := []string{"list", "--format=json"} if viper.GetBool("all") { arguments = append(arguments, "--all") @@ -42,15 +24,17 @@ func newListCommand(cnf *config.Config) *cobra.Command { if len(args) > 0 { arguments = append(arguments, args[0]) } + + var b bytes.Buffer + c := makeLegacyCLIWrapper(cnf, &b, cmd.ErrOrStderr(), cmd.InOrStdin()) + if err := c.Exec(cmd.Context(), arguments...); err != nil { - exitWithError(cmd, err) - return + exitWithError(err) } var list List if err := json.Unmarshal(b.Bytes(), &list); err != nil { - exitWithError(cmd, err) - return + exitWithError(err) } // Override the application name and executable with our own config. @@ -88,15 +72,14 @@ func newListCommand(cnf *config.Config) *cobra.Command { c.Stdout = cmd.OutOrStdout() arguments := []string{"list", "--format=" + format} if err := c.Exec(cmd.Context(), arguments...); err != nil { - exitWithError(cmd, err) + exitWithError(err) } return } result, err := formatter.Format(&list, config.FromContext(cmd.Context())) if err != nil { - exitWithError(cmd, err) - return + exitWithError(err) } fmt.Fprintln(cmd.OutOrStdout(), string(result)) diff --git a/commands/root.go b/commands/root.go index 6dcbf232..48ccebd0 100644 --- a/commands/root.go +++ b/commands/root.go @@ -76,7 +76,10 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob } }, Run: func(cmd *cobra.Command, _ []string) { - runLegacyCLI(cmd.Context(), cnf, cmd.OutOrStdout(), cmd.ErrOrStderr(), cmd.InOrStdin(), os.Args[1:]) + c := makeLegacyCLIWrapper(cnf, cmd.OutOrStdout(), cmd.ErrOrStderr(), cmd.InOrStdin()) + if err := c.Exec(cmd.Context(), os.Args[1:]...); err != nil { + exitWithError(err) + } }, PersistentPostRun: func(cmd *cobra.Command, _ []string) { checkShellConfigLeftovers(cmd.ErrOrStderr(), cnf) @@ -103,7 +106,10 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob args = []string{"help"} } - runLegacyCLI(cmd.Context(), cnf, cmd.OutOrStdout(), cmd.ErrOrStderr(), cmd.InOrStdin(), args) + c := makeLegacyCLIWrapper(cnf, cmd.OutOrStdout(), cmd.ErrOrStderr(), cmd.InOrStdin()) + if err := c.Exec(cmd.Context(), args...); err != nil { + exitWithError(err) + } }) cmd.PersistentFlags().BoolP("version", "V", false, fmt.Sprintf("Displays the %s version", cnf.Application.Name)) @@ -239,18 +245,21 @@ func debugLog(format string, v ...any) { fmt.Fprintf(color.Error, prefix+" "+strings.TrimSpace(format)+"\n", v...) } -func exitWithError(cmd *cobra.Command, err error) { - cmd.PrintErrln(color.RedString(err.Error())) - exitCode := 1 +func exitWithError(err error) { var execErr *exec.ExitError if errors.As(err, &execErr) { - exitCode = execErr.ExitCode() + exitCode := execErr.ExitCode() + debugLog(err.Error()) + os.Exit(exitCode) } - os.Exit(exitCode) + if !viper.GetBool("quiet") { + fmt.Fprintln(color.Error, color.RedString(err.Error())) + } + os.Exit(1) } -func runLegacyCLI(ctx context.Context, cnf *config.Config, stdout, stderr io.Writer, stdin io.Reader, args []string) { - c := &legacy.CLIWrapper{ +func makeLegacyCLIWrapper(cnf *config.Config, stdout, stderr io.Writer, stdin io.Reader) *legacy.CLIWrapper { + return &legacy.CLIWrapper{ Config: cnf, Version: version, CustomPharPath: viper.GetString("phar-path"), @@ -261,18 +270,4 @@ func runLegacyCLI(ctx context.Context, cnf *config.Config, stdout, stderr io.Wri Stderr: stderr, Stdin: stdin, } - if err := c.Init(); err != nil { - fmt.Fprintln(stderr, color.RedString(err.Error())) - os.Exit(1) - } - - if err := c.Exec(ctx, args...); err != nil { - debugLog("%s\n", color.RedString(err.Error())) - exitCode := 1 - var execErr *exec.ExitError - if errors.As(err, &execErr) { - exitCode = execErr.ExitCode() - } - os.Exit(exitCode) - } }