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
116 changes: 1 addition & 115 deletions cmd/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,125 +15,11 @@
package cmd

import (
"fmt"
"io"
"strings"

"github.com/dropbox/dbxcli/v3/internal/output"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/files"
"github.com/spf13/cobra"
)

func cp(cmd *cobra.Command, args []string) error {
var destination string
var argsToCopy []string

if len(args) > 2 {
destination = args[len(args)-1]
argsToCopy = args[0 : len(args)-1]
} else if len(args) == 2 {
destination = args[1]
argsToCopy = append(argsToCopy, args[0])
} else {
return invalidArgumentsErrorWithDetails("cp requires a source and a destination", argumentsErrorDetails("source", "destination"))
}

opts, err := parseRelocationOptions(cmd)
if err != nil {
return err
}

var cpErrors []error
var cpErrorDetails []map[string]any
var relocationArgs []*files.RelocationArg
var plannedResults []relocationResult
var results []jsonOperationResult
collectResults := commandOutputFormat(cmd) == output.FormatJSON

dbx := filesNewFunc(config)
destIsFolder := len(argsToCopy) > 1 || strings.HasSuffix(destination, "/") || isRemoteFolder(dbx, destination)

for _, argument := range argsToCopy {
dst := relocationDestination(argument, destination, destIsFolder)
arg, err := makeRelocationArg(argument, dst)
if err != nil {
relocationError := fmt.Errorf("Error validating copy for %s to %s: %v", argument, dst, err)
cpErrors = append(cpErrors, relocationError)
cpErrorDetails = append(cpErrorDetails, relocationFailureDetails(argument, dst))
} else {
arg.Autorename = opts.ifExists == relocationIfExistsAutorename
if opts.dryRun {
result, err := plannedRelocationResult(dbx, arg)
if err != nil {
cpErrors = append(cpErrors, fmt.Errorf("copy %q to %q: %v", arg.FromPath, arg.ToPath, err))
cpErrorDetails = append(cpErrorDetails, relocationFailureDetails(arg.FromPath, arg.ToPath))
continue
}
plannedResults = append(plannedResults, result)
if collectResults {
results = append(results, relocationOperationResult(relocationJSONStatusCopied, result))
}
continue
}
result, skipped, err := relocationSkipIfDestinationExists(dbx, arg, opts)
if err != nil {
cpErrors = append(cpErrors, fmt.Errorf("copy %q to %q: %v", arg.FromPath, arg.ToPath, err))
cpErrorDetails = append(cpErrorDetails, relocationFailureDetails(arg.FromPath, arg.ToPath))
continue
}
if skipped {
if collectResults {
results = append(results, relocationOperationResult(relocationJSONStatusSkipped, result))
}
continue
}
relocationArgs = append(relocationArgs, arg)
}
}

for _, arg := range relocationArgs {
res, err := dbx.CopyV2Context(currentContext(), arg)
if err != nil {
if result, skipped := relocationSkipAfterDestinationConflict(dbx, arg, err, opts); skipped {
if collectResults {
results = append(results, relocationOperationResult(relocationJSONStatusSkipped, result))
}
continue
}
copyError := fmt.Errorf("copy %q to %q: %v", arg.FromPath, arg.ToPath, err)
cpErrors = append(cpErrors, copyError)
cpErrorDetails = append(cpErrorDetails, relocationFailureDetails(arg.FromPath, arg.ToPath))
continue
}
if collectResults {
result, err := newRelocationResult(arg, res)
if err != nil {
copyError := fmt.Errorf("copy %q to %q: %v", arg.FromPath, arg.ToPath, err)
cpErrors = append(cpErrors, copyError)
cpErrorDetails = append(cpErrorDetails, relocationFailureDetails(arg.FromPath, arg.ToPath))
continue
}
results = append(results, relocationOperationResult(relocationSuccessStatus(relocationJSONStatusCopied, arg, result, opts), result))
}
}

if len(cpErrors) > 0 {
for _, cpError := range cpErrors {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%v\n", cpError)
}
return relocationAggregateError("cp", "copy", len(cpErrors), cpErrorDetails)
}

if opts.dryRun {
return commandOutput(cmd).Render(func(w io.Writer) error {
return renderPlannedRelocationResults(w, "copy", plannedResults)
}, newJSONCommandOperationOutput(cmd, nil, results, nil))
}

if !collectResults {
return nil
}
return renderJSONOperationOutput(cmd, nil, results)
return runRelocation(cmd, args, copySpec)
}

