mirror of https://github.com/docker/cli.git
Fast fail when save directory does not exist
Signed-off-by: Darren Stahl <darst@microsoft.com>
This commit is contained in:
parent
2dac00bdca
commit
18c877d35c
|
@ -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
|
||||||
|
}
|
||||||
|
|
|
@ -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})
|
||||||
|
|
Loading…
Reference in New Issue