Răsfoiți Sursa

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

Guillaume Tardif 5 ani în urmă
părinte
comite
98f7a8e1aa
2 a modificat fișierele cu 9 adăugiri și 15 ștergeri
  1. 5 7
      cli/main.go
  2. 4 8
      cli/main_test.go

+ 5 - 7
cli/main.go

@@ -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)
-	if err != nil {
-		fatal(errors.Wrap(err, "unable to determine current context"))
-	}
+	currentContext := determineCurrentContext(opts.Context, configDir)
 
 
 	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) {

+ 4 - 8
cli/main_test.go

@@ -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("", "")
-	require.NoError(t, err)
+	c := determineCurrentContext("", "")
 	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", "")
-	require.NoError(t, err)
+	c = determineCurrentContext("other-context", "")
 	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)
-	require.NoError(t, err)
+	c = determineCurrentContext("", d)
 	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)
-	require.NoError(t, err)
+	c = determineCurrentContext("other-context", d)
 	require.Equal(t, "other-context", c)
 	require.Equal(t, "other-context", c)
 }
 }