Merge pull request #387 from darrenstahlmsft/saveFastFail

Fast fail when save directory does not exist
This commit is contained in:
Sebastiaan van Stijn 2017-07-28 17:46:13 +01:00 committed by GitHub
commit eed14e840c
2 changed files with 21 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package image
import ( import (
"io" "io"
"os"
"path/filepath"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "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") 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) responseBody, err := dockerCli.Client().ImageSave(context.Background(), opts.images)
if err != nil { if err != nil {
return err return err
@ -54,3 +60,13 @@ func runSave(dockerCli command.Cli, opts saveOptions) error {
return command.CopyToFile(opts.output, responseBody) 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
}

View File

@ -43,6 +43,11 @@ func TestNewSaveCommandErrors(t *testing.T) {
return ioutil.NopCloser(strings.NewReader("")), errors.Errorf("error saving image") 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 { for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc}) cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc})