Skip to content

Commit 02757a3

Browse files
authored
chore(ux): improve oras backup error message (#1833)
Signed-off-by: Lixia (Sylvia) Lei <lixlei@microsoft.com>
1 parent 97cb6f7 commit 02757a3

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

cmd/oras/root/backup.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ func runBackup(cmd *cobra.Command, opts *backupOptions) error {
167167
// test if the output file can be created and fail early if there is an issue
168168
fp, err := os.OpenFile(opts.output, os.O_CREATE|os.O_WRONLY, 0666)
169169
if err != nil {
170+
if fi, statErr := os.Stat(opts.output); statErr == nil && fi.IsDir() {
171+
return &oerrors.Error{
172+
Err: fmt.Errorf("the output path %q already exists and is a directory", opts.output),
173+
Recommendation: "To back up to a tar archive, please specify a different output file name or remove the existing directory.",
174+
}
175+
}
170176
return fmt.Errorf("unable to create output file %s: %w", opts.output, err)
171177
}
172178
if err := fp.Close(); err != nil {

test/e2e/suite/command/backup.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,26 @@ var _ = Describe("ORAS users:", func() {
490490
ExpectFailure().MatchErrKeyWords("Error:").Exec()
491491
})
492492

493+
It("should fail with appropriate error when output path is a directory but tar file expected", func() {
494+
// Create a directory that will conflict with the tar output path
495+
tmpDir := GinkgoT().TempDir()
496+
tarDirPath := filepath.Join(tmpDir, "backup-dir-conflict.tar")
497+
498+
// Create a directory with the .tar extension
499+
err := os.MkdirAll(tarDirPath, 0755)
500+
Expect(err).ToNot(HaveOccurred())
501+
defer func() {
502+
_ = os.RemoveAll(tarDirPath)
503+
}()
504+
505+
// Try to use the directory as a tar output file
506+
ORAS("backup", "--output", tarDirPath, RegistryRef(ZOTHost, ImageRepo, foobar.Tag)).
507+
ExpectFailure().
508+
MatchErrKeyWords("Error:", "already exists and is a directory").
509+
MatchErrKeyWords("To back up to a tar archive, please specify a different output file name or remove the existing directory.").
510+
Exec()
511+
})
512+
493513
It("should fail when the repository doesn't exist", func() {
494514
tmpDir := GinkgoT().TempDir()
495515
outDir := filepath.Join(tmpDir, "backup-nonexistent-repo")

0 commit comments

Comments
 (0)