Skip to content

Commit

Permalink
upload: add -R/--replace switch
Browse files Browse the repository at this point in the history
A PATCH would've been better in order to make the whole shebang atomic and
save some code. However, the Github API documentation seems to imply that
you can only use PATCH to edit metadata:
https://developer.github.com/v3/repos/releases/#edit-a-release-asset.

Fixes github-release#38.
Fixes github-release#39.
  • Loading branch information
aktau committed Mar 26, 2017
1 parent 3058c29 commit 47f6a5a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
11 changes: 11 additions & 0 deletions assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ type Asset struct {
Created time.Time `json:"created_at"`
Published time.Time `json:"published_at"`
}

// findAssetID returns the asset ID if name can be found in assets,
// otherwise returns -1.
func findAssetID(assets []Asset, name string) int {
for _, asset := range assets {
if asset.Name == name {
return asset.Id
}
}
return -1
}
25 changes: 15 additions & 10 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ func uploadcmd(opt Options) error {
return err
}

// If asked to replace, first delete the existing asset, if any.
if assetID := findAssetID(rel.Assets, name); opt.Upload.Replace && assetID != -1 {
URL := nvls(EnvApiEndpoint, github.DefaultBaseURL) +
fmt.Sprintf(ASSET_DOWNLOAD_URI, user, repo, assetID)
resp, err := github.DoAuthRequest("DELETE", URL, "application/json", token, nil, nil)
if err != nil || resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("could not replace asset %s (ID: %d), deletion failed (error: %v, status: %s)",
name, assetID, err, resp.Status)
}
}

v := url.Values{}
v.Set("name", name)
if label != "" {
Expand Down Expand Up @@ -154,14 +165,8 @@ func downloadcmd(opt Options) error {
return err
}

assetId := 0
for _, asset := range rel.Assets {
if asset.Name == name {
assetId = asset.Id
}
}

if assetId == 0 {
assetID := findAssetID(rel.Assets, name)
if assetID == -1 {
return fmt.Errorf("coud not find asset named %s", name)
}

Expand All @@ -170,7 +175,7 @@ func downloadcmd(opt Options) error {
// Use the regular github.com site it we don't have a token.
resp, err = http.Get("https://github.com" + fmt.Sprintf("/%s/%s/releases/download/%s/%s", user, repo, tag, name))
} else {
url := nvls(EnvApiEndpoint, github.DefaultBaseURL) + fmt.Sprintf(ASSET_DOWNLOAD_URI, user, repo, assetId)
url := nvls(EnvApiEndpoint, github.DefaultBaseURL) + fmt.Sprintf(ASSET_DOWNLOAD_URI, user, repo, assetID)
resp, err = github.DoAuthRequest("GET", url, "", token, map[string]string{
"Accept": "application/octet-stream",
}, nil)
Expand Down Expand Up @@ -401,7 +406,7 @@ func deletecmd(opt Options) error {
resp, err := github.DoAuthRequest("DELETE", baseURL+fmt.Sprintf("/repos/%s/%s/releases/%d",
user, repo, id), "application/json", token, nil, nil)
if err != nil {
return fmt.Errorf("release deletion unsuccesful, %v", err)
return fmt.Errorf("release deletion failed: %v", err)
}
defer resp.Body.Close()

Expand Down
15 changes: 8 additions & 7 deletions github-release.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ type Options struct {
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
} `goptions:"download"`
Upload struct {
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
Tag string `goptions:"-t, --tag, description='Git tag to upload to', obligatory"`
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
Label string `goptions:"-l, --label, description='Label (description) of the file'"`
File *os.File `goptions:"-f, --file, description='File to upload (use - for stdin)', rdonly, obligatory"`
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
Tag string `goptions:"-t, --tag, description='Git tag to upload to', obligatory"`
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
Label string `goptions:"-l, --label, description='Label (description) of the file'"`
File *os.File `goptions:"-f, --file, description='File to upload (use - for stdin)', rdonly, obligatory"`
Replace bool `goptions:"-R, --replace, description='Replace asset with same name if it already exists (WARNING: not atomic, failure to upload will remove the original asset too)'"`
} `goptions:"upload"`
Release struct {
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
Expand Down

0 comments on commit 47f6a5a

Please sign in to comment.