|
| 1 | +package upload |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/cli/cli/internal/ghrepo" |
| 12 | + "github.com/cli/cli/pkg/cmdutil" |
| 13 | + "github.com/cli/cli/pkg/iostreams" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +type UploadOptions struct { |
| 18 | + HttpClient func() (*http.Client, error) |
| 19 | + IO *iostreams.IOStreams |
| 20 | + BaseRepo func() (ghrepo.Interface, error) |
| 21 | + |
| 22 | + TagName string |
| 23 | + Assets []*AssetForUpload |
| 24 | + |
| 25 | + // maximum number of simultaneous uploads |
| 26 | + Concurrency int |
| 27 | + OverwriteExisting bool |
| 28 | +} |
| 29 | + |
| 30 | +type AssetForUpload struct { |
| 31 | + Name string |
| 32 | + Label string |
| 33 | + |
| 34 | + Data io.ReadCloser |
| 35 | + Size int64 |
| 36 | + MIMEType string |
| 37 | + |
| 38 | + ExistingURL string |
| 39 | +} |
| 40 | + |
| 41 | +func NewCmdUpload(f *cmdutil.Factory, runF func(*UploadOptions) error) *cobra.Command { |
| 42 | + opts := &UploadOptions{ |
| 43 | + IO: f.IOStreams, |
| 44 | + HttpClient: f.HttpClient, |
| 45 | + } |
| 46 | + |
| 47 | + cmd := &cobra.Command{ |
| 48 | + Use: "upload <tag> <files>...", |
| 49 | + Short: "Upload assets to a release", |
| 50 | + Args: cobra.MinimumNArgs(2), |
| 51 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 52 | + // support `-R, --repo` override |
| 53 | + opts.BaseRepo = f.BaseRepo |
| 54 | + |
| 55 | + opts.TagName = args[0] |
| 56 | + |
| 57 | + var err error |
| 58 | + opts.Assets, err = assetsFromArgs(args[1:]) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + opts.Concurrency = 5 |
| 64 | + |
| 65 | + if runF != nil { |
| 66 | + return runF(opts) |
| 67 | + } |
| 68 | + return uploadRun(opts) |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + cmd.Flags().BoolVar(&opts.OverwriteExisting, "clobber", false, "Overwrite existing assets of the same name") |
| 73 | + |
| 74 | + return cmd |
| 75 | +} |
| 76 | + |
| 77 | +func assetsFromArgs(args []string) (assets []*AssetForUpload, err error) { |
| 78 | + for _, fn := range args { |
| 79 | + var f *os.File |
| 80 | + var fi os.FileInfo |
| 81 | + |
| 82 | + f, err = os.Open(fn) |
| 83 | + if err != nil { |
| 84 | + return |
| 85 | + } |
| 86 | + fi, err = f.Stat() |
| 87 | + if err != nil { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + assets = append(assets, &AssetForUpload{ |
| 92 | + Data: f, |
| 93 | + Size: fi.Size(), |
| 94 | + Name: filepath.Base(fn), |
| 95 | + Label: "", |
| 96 | + // TODO: infer content type from file extension |
| 97 | + MIMEType: "application/octet-stream", |
| 98 | + }) |
| 99 | + } |
| 100 | + return |
| 101 | +} |
| 102 | + |
| 103 | +func uploadRun(opts *UploadOptions) error { |
| 104 | + httpClient, err := opts.HttpClient() |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + baseRepo, err := opts.BaseRepo() |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + release, err := fetchRelease(httpClient, baseRepo, opts.TagName) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + uploadURL := release.UploadURL |
| 120 | + if idx := strings.IndexRune(uploadURL, '{'); idx > 0 { |
| 121 | + uploadURL = uploadURL[:idx] |
| 122 | + } |
| 123 | + |
| 124 | + var existingNames []string |
| 125 | + for _, a := range opts.Assets { |
| 126 | + for _, ea := range release.Assets { |
| 127 | + if ea.Name == a.Name { |
| 128 | + a.ExistingURL = ea.URL |
| 129 | + existingNames = append(existingNames, ea.Name) |
| 130 | + break |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if len(existingNames) > 0 && !opts.OverwriteExisting { |
| 136 | + return fmt.Errorf("asset under the same name already exists: %v", existingNames) |
| 137 | + } |
| 138 | + |
| 139 | + opts.IO.StartProgressIndicator() |
| 140 | + err = concurrentUpload(httpClient, uploadURL, opts.Concurrency, opts.Assets) |
| 141 | + opts.IO.EndProgressIndicator() |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func concurrentUpload(httpClient *http.Client, uploadURL string, numWorkers int, assets []*AssetForUpload) error { |
| 150 | + jobs := make(chan AssetForUpload, len(assets)) |
| 151 | + results := make(chan error, len(assets)) |
| 152 | + |
| 153 | + for w := 1; w <= numWorkers; w++ { |
| 154 | + go func() { |
| 155 | + for a := range jobs { |
| 156 | + results <- uploadWithDelete(httpClient, uploadURL, a) |
| 157 | + } |
| 158 | + }() |
| 159 | + } |
| 160 | + |
| 161 | + for _, a := range assets { |
| 162 | + jobs <- *a |
| 163 | + } |
| 164 | + close(jobs) |
| 165 | + |
| 166 | + var uploadError error |
| 167 | + for i := 0; i < len(assets); i++ { |
| 168 | + if err := <-results; err != nil { |
| 169 | + uploadError = err |
| 170 | + } |
| 171 | + } |
| 172 | + return uploadError |
| 173 | +} |
| 174 | + |
| 175 | +func uploadWithDelete(httpClient *http.Client, uploadURL string, a AssetForUpload) error { |
| 176 | + if a.ExistingURL != "" { |
| 177 | + err := deleteAsset(httpClient, a.ExistingURL) |
| 178 | + if err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + defer a.Data.Close() |
| 184 | + _, err := uploadAsset(httpClient, uploadURL, a) |
| 185 | + return err |
| 186 | +} |
0 commit comments