| 64 | } |
| 65 | |
| 66 | func get(cmd *cobra.Command, args []string) (err error) { |
| 67 | if len(args) == 0 || len(args) > 2 { |
| 68 | return invalidArgumentsErrorWithDetails("`get` requires `src` and/or `dst` arguments", argumentsErrorDetails("src", "dst")) |
| 69 | } |
| 70 | |
| 71 | src, err := validatePath(args[0]) |
| 72 | if err != nil { |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | dst := path.Base(src) |
| 77 | if len(args) == 2 { |
| 78 | dst = args[1] |
| 79 | } |
| 80 | |
| 81 | recursive, _ := cmd.Flags().GetBool("recursive") |
| 82 | opts := parseGetOptions(cmd) |
| 83 | |
| 84 | if dst == "-" { |
| 85 | if commandOutputFormat(cmd) == output.FormatJSON { |
| 86 | return invalidArgumentsErrorWithDetails("`get --output=json` cannot be used with stdout target `-`", mergeJSONErrorDetails(operationErrorDetails("download"), argumentErrorDetails("dst"), flagErrorDetails("output"))) |
| 87 | } |
| 88 | return getStdout(cmd, src, recursive) |
| 89 | } |
| 90 | |
| 91 | dbx := filesNewFunc(config) |
| 92 | |
| 93 | meta, err := dbx.GetMetadataContext(currentContext(), files.NewGetMetadataArg(src)) |
| 94 | if err != nil { |
| 95 | if recursive { |
| 96 | return withJSONErrorDetails(fmt.Errorf("get metadata for %s: %v", src, err), operationErrorDetails("download"), pathErrorDetails(src)) |
| 97 | } |
| 98 | // For non-recursive, fall through to download (will fail with proper error) |
| 99 | if f, statErr := os.Stat(dst); statErr == nil && f.IsDir() { |
| 100 | dst = filepath.Join(dst, path.Base(src)) |
| 101 | } |
| 102 | result, err := downloadFileWithResult(dbx, src, dst, opts) |
| 103 | if err != nil { |
| 104 | return withJSONErrorDetails(err, operationErrorDetails("download"), pathErrorDetails(src), relocationErrorDetails(src, dst)) |
| 105 | } |
| 106 | return renderGetResults(cmd, getCommandInput{ |
| 107 | Source: src, |
| 108 | Target: dst, |
| 109 | Recursive: false, |
| 110 | Stdout: false, |
| 111 | }, []getResult{result}) |
| 112 | } |
| 113 | |
| 114 | if _, ok := meta.(*files.FolderMetadata); ok { |
| 115 | if !recursive { |
| 116 | return invalidArgumentsErrorfWithDetails("%s is a folder (use --recursive to download folders)", mergeJSONErrorDetails(operationErrorDetails("download"), pathErrorDetails(src)), src) |
| 117 | } |
| 118 | if f, statErr := os.Stat(dst); statErr == nil && f.IsDir() { |
| 119 | dst = filepath.Join(dst, path.Base(src)) |
| 120 | } |
| 121 | if commandOutputFormat(cmd) == output.FormatText { |
| 122 | return withJSONErrorDetails(getRecursive(dbx, src, dst), operationErrorDetails("download"), pathErrorDetails(src), relocationErrorDetails(src, dst)) |
| 123 | } |