From 62b45821298cd6861473fdc61ab6397b98f2505d Mon Sep 17 00:00:00 2001 From: David Orozco Date: Wed, 14 Feb 2024 11:00:02 -0500 Subject: [PATCH] wrap local commands to support different formats --- internal/command/local/connect.go | 7 ++++++- internal/command/local/disconnect.go | 7 ++++++- internal/command/local/status.go | 6 +++++- internal/print/error.go | 31 ++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 internal/print/error.go diff --git a/internal/command/local/connect.go b/internal/command/local/connect.go index abdfa7b..14b1312 100644 --- a/internal/command/local/connect.go +++ b/internal/command/local/connect.go @@ -16,6 +16,7 @@ import ( "github.com/signadot/cli/internal/config" sbmapi "github.com/signadot/cli/internal/locald/api/sandboxmanager" sbmgr "github.com/signadot/cli/internal/locald/sandboxmanager" + "github.com/signadot/cli/internal/print" "github.com/signadot/cli/internal/utils/system" "github.com/signadot/libconnect/common/processes" "github.com/spf13/cobra" @@ -29,7 +30,11 @@ func newConnect(localConfig *config.Local) *cobra.Command { Use: "connect", Short: "connect local machine to cluster", RunE: func(cmd *cobra.Command, args []string) error { - return runConnect(cmd, cmd.OutOrStdout(), cfg, args) + if err := runConnect(cmd, cmd.OutOrStdout(), cfg, args); err != nil { + return print.Error(cmd.OutOrStdout(), err, cfg.OutputFormat) + } + + return nil }, } cfg.AddFlags(cmd) diff --git a/internal/command/local/disconnect.go b/internal/command/local/disconnect.go index 536a988..8399dfd 100644 --- a/internal/command/local/disconnect.go +++ b/internal/command/local/disconnect.go @@ -12,6 +12,7 @@ import ( rmapi "github.com/signadot/cli/internal/locald/api/rootmanager" sbmapi "github.com/signadot/cli/internal/locald/api/sandboxmanager" sbmgr "github.com/signadot/cli/internal/locald/sandboxmanager" + "github.com/signadot/cli/internal/print" "github.com/signadot/cli/internal/utils/system" "github.com/signadot/go-sdk/client/sandboxes" "github.com/signadot/libconnect/common/processes" @@ -27,7 +28,11 @@ func newDisconnect(localConfig *config.Local) *cobra.Command { Use: "disconnect", Short: "disconnect local machine from cluster", RunE: func(cmd *cobra.Command, args []string) error { - return runDisconnect(cfg, args) + if err := runDisconnect(cfg, args); err != nil { + return print.Error(cmd.OutOrStdout(), err, cfg.OutputFormat) + } + + return nil }, } cfg.AddFlags(cmd) diff --git a/internal/command/local/status.go b/internal/command/local/status.go index f161f02..42fbbcd 100644 --- a/internal/command/local/status.go +++ b/internal/command/local/status.go @@ -20,7 +20,11 @@ func newStatus(localConfig *config.Local) *cobra.Command { Use: "status", Short: "show status of the local machine's connection with cluster", RunE: func(cmd *cobra.Command, args []string) error { - return runStatus(cfg, cmd.OutOrStdout(), args) + if err := runStatus(cfg, cmd.OutOrStdout(), args); err != nil { + return print.Error(cmd.OutOrStdout(), err, cfg.OutputFormat) + } + + return nil }, } cfg.AddFlags(cmd) diff --git a/internal/print/error.go b/internal/print/error.go new file mode 100644 index 0000000..72d12c2 --- /dev/null +++ b/internal/print/error.go @@ -0,0 +1,31 @@ +package print + +import ( + "fmt" + "github.com/signadot/cli/internal/config" + "io" +) + +func printErrorJson(err error) error { + + return nil +} + +func Error(out io.Writer, err error, outputFormat config.OutputFormat) error { + type errorResponse struct { + Error string `json:"error"` + } + + rawResponse := errorResponse{Error: err.Error()} + + switch outputFormat { + case config.OutputFormatDefault: + return err + case config.OutputFormatJSON: + return RawJSON(out, rawResponse) + case config.OutputFormatYAML: + return RawK8SYAML(out, rawResponse) + default: + return fmt.Errorf("unsupported output format: %q", outputFormat) + } +}