From 18c877d35cd1b70a796184211aa8ae030259bf19 Mon Sep 17 00:00:00 2001 From: Darren Stahl Date: Wed, 26 Jul 2017 12:11:02 -0700 Subject: [PATCH] Fast fail when save directory does not exist Signed-off-by: Darren Stahl --- cli/command/image/save.go | 16 ++++++++++++++++ cli/command/image/save_test.go | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/cli/command/image/save.go b/cli/command/image/save.go index ba666d2740..9cae97b370 100644 --- a/cli/command/image/save.go +++ b/cli/command/image/save.go @@ -2,6 +2,8 @@ package image import ( "io" + "os" + "path/filepath" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" @@ -41,6 +43,10 @@ func runSave(dockerCli command.Cli, opts saveOptions) error { return errors.New("cowardly refusing to save to a terminal. Use the -o flag or redirect") } + if err := validateOutputPath(opts.output); err != nil { + return errors.Wrap(err, "failed to save image") + } + responseBody, err := dockerCli.Client().ImageSave(context.Background(), opts.images) if err != nil { return err @@ -54,3 +60,13 @@ func runSave(dockerCli command.Cli, opts saveOptions) error { return command.CopyToFile(opts.output, responseBody) } + +func validateOutputPath(path string) error { + dir := filepath.Dir(path) + if dir != "" && dir != "." { + if _, err := os.Stat(dir); os.IsNotExist(err) { + return errors.Errorf("unable to validate output path: directory %q does not exist", dir) + } + } + return nil +} diff --git a/cli/command/image/save_test.go b/cli/command/image/save_test.go index c63eeef41c..1cac4c5749 100644 --- a/cli/command/image/save_test.go +++ b/cli/command/image/save_test.go @@ -43,6 +43,11 @@ func TestNewSaveCommandErrors(t *testing.T) { return ioutil.NopCloser(strings.NewReader("")), errors.Errorf("error saving image") }, }, + { + name: "output directory does not exist", + args: []string{"-o", "fakedir/out.tar", "arg1"}, + expectedError: "failed to save image: unable to validate output path: directory \"fakedir\" does not exist", + }, } for _, tc := range testCases { cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc})