Browse Source

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 năm trước cách đây
mục cha
commit
98f7a8e1aa
2 tập tin đã thay đổi với 9 bổ sung15 xóa
  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
 	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) {

+ 4 - 8
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)
 }