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

wrap local commands to support different formats #104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/command/local/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion internal/command/local/disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion internal/command/local/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions internal/print/error.go
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this will change the exit status based on the output format, which is odd.

Can we keep the CLI exit status non zero on error? Perhaps using errors.Join when json or yaml or the default switch case, combining the argument error with the error result of the encoding would not change the exit code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-02-14 at 12 08 48 PM

WDYT on this approach?
Screenshot 2024-02-14 at 12 11 48 PM

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)
}
}