mirror of
https://github.com/openziti/zrok.git
synced 2024-11-22 16:13:47 +01:00
better 'enable' error handling (#67)
This commit is contained in:
parent
b913aaee85
commit
ac63bd6cd8
@ -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)
|
||||
}
|
||||
|
@ -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{
|
||||
|
Loading…
Reference in New Issue
Block a user