Skip to content

Commit

Permalink
Added `--json/-j' flag to info command along with corresponding rende…
Browse files Browse the repository at this point in the history
…rers for both txt and JSON.
  • Loading branch information
jaytaylor authored and aktau committed Mar 29, 2017
1 parent 70ed556 commit f2ebbf7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
39 changes: 34 additions & 5 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@ func infocmd(opt Options) error {
}

// Find regular git tags.
tags, err := Tags(user, repo, token)
foundTags, err := Tags(user, repo, token)
if err != nil {
return fmt.Errorf("could not fetch tags, %v", err)
}
if len(tags) == 0 {
if len(foundTags) == 0 {
return fmt.Errorf("no tags available for %v/%v", user, repo)
}

fmt.Println("tags:")
for _, t := range tags {
tags := foundTags[:0]
for _, t := range foundTags {
// If the user only requested one tag, filter out the rest.
if tag == "" || t.Name == tag {
fmt.Println("-", &t)
tags = append(tags, t)
}
}

renderer := renderInfoText

if opt.Info.JSON {
renderer = renderInfoJSON
}

// List releases + assets.
var releases []Release
if tag == "" {
Expand All @@ -60,6 +66,15 @@ func infocmd(opt Options) error {
releases = []Release{*release}
}

return renderer(tags, releases)
}

func renderInfoText(tags []Tag, releases []Release) error {
fmt.Println("tags:")
for _, tag := range tags {
fmt.Println("-", &tag)
}

fmt.Println("releases:")
for _, release := range releases {
fmt.Println("-", &release)
Expand All @@ -68,6 +83,20 @@ func infocmd(opt Options) error {
return nil
}

func renderInfoJSON(tags []Tag, releases []Release) error {
out := struct {
Tags []Tag
Releases []Release
}{
Tags: tags,
Releases: releases,
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(&out)
}

func uploadcmd(opt Options) error {
user := nvls(opt.Upload.User, EnvUser)
repo := nvls(opt.Upload.Repo, EnvRepo)
Expand Down
1 change: 1 addition & 0 deletions github-release.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Options struct {
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 query (optional)'"`
JSON bool `goptions:"-j, --json, description='Emit info as JSON instead of text'"`
} `goptions:"info"`
}

Expand Down

0 comments on commit f2ebbf7

Please sign in to comment.