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
This commit is contained in:
Guillaume Tardif 2020-06-15 10:04:01 +02:00
parent bc1b2b778a
commit 98f7a8e1aa
2 changed files with 9 additions and 15 deletions

View File

@ -148,10 +148,7 @@ func main() {
configDir := opts.Config configDir := opts.Config
ctx = config.WithDir(ctx, configDir) ctx = config.WithDir(ctx, configDir)
currentContext, err := determineCurrentContext(opts.Context, configDir) currentContext := determineCurrentContext(opts.Context, configDir)
if err != nil {
fatal(errors.Wrap(err, "unable to determine current context"))
}
s, err := store.New(store.WithRoot(configDir)) s, err := store.New(store.WithRoot(configDir))
if err != nil { if err != nil {
@ -200,19 +197,20 @@ func newSigContext() (context.Context, func()) {
return ctx, cancel return ctx, cancel
} }
func determineCurrentContext(flag string, configDir string) (string, error) { func determineCurrentContext(flag string, configDir string) string {
res := flag res := flag
if res == "" { if res == "" {
config, err := config.LoadFile(configDir) config, err := config.LoadFile(configDir)
if err != nil { if err != nil {
return "", err fmt.Fprintln(os.Stderr, errors.Wrap(err, "WARNING"))
return "default"
} }
res = config.CurrentContext res = config.CurrentContext
} }
if res == "" { if res == "" {
res = "default" res = "default"
} }
return res, nil return res
} }
func fatal(err error) { func fatal(err error) {

View File

@ -56,23 +56,19 @@ func TestDetermineCurrentContext(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// If nothing set, fallback to default // If nothing set, fallback to default
c, err := determineCurrentContext("", "") c := determineCurrentContext("", "")
require.NoError(t, err)
require.Equal(t, "default", c) require.Equal(t, "default", c)
// If context flag set, use that // If context flag set, use that
c, err = determineCurrentContext("other-context", "") c = determineCurrentContext("other-context", "")
require.NoError(t, err)
require.Equal(t, "other-context", c) require.Equal(t, "other-context", c)
// If no context flag, use config // If no context flag, use config
c, err = determineCurrentContext("", d) c = determineCurrentContext("", d)
require.NoError(t, err)
require.Equal(t, "some-context", c) require.Equal(t, "some-context", c)
// Ensure context flag overrides config // Ensure context flag overrides config
c, err = determineCurrentContext("other-context", d) c = determineCurrentContext("other-context", d)
require.NoError(t, err)
require.Equal(t, "other-context", c) require.Equal(t, "other-context", c)
} }