From 27bee792a05723e9473261f6aa5202c432501089 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 11 May 2026 10:10:03 +0200 Subject: [PATCH] decorate --env-file, --label-file errors Before: docker run --rm --env-file=./no-such-file alpine docker: open ./no-such-file: no such file or directory Run 'docker run --help' for more information After: docker run --rm --env-file=./no-such-file alpine docker: --env-file: open ./no-such-file: no such file or directory Run 'docker run --help' for more information Signed-off-by: Sebastiaan van Stijn --- cli/command/container/opts.go | 4 ++-- cli/command/container/opts_test.go | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index f9d896882ebf..905674627dbb 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -506,13 +506,13 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con // collect all the environment variables for the container envVariables, err := opts.ReadKVEnvStrings(copts.envFile.GetSlice(), copts.env.GetSlice()) if err != nil { - return nil, err + return nil, fmt.Errorf("--env-file: %w", err) } // collect all the labels for the container labels, err := opts.ReadKVStrings(copts.labelsFile.GetSlice(), copts.labels.GetSlice()) if err != nil { - return nil, err + return nil, fmt.Errorf("--label-file: %w", err) } pidMode := container.PidMode(copts.pidMode) diff --git a/cli/command/container/opts_test.go b/cli/command/container/opts_test.go index 51a0e72b4943..394cf1f467bb 100644 --- a/cli/command/container/opts_test.go +++ b/cli/command/container/opts_test.go @@ -937,13 +937,13 @@ func TestParseLoggingOpts(t *testing.T) { } func TestParseEnvfileVariables(t *testing.T) { - e := "open nonexistent: no such file or directory" + expErr := "--env-file: open nonexistent: no such file or directory" if runtime.GOOS == "windows" { - e = "open nonexistent: The system cannot find the file specified." + expErr = "--env-file: open nonexistent: The system cannot find the file specified." } // env ko - if _, _, _, err := parseRun([]string{"--env-file=nonexistent", "img", "cmd"}); err == nil || err.Error() != e { - t.Fatalf("Expected an error with message '%s', got %v", e, err) + if _, _, _, err := parseRun([]string{"--env-file=nonexistent", "img", "cmd"}); err == nil || err.Error() != expErr { + t.Fatalf("Expected an error with message '%s', got %v", expErr, err) } // env ok config, _, _, err := parseRun([]string{"--env-file=testdata/valid.env", "img", "cmd"}) @@ -990,13 +990,13 @@ func TestParseEnvfileVariablesWithBOMUnicode(t *testing.T) { } func TestParseLabelfileVariables(t *testing.T) { - e := "open nonexistent: no such file or directory" + expErr := "--label-file: open nonexistent: no such file or directory" if runtime.GOOS == "windows" { - e = "open nonexistent: The system cannot find the file specified." + expErr = "--label-file: open nonexistent: The system cannot find the file specified." } // label ko - if _, _, _, err := parseRun([]string{"--label-file=nonexistent", "img", "cmd"}); err == nil || err.Error() != e { - t.Fatalf("Expected an error with message '%s', got %v", e, err) + if _, _, _, err := parseRun([]string{"--label-file=nonexistent", "img", "cmd"}); err == nil || err.Error() != expErr { + t.Fatalf("Expected an error with message '%s', got %v", expErr, err) } // label ok config, _, _, err := parseRun([]string{"--label-file=testdata/valid.label", "img", "cmd"})