Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions cmd/fs/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
)

type copy struct {
ctx context.Context
sourceFiler filer.Filer
targetFiler filer.Filer
ctx context.Context
sourceFiler filer.Filer
targetFiler filer.Filer
sourceScheme string
targetScheme string
}

func (c *copy) cpWriteCallback(sourceDir, targetDir string) fs.WalkDirFunc {
Expand Down Expand Up @@ -78,29 +80,47 @@ func (c *copy) cpFileToFile(sourcePath, targetPath string) error {
err = c.targetFiler.Write(c.ctx, targetPath, r)
// skip if file already exists
if err != nil && errors.Is(err, fs.ErrExist) {
return emitCpFileSkippedEvent(c.ctx, sourcePath, targetPath)
return c.emitFileSkippedEvent(sourcePath, targetPath)
}
if err != nil {
return err
}
}
return emitCpFileCopiedEvent(c.ctx, sourcePath, targetPath)
return c.emitFileCopiedEvent(sourcePath, targetPath)
}

// TODO: emit these events on stderr
// TODO: add integration tests for these events
func emitCpFileSkippedEvent(ctx context.Context, sourcePath, targetPath string) error {
event := newFileSkippedEvent(sourcePath, targetPath)
func (c *copy) emitFileSkippedEvent(sourcePath, targetPath string) error {
fullSourcePath := sourcePath
if c.sourceScheme != "" {
fullSourcePath = path.Join(c.sourceScheme+":", sourcePath)
}
fullTargetPath := targetPath
if c.targetScheme != "" {
fullTargetPath = path.Join(c.targetScheme+":", targetPath)
}

event := newFileSkippedEvent(fullSourcePath, fullTargetPath)
template := "{{.SourcePath}} -> {{.TargetPath}} (skipped; already exists)\n"

return cmdio.RenderWithTemplate(ctx, event, template)
return cmdio.RenderWithTemplate(c.ctx, event, template)
}

func emitCpFileCopiedEvent(ctx context.Context, sourcePath, targetPath string) error {
event := newFileCopiedEvent(sourcePath, targetPath)
func (c *copy) emitFileCopiedEvent(sourcePath, targetPath string) error {
fullSourcePath := sourcePath
if c.sourceScheme != "" {
fullSourcePath = path.Join(c.sourceScheme+":", sourcePath)
}
fullTargetPath := targetPath
if c.targetScheme != "" {
fullTargetPath = path.Join(c.targetScheme+":", targetPath)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplication of this block is a bit of a smell.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, Not sure how to refactor it better


event := newFileCopiedEvent(fullSourcePath, fullTargetPath)
template := "{{.SourcePath}} -> {{.TargetPath}}\n"

return cmdio.RenderWithTemplate(ctx, event, template)
return cmdio.RenderWithTemplate(c.ctx, event, template)
}

var cpOverwrite bool
Expand Down Expand Up @@ -144,10 +164,21 @@ var cpCmd = &cobra.Command{
return err
}

sourceScheme := ""
if isDbfsPath(fullSourcePath) {
sourceScheme = "dbfs"
}
targetScheme := ""
if isDbfsPath(fullTargetPath) {
targetScheme = "dbfs"
}

c := copy{
ctx: ctx,
sourceFiler: sourceFiler,
targetFiler: targetFiler,
ctx: ctx,
sourceFiler: sourceFiler,
targetFiler: targetFiler,
sourceScheme: sourceScheme,
targetScheme: targetScheme,
}

// Get information about file at source path
Expand Down
4 changes: 4 additions & 0 deletions cmd/fs/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ func filerForPath(ctx context.Context, fullPath string) (filer.Filer, string, er
f, err := filer.NewDbfsClient(w, "/")
return f, path, err
}

func isDbfsPath(path string) bool {
return strings.HasPrefix(path, "dbfs:/")
}