From 98f7a8e1aaa6fcbc0bbd4e5c6e022ac8b8c509bf Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Mon, 15 Jun 2020 10:04:01 +0200 Subject: [PATCH] When can not load config file, the previous cli is displaying a WARNING message, but continues with default context, we should do the same. This happened in some desktop e2e tests where the config file is not set properly in WSL2 environment, see https://github.com/docker/pinata/pull/14062 --- cli/main.go | 12 +++++------- cli/main_test.go | 12 ++++-------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/cli/main.go b/cli/main.go index 04e92fca7..6b63306b9 100644 --- a/cli/main.go +++ b/cli/main.go @@ -148,10 +148,7 @@ func main() { configDir := opts.Config ctx = config.WithDir(ctx, configDir) - currentContext, err := determineCurrentContext(opts.Context, configDir) - if err != nil { - fatal(errors.Wrap(err, "unable to determine current context")) - } + currentContext := determineCurrentContext(opts.Context, configDir) s, err := store.New(store.WithRoot(configDir)) if err != nil { @@ -200,19 +197,20 @@ func newSigContext() (context.Context, func()) { return ctx, cancel } -func determineCurrentContext(flag string, configDir string) (string, error) { +func determineCurrentContext(flag string, configDir string) string { res := flag if res == "" { config, err := config.LoadFile(configDir) if err != nil { - return "", err + fmt.Fprintln(os.Stderr, errors.Wrap(err, "WARNING")) + return "default" } res = config.CurrentContext } if res == "" { res = "default" } - return res, nil + return res } func fatal(err error) { diff --git a/cli/main_test.go b/cli/main_test.go index e616c3169..f750d4a15 100644 --- a/cli/main_test.go +++ b/cli/main_test.go @@ -56,23 +56,19 @@ func TestDetermineCurrentContext(t *testing.T) { require.NoError(t, err) // If nothing set, fallback to default - c, err := determineCurrentContext("", "") - require.NoError(t, err) + c := determineCurrentContext("", "") require.Equal(t, "default", c) // If context flag set, use that - c, err = determineCurrentContext("other-context", "") - require.NoError(t, err) + c = determineCurrentContext("other-context", "") require.Equal(t, "other-context", c) // If no context flag, use config - c, err = determineCurrentContext("", d) - require.NoError(t, err) + c = determineCurrentContext("", d) require.Equal(t, "some-context", c) // Ensure context flag overrides config - c, err = determineCurrentContext("other-context", d) - require.NoError(t, err) + c = determineCurrentContext("other-context", d) require.Equal(t, "other-context", c) }