| 91 | } |
| 92 | |
| 93 | func (rc *releaseClient) uploadFiles(id int64, files []string) error { |
| 94 | assets, _, err := rc.Client.Repositories.ListReleaseAssets(rc.Context, rc.Owner, rc.Repo, id, &github.ListOptions{}) |
| 95 | |
| 96 | if err != nil { |
| 97 | return fmt.Errorf("Failed to fetch existing assets: %s", err) |
| 98 | } |
| 99 | |
| 100 | var uploadFiles []string |
| 101 | |
| 102 | files: |
| 103 | for _, file := range files { |
| 104 | for _, asset := range assets { |
| 105 | if *asset.Name == path.Base(file) { |
| 106 | switch rc.FileExists { |
| 107 | case "overwrite": |
| 108 | // do nothing |
| 109 | case "fail": |
| 110 | return fmt.Errorf("Asset file %s already exists", path.Base(file)) |
| 111 | case "skip": |
| 112 | fmt.Printf("Skipping pre-existing %s artifact\n", *asset.Name) |
| 113 | continue files |
| 114 | default: |
| 115 | return fmt.Errorf("Internal error, unkown file_exist value %s", rc.FileExists) |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | uploadFiles = append(uploadFiles, file) |
| 121 | } |
| 122 | |
| 123 | for _, file := range uploadFiles { |
| 124 | handle, err := os.Open(file) |
| 125 | |
| 126 | if err != nil { |
| 127 | return fmt.Errorf("Failed to read %s artifact: %s", file, err) |
| 128 | } |
| 129 | |
| 130 | for _, asset := range assets { |
| 131 | if *asset.Name == path.Base(file) { |
| 132 | if _, err := rc.Client.Repositories.DeleteReleaseAsset(rc.Context, rc.Owner, rc.Repo, *asset.ID); err != nil { |
| 133 | return fmt.Errorf("Failed to delete %s artifact: %s", file, err) |
| 134 | } |
| 135 | |
| 136 | fmt.Printf("Successfully deleted old %s artifact\n", *asset.Name) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | uo := &github.UploadOptions{Name: path.Base(file)} |
| 141 | |
| 142 | if _, _, err = rc.Client.Repositories.UploadReleaseAsset(rc.Context, rc.Owner, rc.Repo, id, uo, handle); err != nil { |
| 143 | return fmt.Errorf("Failed to upload %s artifact: %s", file, err) |
| 144 | } |
| 145 | |
| 146 | fmt.Printf("Successfully uploaded %s artifact\n", file) |
| 147 | } |
| 148 | |
| 149 | return nil |
| 150 | } |