From ac63bd6cd83cd8b17dc807e23df709afbbb5236c Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 26 Sep 2022 13:55:15 -0400 Subject: [PATCH] better 'enable' error handling (#67) --- cmd/zrok/enable.go | 23 +++++++++++++++++++++-- cmd/zrok/main.go | 2 ++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cmd/zrok/enable.go b/cmd/zrok/enable.go index 70835de7..ad482c69 100644 --- a/cmd/zrok/enable.go +++ b/cmd/zrok/enable.go @@ -6,10 +6,11 @@ import ( "github.com/openziti-test-kitchen/zrok/rest_client_zrok/identity" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/zrokdir" - "github.com/pkg/errors" "github.com/shirou/gopsutil/v3/host" "github.com/spf13/cobra" + "os" user2 "os/user" + "strings" ) func init() { @@ -36,7 +37,7 @@ func newEnableCommand() *enableCommand { func (cmd *enableCommand) run(_ *cobra.Command, args []string) { env, err := zrokdir.LoadEnvironment() if err == nil { - panic(errors.Errorf("environment '%v' already enabled!", env.ZitiIdentityId)) + showError(fmt.Sprintf("you already have an environment '%v' for '%v'", env.ZitiIdentityId, env.ZrokToken), nil) } token := args[0] @@ -66,12 +67,21 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) { } resp, err := zrok.Identity.Enable(req, auth) if err != nil { + if !panicInstead { + showError("the zrok service returned an error", err) + } panic(err) } if err := zrokdir.SaveEnvironment(&zrokdir.Environment{ZrokToken: token, ZitiIdentityId: resp.Payload.Identity, ApiEndpoint: apiEndpoint}); err != nil { + if !panicInstead { + showError("there was an error saving the new environment", err) + } panic(err) } if err := zrokdir.WriteZitiIdentity("environment", resp.Payload.Cfg); err != nil { + if !panicInstead { + showError("there was an error writing the environment file", err) + } panic(err) } @@ -87,3 +97,12 @@ func getHost() (string, string, error) { info.Hostname, info.OS, info.Platform, info.PlatformFamily, info.PlatformVersion, info.KernelVersion, info.KernelArch) return info.Hostname, thisHost, nil } + +func showError(msg string, err error) { + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "ERROR: %v (%v)\n", msg, strings.TrimSpace(err.Error())) + } else { + _, _ = fmt.Fprintf(os.Stderr, "ERROR: %v\n", msg) + } + os.Exit(1) +} diff --git a/cmd/zrok/main.go b/cmd/zrok/main.go index 9114e1b9..4f7a748c 100644 --- a/cmd/zrok/main.go +++ b/cmd/zrok/main.go @@ -18,6 +18,7 @@ import ( func init() { pfxlog.GlobalInit(logrus.InfoLevel, pfxlog.DefaultOptions().SetTrimPrefix("github.com/openziti-test-kitchen/")) rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging") + rootCmd.PersistentFlags().BoolVarP(&panicInstead, "panic", "p", false, "Panic instead of showing pretty errors") apiEndpointDefault := os.Getenv("ZROK_API_ENDPOINT") if apiEndpointDefault == "" { apiEndpointDefault = "https://api.zrok.io" @@ -36,6 +37,7 @@ var rootCmd = &cobra.Command{ }, } var verbose bool +var panicInstead bool var apiEndpoint string var httpCmd = &cobra.Command{