| 284 | } |
| 285 | |
| 286 | func put(cmd *cobra.Command, args []string) (err error) { |
| 287 | if len(args) == 0 || len(args) > 2 { |
| 288 | return invalidArgumentsErrorWithDetails("`put` requires `src` and/or `dst` arguments", argumentsErrorDetails("src", "dst")) |
| 289 | } |
| 290 | |
| 291 | opts, err := parsePutOptions(cmd) |
| 292 | if err != nil { |
| 293 | return err |
| 294 | } |
| 295 | |
| 296 | recursive, _ := cmd.Flags().GetBool("recursive") |
| 297 | |
| 298 | src := args[0] |
| 299 | |
| 300 | if src == "-" { |
| 301 | return putStdin(cmd, args, opts, recursive) |
| 302 | } |
| 303 | |
| 304 | srcInfo, err := os.Stat(src) |
| 305 | if err != nil { |
| 306 | return withJSONErrorDetails(err, operationErrorDetails("upload"), pathErrorDetails(src)) |
| 307 | } |
| 308 | |
| 309 | if srcInfo.IsDir() && !recursive { |
| 310 | return invalidArgumentsErrorfWithDetails("%s is a directory (use --recursive to upload directories)", mergeJSONErrorDetails(operationErrorDetails("upload"), pathErrorDetails(src)), src) |
| 311 | } |
| 312 | |
| 313 | // Default `dst` to the base segment of the source path; use the second argument if provided. |
| 314 | dst := "/" + filepath.Base(src) |
| 315 | dstIsDir := false |
| 316 | if len(args) == 2 { |
| 317 | dstIsDir = strings.HasSuffix(args[1], "/") |
| 318 | dst, err = validatePath(args[1]) |
| 319 | if err != nil { |
| 320 | return |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if !srcInfo.IsDir() { |
| 325 | dst = resolveDestination(filesNewFunc(config), src, dst, dstIsDir) |
| 326 | } |
| 327 | |
| 328 | if srcInfo.IsDir() { |
| 329 | if commandOutputFormat(cmd) == output.FormatText { |
| 330 | return withJSONErrorDetails(putRecursive(src, dst, opts), operationErrorDetails("upload"), relocationErrorDetails(src, dst)) |
| 331 | } |
| 332 | results, warnings, err := putRecursiveWithResults(src, dst, opts) |
| 333 | if err != nil { |
| 334 | return withJSONErrorDetails(err, operationErrorDetails("upload"), relocationErrorDetails(src, dst)) |
| 335 | } |
| 336 | return renderPutResultsWithWarnings(cmd, putCommandInput{ |
| 337 | Source: src, |
| 338 | Target: dst, |
| 339 | Recursive: true, |
| 340 | IfExists: opts.ifExists, |
| 341 | Stdin: false, |
| 342 | }, results, warnings) |
| 343 | } |