| 136 | } |
| 137 | |
| 138 | func editRun(opts *EditOptions) error { |
| 139 | repo, err := opts.BaseRepo() |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | if err := shared.CheckHost(repo.RepoHost()); err != nil { |
| 144 | return err |
| 145 | } |
| 146 | |
| 147 | // The new content is read and checked before any request, so a file that |
| 148 | // can't be used fails without one. |
| 149 | content, dir, err := readContent(opts) |
| 150 | if err != nil { |
| 151 | return err |
| 152 | } |
| 153 | |
| 154 | c, err := opts.Client() |
| 155 | if err != nil { |
| 156 | return err |
| 157 | } |
| 158 | |
| 159 | var uploader *attachments.Uploader |
| 160 | if len(opts.Assets) > 0 { |
| 161 | uploader, err = shared.NewUploader(c, repo, opts.IssueNumber, opts.HttpClient) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | } else if err := shared.CheckIssue(c, repo, opts.IssueNumber); err != nil { |
| 166 | return err |
| 167 | } |
| 168 | |
| 169 | action := fmt.Sprintf("update artifact %d", opts.ArtifactNumber) |
| 170 | if opts.RestoreVersion > 0 { |
| 171 | action = fmt.Sprintf("restore artifact %d", opts.ArtifactNumber) |
| 172 | } |
| 173 | result := shared.Result{Number: opts.ArtifactNumber, Source: opts.BodyFile} |
| 174 | current, err := c.Get(repo, opts.IssueNumber, opts.ArtifactNumber) |
| 175 | if err != nil { |
| 176 | shared.PrintFailure(opts.IO, result, action, err) |
| 177 | return cmdutil.SilentError |
| 178 | } |
| 179 | result.Name = text.RemoveExcessiveWhitespace(current.Name) |
| 180 | |
| 181 | // The type decides what the new content can be, so every check that |
| 182 | // depends on it runs before anything is uploaded or saved. |
| 183 | if err := shared.CheckType(current.Artifact); err != nil { |
| 184 | return err |
| 185 | } |
| 186 | if opts.RestoreVersion > 0 { |
| 187 | return restoreVersion(opts, c, repo, current, result, action) |
| 188 | } |
| 189 | body, err := newBody(opts, current.Artifact, content) |
| 190 | if err != nil { |
| 191 | return err |
| 192 | } |
| 193 | |
| 194 | var uploadErr error |
| 195 | if uploader != nil { |