better 'enable' error handling (#67)

This commit is contained in:
Michael Quigley 2022-09-26 13:55:15 -04:00
parent b913aaee85
commit ac63bd6cd8
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 23 additions and 2 deletions

View File

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

View File

@ -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{