Skip to content

Commit f2ebbf7

Browse files
jaytayloraktau
authored andcommitted
Added `--json/-j' flag to info command along with corresponding renderers for both txt and JSON.
1 parent 70ed556 commit f2ebbf7

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

cmd.go

+34-5
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,28 @@ func infocmd(opt Options) error {
2525
}
2626

2727
// Find regular git tags.
28-
tags, err := Tags(user, repo, token)
28+
foundTags, err := Tags(user, repo, token)
2929
if err != nil {
3030
return fmt.Errorf("could not fetch tags, %v", err)
3131
}
32-
if len(tags) == 0 {
32+
if len(foundTags) == 0 {
3333
return fmt.Errorf("no tags available for %v/%v", user, repo)
3434
}
3535

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

44+
renderer := renderInfoText
45+
46+
if opt.Info.JSON {
47+
renderer = renderInfoJSON
48+
}
49+
4450
// List releases + assets.
4551
var releases []Release
4652
if tag == "" {
@@ -60,6 +66,15 @@ func infocmd(opt Options) error {
6066
releases = []Release{*release}
6167
}
6268

69+
return renderer(tags, releases)
70+
}
71+
72+
func renderInfoText(tags []Tag, releases []Release) error {
73+
fmt.Println("tags:")
74+
for _, tag := range tags {
75+
fmt.Println("-", &tag)
76+
}
77+
6378
fmt.Println("releases:")
6479
for _, release := range releases {
6580
fmt.Println("-", &release)
@@ -68,6 +83,20 @@ func infocmd(opt Options) error {
6883
return nil
6984
}
7085

86+
func renderInfoJSON(tags []Tag, releases []Release) error {
87+
out := struct {
88+
Tags []Tag
89+
Releases []Release
90+
}{
91+
Tags: tags,
92+
Releases: releases,
93+
}
94+
95+
enc := json.NewEncoder(os.Stdout)
96+
enc.SetIndent("", " ")
97+
return enc.Encode(&out)
98+
}
99+
71100
func uploadcmd(opt Options) error {
72101
user := nvls(opt.Upload.User, EnvUser)
73102
repo := nvls(opt.Upload.Repo, EnvRepo)

github-release.go

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type Options struct {
6767
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
6868
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
6969
Tag string `goptions:"-t, --tag, description='Git tag to query (optional)'"`
70+
JSON bool `goptions:"-j, --json, description='Emit info as JSON instead of text'"`
7071
} `goptions:"info"`
7172
}
7273

0 commit comments

Comments
 (0)