From 38318c68e2e0c31359ebea53acf5ef8282041f2a Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 9 Jan 2023 17:48:19 -0500 Subject: [PATCH] tui; warning --- cmd/zrok/accessPrivate.go | 15 ++++++++------- cmd/zrok/accessPublic.go | 7 ++++--- cmd/zrok/disable.go | 11 ++++++----- cmd/zrok/enable.go | 7 ++++--- cmd/zrok/invite.go | 11 ++++++----- cmd/zrok/loop.go | 3 ++- cmd/zrok/release.go | 9 +++++---- cmd/zrok/reserve.go | 13 +++++++------ cmd/zrok/sharePrivate.go | 21 +++++++++++---------- cmd/zrok/sharePublic.go | 21 +++++++++++---------- cmd/zrok/shareReserved.go | 19 ++++++++++--------- cmd/zrok/status.go | 11 ++++++----- cmd/zrok/testEndpoint.go | 5 +++-- cmd/zrok/util.go | 18 ------------------ cmd/zrok/version.go | 3 ++- tui/messages.go | 27 +++++++++++++++++++++++++++ zrokdir/version.go | 4 ++-- 17 files changed, 114 insertions(+), 91 deletions(-) create mode 100644 tui/messages.go diff --git a/cmd/zrok/accessPrivate.go b/cmd/zrok/accessPrivate.go index 798001d2..7346c30e 100644 --- a/cmd/zrok/accessPrivate.go +++ b/cmd/zrok/accessPrivate.go @@ -7,6 +7,7 @@ import ( "github.com/openziti-test-kitchen/zrok/rest_client_zrok" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/share" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -43,24 +44,24 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) { endpointUrl, err := url.Parse("http://" + cmd.bindAddress) if err != nil { if !panicInstead { - showError("invalid endpoint address", err) + tui.Error("invalid endpoint address", err) } panic(err) } zrd, err := zrokdir.Load() if err != nil { - showError("unable to load zrokdir", err) + tui.Error("unable to load zrokdir", err) } if zrd.Env == nil { - showError("unable to load environment; did you 'zrok enable'?", nil) + tui.Error("unable to load environment; did you 'zrok enable'?", nil) } zrok, err := zrd.Client() if err != nil { if !panicInstead { - showError("unable to create zrok client", err) + tui.Error("unable to create zrok client", err) } panic(err) } @@ -74,7 +75,7 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) { accessResp, err := zrok.Share.Access(req, auth) if err != nil { if !panicInstead { - showError("unable to access", err) + tui.Error("unable to access", err) } panic(err) } @@ -95,7 +96,7 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) { frontend, err := privateFrontend.NewHTTP(cfg) if err != nil { if !panicInstead { - showError("unable to create private frontend", err) + tui.Error("unable to create private frontend", err) } panic(err) } @@ -104,7 +105,7 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) { if err := frontend.Run(); err != nil { if !panicInstead { - showError("unable to run frontend", err) + tui.Error("unable to run frontend", err) } } } diff --git a/cmd/zrok/accessPublic.go b/cmd/zrok/accessPublic.go index 6a7ef637..9e08f471 100644 --- a/cmd/zrok/accessPublic.go +++ b/cmd/zrok/accessPublic.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/michaelquigley/cf" "github.com/openziti-test-kitchen/zrok/endpoints/publicFrontend" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -33,7 +34,7 @@ func (self *accessPublicCommand) run(_ *cobra.Command, args []string) { if len(args) == 1 { if err := cfg.Load(args[0]); err != nil { if !panicInstead { - showError(fmt.Sprintf("unable to load configuration '%v'", args[0]), err) + tui.Error(fmt.Sprintf("unable to load configuration '%v'", args[0]), err) } panic(err) } @@ -42,13 +43,13 @@ func (self *accessPublicCommand) run(_ *cobra.Command, args []string) { frontend, err := publicFrontend.NewHTTP(cfg) if err != nil { if !panicInstead { - showError("unable to create http frontend", err) + tui.Error("unable to create http frontend", err) } panic(err) } if err := frontend.Run(); err != nil { if !panicInstead { - showError("unable to run http frontend", err) + tui.Error("unable to run http frontend", err) } panic(err) } diff --git a/cmd/zrok/disable.go b/cmd/zrok/disable.go index 7df40a03..f305eac7 100644 --- a/cmd/zrok/disable.go +++ b/cmd/zrok/disable.go @@ -5,6 +5,7 @@ import ( httptransport "github.com/go-openapi/runtime/client" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/environment" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -33,19 +34,19 @@ func (cmd *disableCommand) run(_ *cobra.Command, _ []string) { zrd, err := zrokdir.Load() if err != nil { if !panicInstead { - showError("unable to load zrokdir", err) + tui.Error("unable to load zrokdir", err) } panic(err) } if zrd.Env == nil { - showError("no environment found; nothing to disable!", nil) + tui.Error("no environment found; nothing to disable!", nil) } zrok, err := zrd.Client() if err != nil { if !panicInstead { - showError("could not create zrok client", err) + tui.Error("could not create zrok client", err) } panic(err) } @@ -60,13 +61,13 @@ func (cmd *disableCommand) run(_ *cobra.Command, _ []string) { } if err := zrokdir.DeleteEnvironment(); err != nil { if !panicInstead { - showError("error removing zrok environment", err) + tui.Error("error removing zrok environment", err) } panic(err) } if err := zrokdir.DeleteZitiIdentity("backend"); err != nil { if !panicInstead { - showError("error removing zrok backend identity", err) + tui.Error("error removing zrok backend identity", err) } } fmt.Printf("zrok environment '%v' disabled for '%v'\n", zrd.Env.ZId, zrd.Env.Token) diff --git a/cmd/zrok/enable.go b/cmd/zrok/enable.go index 9469e60c..e220123d 100644 --- a/cmd/zrok/enable.go +++ b/cmd/zrok/enable.go @@ -5,6 +5,7 @@ import ( httptransport "github.com/go-openapi/runtime/client" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/environment" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/shirou/gopsutil/v3/host" "github.com/spf13/cobra" @@ -65,7 +66,7 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) { resp, err := zrok.Environment.Enable(req, auth) if err != nil { if !panicInstead { - showError("the zrok service returned an error", err) + tui.Error("the zrok service returned an error", err) } panic(err) } @@ -73,13 +74,13 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) { zrd.Env = &zrokdir.Environment{Token: token, ZId: resp.Payload.Identity, ApiEndpoint: apiEndpoint} if err := zrd.Save(); err != nil { if !panicInstead { - showError("there was an error saving the new environment", err) + tui.Error("there was an error saving the new environment", err) } panic(err) } if err := zrokdir.SaveZitiIdentity("backend", resp.Payload.Cfg); err != nil { if !panicInstead { - showError("there was an error writing the environment file", err) + tui.Error("there was an error writing the environment file", err) } panic(err) } diff --git a/cmd/zrok/invite.go b/cmd/zrok/invite.go index 9654d363..8a5a91d4 100644 --- a/cmd/zrok/invite.go +++ b/cmd/zrok/invite.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/account" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/openziti-test-kitchen/zrok/util" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/openziti/foundation/v2/term" @@ -35,25 +36,25 @@ func (cmd *inviteCommand) run(_ *cobra.Command, _ []string) { panic(err) } if !util.IsValidEmail(email) { - showError(fmt.Sprintf("'%v' is not a valid email address", email), nil) + tui.Error(fmt.Sprintf("'%v' is not a valid email address", email), nil) } confirm, err := term.Prompt("Confirm Email: ") if err != nil { panic(err) } if confirm != email { - showError("entered emails do not match... aborting!", nil) + tui.Error("entered emails do not match... aborting!", nil) } zrd, err := zrokdir.Load() if err != nil { - showError("error loading zrokdir", err) + tui.Error("error loading zrokdir", err) } zrok, err := zrd.Client() if err != nil { if !panicInstead { - showError("error creating zrok api client", err) + tui.Error("error creating zrok api client", err) } panic(err) } @@ -64,7 +65,7 @@ func (cmd *inviteCommand) run(_ *cobra.Command, _ []string) { _, err = zrok.Account.Invite(req) if err != nil { if !panicInstead { - showError("error creating invitation", err) + tui.Error("error creating invitation", err) } panic(err) } diff --git a/cmd/zrok/loop.go b/cmd/zrok/loop.go index 74bd3778..71486a99 100644 --- a/cmd/zrok/loop.go +++ b/cmd/zrok/loop.go @@ -10,6 +10,7 @@ import ( "github.com/openziti-test-kitchen/zrok/rest_client_zrok" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/share" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/openziti-test-kitchen/zrok/util" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/openziti/sdk-golang/ziti" @@ -176,7 +177,7 @@ func (l *looper) startup() { } if zrd.Env == nil { - showError("unable to load environment; did you 'zrok enable'?", nil) + tui.Error("unable to load environment; did you 'zrok enable'?", nil) } l.env = zrd.Env diff --git a/cmd/zrok/release.go b/cmd/zrok/release.go index f16e4b8a..a86ea2a9 100644 --- a/cmd/zrok/release.go +++ b/cmd/zrok/release.go @@ -4,6 +4,7 @@ import ( httptransport "github.com/go-openapi/runtime/client" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/share" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -33,19 +34,19 @@ func (cmd *releaseCommand) run(_ *cobra.Command, args []string) { zrd, err := zrokdir.Load() if err != nil { if !panicInstead { - showError("unable to load zrokdir", err) + tui.Error("unable to load zrokdir", err) } panic(err) } if zrd.Env == nil { - showError("unable to load environment; did you 'zrok enable'?", nil) + tui.Error("unable to load environment; did you 'zrok enable'?", nil) } zrok, err := zrd.Client() if err != nil { if !panicInstead { - showError("unable to create zrok client", err) + tui.Error("unable to create zrok client", err) } panic(err) } @@ -59,7 +60,7 @@ func (cmd *releaseCommand) run(_ *cobra.Command, args []string) { } if _, err := zrok.Share.Unshare(req, auth); err != nil { if !panicInstead { - showError("error releasing share", err) + tui.Error("error releasing share", err) } panic(err) } diff --git a/cmd/zrok/reserve.go b/cmd/zrok/reserve.go index 50e315b3..501639e5 100644 --- a/cmd/zrok/reserve.go +++ b/cmd/zrok/reserve.go @@ -5,6 +5,7 @@ import ( "github.com/openziti-test-kitchen/zrok/model" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/share" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -39,13 +40,13 @@ func newReserveCommand() *reserveCommand { func (cmd *reserveCommand) run(_ *cobra.Command, args []string) { shareMode := args[0] if shareMode != "public" && shareMode != "private" { - showError("invalid sharing mode; expecting 'public' or 'private'", nil) + tui.Error("invalid sharing mode; expecting 'public' or 'private'", nil) } targetEndpoint, err := url.Parse(args[1]) if err != nil { if !panicInstead { - showError("invalid target endpoint URL", err) + tui.Error("invalid target endpoint URL", err) } panic(err) } @@ -56,19 +57,19 @@ func (cmd *reserveCommand) run(_ *cobra.Command, args []string) { zrd, err := zrokdir.Load() if err != nil { if !panicInstead { - showError("error loading zrokdir", err) + tui.Error("error loading zrokdir", err) } panic(err) } if zrd.Env == nil { - showError("unable to load environment; did you 'zrok enable'?", nil) + tui.Error("unable to load environment; did you 'zrok enable'?", nil) } zrok, err := zrd.Client() if err != nil { if !panicInstead { - showError("unable to create zrok client", err) + tui.Error("unable to create zrok client", err) } panic(err) } @@ -101,7 +102,7 @@ func (cmd *reserveCommand) run(_ *cobra.Command, args []string) { resp, err := zrok.Share.Share(req, auth) if err != nil { if !panicInstead { - showError("unable to create tunnel", err) + tui.Error("unable to create tunnel", err) } panic(err) } diff --git a/cmd/zrok/sharePrivate.go b/cmd/zrok/sharePrivate.go index 0ee7c686..f13e6c2d 100644 --- a/cmd/zrok/sharePrivate.go +++ b/cmd/zrok/sharePrivate.go @@ -10,6 +10,7 @@ import ( "github.com/openziti-test-kitchen/zrok/rest_client_zrok" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/share" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -53,7 +54,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { targetEndpoint, err := url.Parse(args[0]) if err != nil { if !panicInstead { - showError("invalid target endpoint URL", err) + tui.Error("invalid target endpoint URL", err) } panic(err) } @@ -66,25 +67,25 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { target = args[0] default: - showError(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", cmd.backendMode), nil) + tui.Error(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", cmd.backendMode), nil) } zrd, err := zrokdir.Load() if err != nil { if !panicInstead { - showError("unable to load zrokdir", err) + tui.Error("unable to load zrokdir", err) } panic(err) } if zrd.Env == nil { - showError("unable to load environment; did you 'zrok enable'?", nil) + tui.Error("unable to load environment; did you 'zrok enable'?", nil) } zif, err := zrokdir.ZitiIdentityFile("backend") if err != nil { if !panicInstead { - showError("unable to load ziti identity configuration", err) + tui.Error("unable to load ziti identity configuration", err) } panic(err) } @@ -92,7 +93,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { zrok, err := zrd.Client() if err != nil { if !panicInstead { - showError("unable to create zrok client", err) + tui.Error("unable to create zrok client", err) } panic(err) } @@ -121,7 +122,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { resp, err := zrok.Share.Share(req, auth) if err != nil { if !panicInstead { - showError("unable to create share", err) + tui.Error("unable to create share", err) } panic(err) } @@ -144,7 +145,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { _, err = cmd.proxyBackendMode(cfg) if err != nil { if !panicInstead { - showError("unable to create proxy backend handler", err) + tui.Error("unable to create proxy backend handler", err) } panic(err) } @@ -158,13 +159,13 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { _, err = cmd.webBackendMode(cfg) if err != nil { if !panicInstead { - showError("unable to create web backend handler", err) + tui.Error("unable to create web backend handler", err) } panic(err) } default: - showError("invalid backend mode", nil) + tui.Error("invalid backend mode", nil) } logrus.Infof("share with others; they will use this command for access: 'zrok access private %v'", resp.Payload.ShrToken) diff --git a/cmd/zrok/sharePublic.go b/cmd/zrok/sharePublic.go index 0163f94b..894b05c0 100644 --- a/cmd/zrok/sharePublic.go +++ b/cmd/zrok/sharePublic.go @@ -10,6 +10,7 @@ import ( "github.com/openziti-test-kitchen/zrok/rest_client_zrok" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/share" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -55,7 +56,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) { targetEndpoint, err := url.Parse(args[0]) if err != nil { if !panicInstead { - showError("invalid target endpoint URL", err) + tui.Error("invalid target endpoint URL", err) } panic(err) } @@ -68,25 +69,25 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) { target = args[0] default: - showError(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", cmd.backendMode), nil) + tui.Error(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", cmd.backendMode), nil) } zrd, err := zrokdir.Load() if err != nil { if !panicInstead { - showError("unable to load zrokdir", nil) + tui.Error("unable to load zrokdir", nil) } panic(err) } if zrd.Env == nil { - showError("unable to load environment; did you 'zrok enable'?", nil) + tui.Error("unable to load environment; did you 'zrok enable'?", nil) } zif, err := zrokdir.ZitiIdentityFile("backend") if err != nil { if !panicInstead { - showError("unable to load ziti identity configuration", err) + tui.Error("unable to load ziti identity configuration", err) } panic(err) } @@ -94,7 +95,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) { zrok, err := zrd.Client() if err != nil { if !panicInstead { - showError("unable to create zrok client", err) + tui.Error("unable to create zrok client", err) } panic(err) } @@ -123,7 +124,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) { resp, err := zrok.Share.Share(req, auth) if err != nil { if !panicInstead { - showError("unable to create tunnel", err) + tui.Error("unable to create tunnel", err) } panic(err) } @@ -147,7 +148,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) { bh, err = cmd.proxyBackendMode(cfg) if err != nil { if !panicInstead { - showError("unable to create proxy backend handler", err) + tui.Error("unable to create proxy backend handler", err) } panic(err) } @@ -161,13 +162,13 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) { bh, err = cmd.webBackendMode(cfg) if err != nil { if !panicInstead { - showError("unable to create web backend handler", err) + tui.Error("unable to create web backend handler", err) } panic(err) } default: - showError("invalid backend mode", nil) + tui.Error("invalid backend mode", nil) } logrus.Infof("access your zrok share: %v", resp.Payload.FrontendProxyEndpoints[0]) diff --git a/cmd/zrok/shareReserved.go b/cmd/zrok/shareReserved.go index c0ea9f86..2b7e8617 100644 --- a/cmd/zrok/shareReserved.go +++ b/cmd/zrok/shareReserved.go @@ -6,6 +6,7 @@ import ( "github.com/openziti-test-kitchen/zrok/rest_client_zrok/metadata" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/share" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -40,7 +41,7 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) { e, err := url.Parse(cmd.overrideEndpoint) if err != nil { if !panicInstead { - showError("invalid override endpoint URL", err) + tui.Error("invalid override endpoint URL", err) } panic(err) } @@ -53,19 +54,19 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) { zrd, err := zrokdir.Load() if err != nil { if !panicInstead { - showError("error loading zrokdir", err) + tui.Error("error loading zrokdir", err) } panic(err) } if zrd.Env == nil { - showError("unable to load environment; did you 'zrok enable'?", nil) + tui.Error("unable to load environment; did you 'zrok enable'?", nil) } zrok, err := zrd.Client() if err != nil { if !panicInstead { - showError("unable to create zrok client", err) + tui.Error("unable to create zrok client", err) } panic(err) } @@ -75,7 +76,7 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) { resp, err := zrok.Metadata.GetShareDetail(req, auth) if err != nil { if !panicInstead { - showError("unable to retrieve reserved share", err) + tui.Error("unable to retrieve reserved share", err) } panic(err) } @@ -86,7 +87,7 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) { zif, err := zrokdir.ZitiIdentityFile("backend") if err != nil { if !panicInstead { - showError("unable to load ziti identity configuration", err) + tui.Error("unable to load ziti identity configuration", err) } panic(err) } @@ -105,7 +106,7 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) { } if _, err := zrok.Share.UpdateShare(upReq, auth); err != nil { if !panicInstead { - showError("unable to update backend proxy endpoint", err) + tui.Error("unable to update backend proxy endpoint", err) } panic(err) } @@ -117,7 +118,7 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) { httpProxy, err := proxyBackend.NewBackend(cfg) if err != nil { if !panicInstead { - showError("unable to create http backend", err) + tui.Error("unable to create http backend", err) } panic(err) } @@ -125,7 +126,7 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) { go func() { if err := httpProxy.Run(); err != nil { if !panicInstead { - showError("unable to run http proxy", err) + tui.Error("unable to run http proxy", err) } panic(err) } diff --git a/cmd/zrok/status.go b/cmd/zrok/status.go index 3d8620cb..4533a06f 100644 --- a/cmd/zrok/status.go +++ b/cmd/zrok/status.go @@ -3,6 +3,7 @@ package main import ( "fmt" "github.com/jedib0t/go-pretty/v6/table" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/spf13/cobra" "os" @@ -33,10 +34,10 @@ func (cmd *statusCommand) run(_ *cobra.Command, _ []string) { zrd, err := zrokdir.Load() if err != nil { - showError("unable to load zrokdir", err) + tui.Error("unable to load zrokdir", err) } - _, _ = fmt.Fprintf(os.Stdout, codeStyle.Render("Config")+":\n\n") + _, _ = fmt.Fprintf(os.Stdout, tui.CodeStyle.Render("Config")+":\n\n") t := table.NewWriter() t.SetOutputMirror(os.Stdout) t.SetStyle(table.StyleColoredDark) @@ -47,10 +48,10 @@ func (cmd *statusCommand) run(_ *cobra.Command, _ []string) { _, _ = fmt.Fprintf(os.Stderr, "\n") if zrd.Env == nil { - _, _ = fmt.Fprintf(os.Stderr, "%v: Unable to load your local environment!\n\n", warningLabel) - _, _ = fmt.Fprintf(os.Stderr, "To create a local environment use the %v command.\n", codeStyle.Render("zrok enable")) + _, _ = fmt.Fprintf(os.Stderr, "%v: Unable to load your local environment!\n\n", tui.WarningLabel) + _, _ = fmt.Fprintf(os.Stderr, "To create a local environment use the %v command.\n", tui.CodeStyle.Render("zrok enable")) } else { - _, _ = fmt.Fprintf(os.Stdout, codeStyle.Render("Environment")+":\n\n") + _, _ = fmt.Fprintf(os.Stdout, tui.CodeStyle.Render("Environment")+":\n\n") t := table.NewWriter() t.SetOutputMirror(os.Stdout) diff --git a/cmd/zrok/testEndpoint.go b/cmd/zrok/testEndpoint.go index 57cf7867..8cf61225 100644 --- a/cmd/zrok/testEndpoint.go +++ b/cmd/zrok/testEndpoint.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/opentracing/opentracing-go/log" "github.com/openziti-test-kitchen/zrok/cmd/zrok/endpointUi" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "html/template" @@ -33,7 +34,7 @@ func newTestEndpointCommand() *testEndpointCommand { var err error if command.t, err = template.ParseFS(endpointUi.FS, "index.gohtml"); err != nil { if !panicInstead { - showError("unable to parse index template", err) + tui.Error("unable to parse index template", err) } panic(err) } @@ -47,7 +48,7 @@ func (cmd *testEndpointCommand) run(_ *cobra.Command, _ []string) { http.HandleFunc("/", cmd.serveIndex) if err := http.ListenAndServe(fmt.Sprintf("%v:%d", cmd.address, cmd.port), nil); err != nil { if !panicInstead { - showError("unable to start http listener", err) + tui.Error("unable to start http listener", err) } panic(err) } diff --git a/cmd/zrok/util.go b/cmd/zrok/util.go index 17cbb9b4..be53c919 100644 --- a/cmd/zrok/util.go +++ b/cmd/zrok/util.go @@ -1,12 +1,9 @@ package main import ( - "fmt" - "github.com/charmbracelet/lipgloss" "github.com/go-openapi/runtime" httptransport "github.com/go-openapi/runtime/client" "os" - "strings" ) type backendHandler interface { @@ -20,18 +17,3 @@ func mustGetAdminAuth() runtime.ClientAuthInfoWriter { } return httptransport.APIKeyAuth("X-TOKEN", "header", adminToken) } - -var errorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#D90166")) -var errorLabel = errorStyle.Render("ERROR") -var warningStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFA500")) -var warningLabel = warningStyle.Render("WARNING") -var codeStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#00FFFF")) - -func showError(msg string, err error) { - if err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%v: %v (%v)\n", errorLabel, msg, strings.TrimSpace(err.Error())) - } else { - _, _ = fmt.Fprintf(os.Stderr, "%v %v\n", errorLabel, msg) - } - os.Exit(1) -} diff --git a/cmd/zrok/version.go b/cmd/zrok/version.go index 84829686..23079c1a 100644 --- a/cmd/zrok/version.go +++ b/cmd/zrok/version.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/charmbracelet/lipgloss" "github.com/openziti-test-kitchen/zrok/build" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/spf13/cobra" ) @@ -27,5 +28,5 @@ func newVersionCommand() *versionCommand { func (cmd *versionCommand) run(_ *cobra.Command, _ []string) { zrokStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#FF00EE")) - fmt.Println(zrokStyle.Render(" _ \n _____ __ ___ | | __\n|_ / '__/ _ \\| |/ /\n / /| | | (_) | < \n/___|_| \\___/|_|\\_\\") + "\n\n" + codeStyle.Render(build.String()) + "\n") + fmt.Println(zrokStyle.Render(" _ \n _____ __ ___ | | __\n|_ / '__/ _ \\| |/ /\n / /| | | (_) | < \n/___|_| \\___/|_|\\_\\") + "\n\n" + tui.CodeStyle.Render(build.String()) + "\n") } diff --git a/tui/messages.go b/tui/messages.go new file mode 100644 index 00000000..16331812 --- /dev/null +++ b/tui/messages.go @@ -0,0 +1,27 @@ +package tui + +import ( + "fmt" + "github.com/charmbracelet/lipgloss" + "os" + "strings" +) + +var ErrorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#D90166")) +var ErrorLabel = ErrorStyle.Render("ERROR") +var WarningStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFA500")) +var WarningLabel = WarningStyle.Render("WARNING") +var CodeStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#00FFFF")) + +func Error(msg string, err error) { + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "%v: %v (%v)\n", ErrorLabel, msg, strings.TrimSpace(err.Error())) + } else { + _, _ = fmt.Fprintf(os.Stderr, "%v %v\n", ErrorLabel, msg) + } + os.Exit(1) +} + +func Warning(msg string, v ...interface{}) { + _, _ = fmt.Fprintf(os.Stderr, "%v: "+msg, v...) +} diff --git a/zrokdir/version.go b/zrokdir/version.go index 57f43068..8a4f5462 100644 --- a/zrokdir/version.go +++ b/zrokdir/version.go @@ -2,8 +2,8 @@ package zrokdir import ( "encoding/json" + "github.com/openziti-test-kitchen/zrok/tui" "github.com/pkg/errors" - "github.com/sirupsen/logrus" "os" ) @@ -20,7 +20,7 @@ func checkMetadata() error { } data, err := os.ReadFile(mf) if err != nil { - logrus.Warnf("unable to read zrokdir metadata; ignoring non-existent: %v", err) + tui.Warning("unable to open zrokdir metadata; ignoring") return nil } m := &Metadata{}