Skip to content

Commit 1bbfe5d

Browse files
committed
feat(plan): allow build plan to be output as YAML or JSON
1 parent b4c5184 commit 1bbfe5d

File tree

7 files changed

+71
-30
lines changed

7 files changed

+71
-30
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ issues:
7575
- source: "`json:"
7676
linters:
7777
- lll
78+
- source: "`yaml:"
79+
linters:
80+
- lll
7881

7982
run:
8083
skip-dirs:

pkg/cli/plan.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67

@@ -37,6 +38,12 @@ func planCmd() *cli2.Command {
3738
Name: "sha",
3839
Usage: "override commit SHA of specified git branch/tag",
3940
},
41+
&cli2.StringFlag{
42+
Name: "format",
43+
Aliases: []string{"f"},
44+
Usage: "output format of build plan (yaml or json)",
45+
Value: "yaml",
46+
},
4047
&cli2.StringFlag{
4148
Name: "output",
4249
Usage: "output filename to write plan to instead of printing " +
@@ -102,7 +109,18 @@ func planAction(c *cli2.Context, opts *Options) error {
102109
return err
103110
}
104111

105-
planYAML, err := p.YAML()
112+
format := c.String("format")
113+
var plan string
114+
switch format {
115+
case "yaml", "yml":
116+
format = "yaml"
117+
plan, err = p.YAML()
118+
case "json":
119+
format = "json"
120+
plan, err = p.JSON()
121+
default:
122+
err = fmt.Errorf("--format must be yaml or json")
123+
}
106124
if err != nil {
107125
return err
108126
}
@@ -111,15 +129,15 @@ func planAction(c *cli2.Context, opts *Options) error {
111129
out = os.Stdout
112130
if f := c.String("output"); f != "" {
113131
logger.Info("writing plan", "file", f)
114-
logger.Debug("content", "yaml", planYAML)
132+
logger.Debug("content", format, plan)
115133
out, err = os.Create(f)
116134
if err != nil {
117135
return err
118136
}
119137
defer out.Close()
120138
}
121139

122-
_, err = out.WriteString(planYAML)
140+
_, err = out.WriteString(plan)
123141
if err != nil {
124142
return err
125143
}

pkg/commit/commit.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
)
99

1010
type Commit struct {
11-
SHA string `yaml:"sha"`
12-
Date *time.Time `yaml:"date"`
13-
Author string `yaml:"author"`
14-
Committer string `yaml:"committer"`
15-
Message string `yaml:"message"`
11+
SHA string `yaml:"sha" json:"sha"`
12+
Date *time.Time `yaml:"date" json:"date"`
13+
Author string `yaml:"author" json:"author"`
14+
Committer string `yaml:"committer" json:"committer"`
15+
Message string `yaml:"message" json:"message"`
1616
}
1717

1818
func New(rc *github.RepositoryCommit) *Commit {

pkg/osinfo/osinfo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
)
77

88
type OSInfo struct {
9-
Name string `yaml:"name"`
10-
Version string `yaml:"version"`
11-
Arch string `yaml:"arch"`
9+
Name string `yaml:"name" json:"name"`
10+
Version string `yaml:"version" json:"version"`
11+
Arch string `yaml:"arch" json:"arch"`
1212
}
1313

1414
func New() (*OSInfo, error) {

pkg/plan/plan.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package plan
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"io"
67
"os"
78

@@ -11,11 +12,11 @@ import (
1112
)
1213

1314
type Plan struct {
14-
Build *Build `yaml:"build,omitempty"`
15-
Source *source.Source `yaml:"source,omitempty"`
16-
OS *osinfo.OSInfo `yaml:"os,omitempty"`
17-
Release *Release `yaml:"release,omitempty"`
18-
Output *Output `yaml:"output,omitempty"`
15+
Build *Build `yaml:"build,omitempty" json:"build,omitempty"`
16+
Source *source.Source `yaml:"source,omitempty" json:"source,omitempty"`
17+
OS *osinfo.OSInfo `yaml:"os,omitempty" json:"os,omitempty"`
18+
Release *Release `yaml:"release,omitempty" json:"release,omitempty"`
19+
Output *Output `yaml:"output,omitempty" json:"output,omitempty"`
1920
}
2021

2122
// Load attempts to loads a plan YAML from given filename.
@@ -53,18 +54,37 @@ func (s *Plan) YAML() (string, error) {
5354
return buf.String(), nil
5455
}
5556

57+
// WriteJSON writes plan in JSON format to given io.Writer.
58+
func (s *Plan) WriteJSON(w io.Writer) error {
59+
enc := json.NewEncoder(w)
60+
enc.SetIndent("", " ")
61+
62+
return enc.Encode(s)
63+
}
64+
65+
// JSON returns plan in JSON format.
66+
func (s *Plan) JSON() (string, error) {
67+
var buf bytes.Buffer
68+
err := s.WriteJSON(&buf)
69+
if err != nil {
70+
return "", err
71+
}
72+
73+
return buf.String(), nil
74+
}
75+
5676
type Build struct {
57-
Name string `yaml:"name,omitempty"`
77+
Name string `yaml:"name,omitempty" json:"name,omitempty"`
5878
}
5979

6080
type Release struct {
61-
Name string `yaml:"name"`
62-
Title string `yaml:"title,omitempty"`
63-
Draft bool `yaml:"draft,omitempty"`
64-
Prerelease bool `yaml:"prerelease,omitempty"`
81+
Name string `yaml:"name" json:"name"`
82+
Title string `yaml:"title,omitempty" json:"title,omitempty"`
83+
Draft bool `yaml:"draft,omitempty" json:"draft,omitempty"`
84+
Prerelease bool `yaml:"prerelease,omitempty" json:"prerelease,omitempty"`
6585
}
6686

6787
type Output struct {
68-
Directory string `yaml:"directory,omitempty"`
69-
DiskImage string `yaml:"disk_image,omitempty"`
88+
Directory string `yaml:"directory,omitempty" json:"directory,omitempty"`
89+
DiskImage string `yaml:"disk_image,omitempty" json:"disk_image,omitempty"`
7090
}

pkg/repository/repository.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const GitHub Type = "github"
2222
// Repository represents basic information about a repository with helper
2323
// methods to get various pieces of information from it.
2424
type Repository struct {
25-
Type Type `yaml:"type,omitempty"`
26-
Source string `yaml:"source,omitempty"`
25+
Type Type `yaml:"type,omitempty" json:"type,omitempty"`
26+
Source string `yaml:"source,omitempty" json:"source,omitempty"`
2727
}
2828

2929
func NewGitHub(ownerAndName string) (*Repository, error) {

pkg/source/source.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
)
77

88
type Source struct {
9-
Ref string `yaml:"ref,omitempty"`
10-
Repository *repository.Repository `yaml:"repository,omitempty"`
11-
Commit *commit.Commit `yaml:"commit,omitempty"`
12-
Tarball *Tarball `yaml:"tarball,omitempty"`
9+
Ref string `yaml:"ref,omitempty" json:"ref,omitempty"`
10+
Repository *repository.Repository `yaml:"repository,omitempty" json:"repository,omitempty"`
11+
Commit *commit.Commit `yaml:"commit,omitempty" json:"commit,omitempty"`
12+
Tarball *Tarball `yaml:"tarball,omitempty" json:"tarball,omitempty"`
1313
}
1414

1515
type Tarball struct {
16-
URL string `yaml:"url,omitempty"`
16+
URL string `yaml:"url,omitempty" json:"url,omitempty"`
1717
}

0 commit comments

Comments
 (0)