Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to specify download name with regex and info to get latest release info #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"os"
"regexp"
"strconv"
)

Expand All @@ -17,11 +18,27 @@ func infocmd(opt Options) error {
repo := nvls(opt.Info.Repo, EnvRepo)
token := nvls(opt.Info.Token, EnvToken)
tag := opt.Info.Tag
latest := opt.Info.Latest
latestTag := opt.Info.LatestTag

if user == "" || repo == "" {
return fmt.Errorf("user and repo need to be passed as arguments")
}

if latest || latestTag {
var rel *Release
var err error
rel, err = LatestRelease(user, repo, token)
if err != nil {
return err
}
tag = rel.TagName
if latestTag {
fmt.Println(tag)
return nil
}
}

/* find regular git tags */
allTags, err := Tags(user, repo, token)
if err != nil {
Expand Down Expand Up @@ -155,6 +172,7 @@ func downloadcmd(opt Options) error {
tag := opt.Download.Tag
name := opt.Download.Name
latest := opt.Download.Latest
regexed := opt.Download.Regexed

vprintln("downloading...")

Expand All @@ -175,20 +193,33 @@ func downloadcmd(opt Options) error {
}

assetId := 0
var rx *regexp.Regexp
var theName string

if regexed {

rx = regexp.MustCompile(name)
}

for _, asset := range rel.Assets {
if asset.Name == name {
if regexed {
theName = rx.FindString(asset.Name)
} else {
theName = name
}
if asset.Name == theName {
assetId = asset.Id
}
}

if assetId == 0 {
return fmt.Errorf("coud not find asset named %s", name)
return fmt.Errorf("coud not find asset named %s", theName)
}

var resp *http.Response
var url string
if token == "" {
url = GH_URL + fmt.Sprintf("/%s/%s/releases/download/%s/%s", user, repo, tag, name)
url = GH_URL + fmt.Sprintf("/%s/%s/releases/download/%s/%s", user, repo, tag, theName)
resp, err = http.Get(url)
} else {
url = ApiURL() + fmt.Sprintf(ASSET_DOWNLOAD_URI, user, repo, assetId)
Expand All @@ -214,9 +245,9 @@ func downloadcmd(opt Options) error {
return fmt.Errorf("github did not respond with 200 OK but with %v", resp.Status)
}

out, err := os.Create(name)
out, err := os.Create(theName)
if err != nil {
return fmt.Errorf("could not create file %s", name)
return fmt.Errorf("could not create file %s", theName)
}
defer out.Close()

Expand Down
23 changes: 13 additions & 10 deletions github-release.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ type Options struct {

goptions.Verbs
Download struct {
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
User string `goptions:"-u, --user, description='Github user (required if $GITHUB_USER not set)'"`
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
Latest bool `goptions:"-l, --latest, description='Download latest release (required if tag is not specified)',mutexgroup='input'"`
Tag string `goptions:"-t, --tag, description='Git tag to download from (required if latest is not specified)', mutexgroup='input',obligatory"`
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
User string `goptions:"-u, --user, description='Github user (required if $GITHUB_USER not set)'"`
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
Latest bool `goptions:"-l, --latest, description='Download latest release (required if tag is not specified)',mutexgroup='input'"`
Tag string `goptions:"-t, --tag, description='Git tag to download from (required if latest is not specified)', mutexgroup='input',obligatory"`
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
Regexed bool `goptions:"-R, --regexed, description='Treat name as a regex'"`
} `goptions:"download"`
Upload struct {
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
Expand Down Expand Up @@ -59,10 +60,12 @@ type Options struct {
Tag string `goptions:"-t, --tag, obligatory, description='Git tag of release to delete'"`
} `goptions:"delete"`
Info struct {
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
User string `goptions:"-u, --user, description='Github user (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 query (optional)'"`
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
User string `goptions:"-u, --user, description='Github user (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 query (optional)', mutexgroup='input',obligatory"`
Latest bool `goptions:"-l, --latest, description='Info on the latest release (required if tag is not specified)',mutexgroup='input'"`
LatestTag bool `goptions:"-L, --latest_tag, description='Info on the latest release (required if tag is not specified)',mutexgroup='input'"`
} `goptions:"info"`
}

Expand Down