-
Notifications
You must be signed in to change notification settings - Fork 87
fix databricks fs cp command #2901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |||||
"io/fs" | ||||||
"path" | ||||||
"path/filepath" | ||||||
"strings" | ||||||
|
||||||
"github.com/databricks/cli/cmd/root" | ||||||
"github.com/databricks/cli/libs/cmdio" | ||||||
|
@@ -60,6 +61,23 @@ func (c *copy) cpDirToDir(sourceDir, targetDir string) error { | |||||
} | ||||||
|
||||||
func (c *copy) cpFileToDir(sourcePath, targetDir string) error { | ||||||
// Remove trailing slash if present | ||||||
targetDir = strings.TrimSuffix(targetDir, "/") | ||||||
|
||||||
// Check if target directory exists | ||||||
_, err := c.targetFiler.Stat(c.ctx, targetDir) | ||||||
if err != nil { | ||||||
if errors.Is(err, fs.ErrNotExist) { | ||||||
// Create the directory if it doesn't exist | ||||||
err = c.targetFiler.Mkdir(c.ctx, targetDir) | ||||||
if err != nil { | ||||||
return fmt.Errorf("failed to create target directory %s: %w", targetDir, err) | ||||||
} | ||||||
} else { | ||||||
return err | ||||||
} | ||||||
} | ||||||
|
||||||
fileName := filepath.Base(sourcePath) | ||||||
targetPath := path.Join(targetDir, fileName) | ||||||
|
||||||
|
@@ -196,6 +214,11 @@ func newCpCommand() *cobra.Command { | |||||
return c.cpFileToDir(sourcePath, targetPath) | ||||||
} | ||||||
|
||||||
// Check if target path ends with a slash, indicating it should be treated as a directory | ||||||
if strings.HasSuffix(fullTargetPath, "/") || strings.HasSuffix(targetPath, "/") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Checking fullTargetPath for a trailing slash should be enough. However, this also needs to work on Windows. In that case I'm not sure whether having this change means a breaking change for customers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect using |
||||||
return c.cpFileToDir(sourcePath, targetPath) | ||||||
} | ||||||
|
||||||
// case 3: source path is a file, and target path is a file | ||||||
return c.cpFileToFile(sourcePath, targetPath) | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the path has a trailing / but does not exist, we should error instead to match the semantics of the linux
cp
command. Could you please update this PR to do so?