|
| 1 | +package artifacts |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "runtime" |
| 7 | + |
| 8 | + "github.com/rwx-cloud/cli/internal/cli" |
| 9 | + "github.com/rwx-cloud/cli/internal/errors" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + downloadOutputDir string |
| 16 | + downloadOutputFile string |
| 17 | + downloadJSON bool |
| 18 | + downloadAutoExtract bool |
| 19 | + downloadOpen bool |
| 20 | + |
| 21 | + DownloadCmd *cobra.Command |
| 22 | +) |
| 23 | + |
| 24 | +func InitDownload(requireAccessToken func() error, getService func() cli.Service) { |
| 25 | + DownloadCmd = &cobra.Command{ |
| 26 | + Args: cobra.ExactArgs(2), |
| 27 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + return requireAccessToken() |
| 29 | + }, |
| 30 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 31 | + taskID := args[0] |
| 32 | + artifactKey := args[1] |
| 33 | + |
| 34 | + outputDirSet := cmd.Flags().Changed("output-dir") |
| 35 | + outputFileSet := cmd.Flags().Changed("output-file") |
| 36 | + if outputDirSet && outputFileSet { |
| 37 | + return errors.New("output-dir and output-file cannot be used together") |
| 38 | + } |
| 39 | + |
| 40 | + var absOutputDir string |
| 41 | + var absOutputFile string |
| 42 | + var err error |
| 43 | + |
| 44 | + if downloadOutputFile != "" { |
| 45 | + absOutputFile, err = filepath.Abs(downloadOutputFile) |
| 46 | + if err != nil { |
| 47 | + return errors.Wrapf(err, "unable to resolve absolute path for %s", downloadOutputFile) |
| 48 | + } |
| 49 | + } else { |
| 50 | + outputDir := downloadOutputDir |
| 51 | + if !outputDirSet { |
| 52 | + outputDir, err = getDefaultDownloadsDir() |
| 53 | + if err != nil { |
| 54 | + return errors.Wrap(err, "unable to determine default downloads directory") |
| 55 | + } |
| 56 | + } |
| 57 | + absOutputDir, err = filepath.Abs(outputDir) |
| 58 | + if err != nil { |
| 59 | + return errors.Wrapf(err, "unable to resolve absolute path for %s", outputDir) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + err = getService().DownloadArtifact(cli.DownloadArtifactConfig{ |
| 64 | + TaskID: taskID, |
| 65 | + ArtifactKey: artifactKey, |
| 66 | + OutputDir: absOutputDir, |
| 67 | + OutputFile: absOutputFile, |
| 68 | + OutputDirExplicitlySet: outputDirSet, |
| 69 | + Json: downloadJSON, |
| 70 | + AutoExtract: downloadAutoExtract, |
| 71 | + Open: downloadOpen, |
| 72 | + }) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | + }, |
| 79 | + Short: "Download an artifact from a task", |
| 80 | + Use: "download <task-id> <artifact-key> [flags]", |
| 81 | + } |
| 82 | + |
| 83 | + DownloadCmd.Flags().StringVar(&downloadOutputDir, "output-dir", "", "output directory for the downloaded artifact (defaults to Downloads folder)") |
| 84 | + DownloadCmd.Flags().StringVar(&downloadOutputFile, "output-file", "", "output file path for the downloaded artifact") |
| 85 | + DownloadCmd.MarkFlagsMutuallyExclusive("output-dir", "output-file") |
| 86 | + DownloadCmd.Flags().BoolVar(&downloadJSON, "json", false, "output file locations as JSON") |
| 87 | + DownloadCmd.Flags().BoolVar(&downloadAutoExtract, "auto-extract", false, "automatically extract directory tar archives") |
| 88 | + DownloadCmd.Flags().BoolVar(&downloadOpen, "open", false, "automatically open the downloaded file(s)") |
| 89 | +} |
| 90 | + |
| 91 | +func getDefaultDownloadsDir() (string, error) { |
| 92 | + if runtime.GOOS == "linux" { |
| 93 | + if xdgDownload := os.Getenv("XDG_DOWNLOAD_DIR"); xdgDownload != "" { |
| 94 | + return xdgDownload, nil |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + homeDir, err := os.UserHomeDir() |
| 99 | + if err != nil { |
| 100 | + return "", errors.Wrap(err, "unable to determine user home directory") |
| 101 | + } |
| 102 | + |
| 103 | + return filepath.Join(homeDir, "Downloads"), nil |
| 104 | +} |
0 commit comments