From c47aa49373311fadabe51ec0d7b39012541837b3 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Tue, 24 Jan 2023 10:48:53 +0100 Subject: [PATCH] fix: improved missing context message --- cli/app/list.go | 2 +- cli/internal/new.go | 2 +- cli/internal/validate.go | 2 +- pkg/context/context.go | 42 +++++++++++++++++++++++--- pkg/upstream/commandconn/connection.go | 2 +- 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/cli/app/list.go b/cli/app/list.go index d38626cb..470d8b31 100644 --- a/cli/app/list.go +++ b/cli/app/list.go @@ -98,7 +98,7 @@ can take some time. alreadySeen := make(map[string]bool) for _, app := range apps { if _, ok := alreadySeen[app.Server]; !ok { - if err := context.HasDockerContext(app.Server); err != nil { + if err := context.HasDockerContext(app.Name, app.Server); err != nil { logrus.Fatal(err) } diff --git a/cli/internal/new.go b/cli/internal/new.go index 56b4c681..7d44d250 100644 --- a/cli/internal/new.go +++ b/cli/internal/new.go @@ -147,7 +147,7 @@ func NewAction(c *cli.Context) error { var secrets AppSecrets var secretTable *jsontable.JSONTable if Secrets { - if err := context.HasDockerContext(NewAppServer); err != nil { + if err := context.HasDockerContext(sanitisedAppName, NewAppServer); err != nil { logrus.Fatal(err) } diff --git a/cli/internal/validate.go b/cli/internal/validate.go index 8a4debe1..8b0b9c6e 100644 --- a/cli/internal/validate.go +++ b/cli/internal/validate.go @@ -142,7 +142,7 @@ func ValidateApp(c *cli.Context) config.App { logrus.Fatal(err) } - if err := context.HasDockerContext(app.Server); err != nil { + if err := context.HasDockerContext(app.Name, app.Server); err != nil { logrus.Fatal(err) } diff --git a/pkg/context/context.go b/pkg/context/context.go index 3412180b..059c354c 100644 --- a/pkg/context/context.go +++ b/pkg/context/context.go @@ -44,10 +44,30 @@ func newContextStore(dir string, config contextStore.Config) contextStore.Store return contextStore.New(dir, config) } -// MissingContextMsg helps end-uers debug missing docker context issues. +// missingContextMsg helps end-user debug missing docker context issues. This +// version of the message has no app domain name included. This is due to the +// code paths being unable to determine which app is requesting a server +// connection at certain points. It is preferred to use +// missingContextWithAppMsg where possible and only use missingContextMsg when +// the call path is located too deep in the SSH stack. var missingContextMsg = `unable to find Docker context for %s? -Please run "abra server ls" to confirm. If you see "unknown" in the table +Please run "abra server ls -p" to confirm. If you see "unknown" in the table +output then you need to run the following command: + + abra server add %s + +See "abra server add --help" for more. +` + +// missingContextWithAppMsg helps end-users debug missing docker context +// issues. The app name is included in this message for extra clarity. See +// missingContextMsg docs for alternative usage. +var missingContextWithAppMsg = `unable to find Docker context for %s? + +%s (app) is deployed on %s (server). + +Please run "abra server ls -p" to confirm. If you see "unknown" in the table output then you need to run the following command: abra server add %s @@ -59,7 +79,7 @@ See "abra server add --help" for more. // configuration or not. This usually tells us if they'll be able to make a SSH // connection to a server or not and can be a useful way to signal to end-users // that they need to fix something up if missing. -func HasDockerContext(serverName string) error { +func HasDockerContext(appName, serverName string) error { dockerContextStore := NewDefaultDockerContextStore() contexts, err := dockerContextStore.Store.List() if err != nil { @@ -72,5 +92,19 @@ func HasDockerContext(serverName string) error { } } - return fmt.Errorf(missingContextMsg, serverName, serverName) + if appName != "" { + return fmt.Errorf( + missingContextWithAppMsg, + serverName, + appName, + serverName, + serverName, + ) + } + + return fmt.Errorf( + missingContextMsg, + serverName, + serverName, + ) } diff --git a/pkg/upstream/commandconn/connection.go b/pkg/upstream/commandconn/connection.go index 80b52a75..574c7a9a 100644 --- a/pkg/upstream/commandconn/connection.go +++ b/pkg/upstream/commandconn/connection.go @@ -37,7 +37,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.Conne return nil, errors.Wrap(err, "ssh host connection is not valid") } - if err := ctxPkg.HasDockerContext(ctxConnDetails.Host); err != nil { + if err := ctxPkg.HasDockerContext("", ctxConnDetails.Host); err != nil { return nil, err }