// cpCmd represents the cp command
Expand Down
115 changes: 1 addition & 114 deletions cmd/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,124 +15,11 @@
package cmd

import (
"fmt"
"io"
"strings"

"github.com/dropbox/dbxcli/v3/internal/output"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/files"
"github.com/spf13/cobra"
)

func mv(cmd *cobra.Command, args []string) error {
var destination string
var argsToMove []string

if len(args) > 2 {
destination = args[len(args)-1]
argsToMove = args[0 : len(args)-1]
} else if len(args) == 2 {
destination = args[1]
argsToMove = append(argsToMove, args[0])
} else {
return invalidArgumentsErrorWithDetails("mv command requires a source and a destination", argumentsErrorDetails("source", "destination"))
}

opts, err := parseRelocationOptions(cmd)
if err != nil {
return err
}

var mvErrors []error
var mvErrorDetails []map[string]any
var relocationArgs []*files.RelocationArg
var plannedResults []relocationResult
var results []jsonOperationResult
collectResults := commandOutputFormat(cmd) == output.FormatJSON

dbx := filesNewFunc(config)
destIsFolder := len(argsToMove) > 1 || strings.HasSuffix(destination, "/") || isRemoteFolder(dbx, destination)

for _, argument := range argsToMove {
dst := relocationDestination(argument, destination, destIsFolder)
arg, err := makeRelocationArg(argument, dst)
if err != nil {
mvErrors = append(mvErrors, fmt.Errorf("Error validating move for %s to %s: %v", argument, dst, err))
mvErrorDetails = append(mvErrorDetails, relocationFailureDetails(argument, dst))
} else {
arg.Autorename = opts.ifExists == relocationIfExistsAutorename
if opts.dryRun {
result, err := plannedRelocationResult(dbx, arg)
if err != nil {
mvErrors = append(mvErrors, fmt.Errorf("move %q to %q: %v", arg.FromPath, arg.ToPath, err))
mvErrorDetails = append(mvErrorDetails, relocationFailureDetails(arg.FromPath, arg.ToPath))
continue
}
plannedResults = append(plannedResults, result)
if collectResults {
results = append(results, relocationOperationResult(relocationJSONStatusMoved, result))
}
continue
}
result, skipped, err := relocationSkipIfDestinationExists(dbx, arg, opts)
if err != nil {
mvErrors = append(mvErrors, fmt.Errorf("move %q to %q: %v", arg.FromPath, arg.ToPath, err))
mvErrorDetails = append(mvErrorDetails, relocationFailureDetails(arg.FromPath, arg.ToPath))
continue
}
if skipped {
if collectResults {
results = append(results, relocationOperationResult(relocationJSONStatusSkipped, result))
}
continue
}
relocationArgs = append(relocationArgs, arg)
}
}

for _, arg := range relocationArgs {
res, err := dbx.MoveV2Context(currentContext(), arg)
if err != nil {
if result, skipped := relocationSkipAfterDestinationConflict(dbx, arg, err, opts); skipped {
if collectResults {
results = append(results, relocationOperationResult(relocationJSONStatusSkipped, result))
}
continue
}
moveError := fmt.Errorf("move %q to %q: %v", arg.FromPath, arg.ToPath, err)
mvErrors = append(mvErrors, moveError)
mvErrorDetails = append(mvErrorDetails, relocationFailureDetails(arg.FromPath, arg.ToPath))
continue
}
if collectResults {
result, err := newRelocationResult(arg, res)
if err != nil {
moveError := fmt.Errorf("move %q to %q: %v", arg.FromPath, arg.ToPath, err)
mvErrors = append(mvErrors, moveError)
mvErrorDetails = append(mvErrorDetails, relocationFailureDetails(arg.FromPath, arg.ToPath))
continue
}
results = append(results, relocationOperationResult(relocationSuccessStatus(relocationJSONStatusMoved, arg, result, opts), result))
}
}

if len(mvErrors) > 0 {
for _, mvError := range mvErrors {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%v\n", mvError)
}
return relocationAggregateError("mv", "move", len(mvErrors), mvErrorDetails)
}

if opts.dryRun {
return commandOutput(cmd).Render(func(w io.Writer) error {
return renderPlannedRelocationResults(w, "move", plannedResults)
}, newJSONCommandOperationOutput(cmd, nil, results, nil))
}

if !collectResults {
return nil
}
return renderJSONOperationOutput(cmd, nil, results)
return runRelocation(cmd, args, moveSpec)
}

// mvCmd represents the mv command
Expand Down
Loading
Loading