diff --git a/.golangci.yml b/.golangci.yml index 34fe368..deed852 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -75,6 +75,9 @@ issues: - source: "`json:" linters: - lll + - source: "`yaml:" + linters: + - lll run: skip-dirs: diff --git a/pkg/cli/plan.go b/pkg/cli/plan.go index 32df157..cfae4c2 100644 --- a/pkg/cli/plan.go +++ b/pkg/cli/plan.go @@ -1,6 +1,7 @@ package cli import ( + "fmt" "os" "path/filepath" @@ -37,6 +38,12 @@ func planCmd() *cli2.Command { Name: "sha", Usage: "override commit SHA of specified git branch/tag", }, + &cli2.StringFlag{ + Name: "format", + Aliases: []string{"f"}, + Usage: "output format of build plan (yaml or json)", + Value: "yaml", + }, &cli2.StringFlag{ Name: "output", Usage: "output filename to write plan to instead of printing " + @@ -102,7 +109,18 @@ func planAction(c *cli2.Context, opts *Options) error { return err } - planYAML, err := p.YAML() + format := c.String("format") + var plan string + switch format { + case "yaml", "yml": + format = "yaml" + plan, err = p.YAML() + case "json": + format = "json" + plan, err = p.JSON() + default: + err = fmt.Errorf("--format must be yaml or json") + } if err != nil { return err } @@ -111,7 +129,7 @@ func planAction(c *cli2.Context, opts *Options) error { out = os.Stdout if f := c.String("output"); f != "" { logger.Info("writing plan", "file", f) - logger.Debug("content", "yaml", planYAML) + logger.Debug("content", format, plan) out, err = os.Create(f) if err != nil { return err @@ -119,7 +137,7 @@ func planAction(c *cli2.Context, opts *Options) error { defer out.Close() } - _, err = out.WriteString(planYAML) + _, err = out.WriteString(plan) if err != nil { return err } diff --git a/pkg/commit/commit.go b/pkg/commit/commit.go index f7d4b79..673386c 100644 --- a/pkg/commit/commit.go +++ b/pkg/commit/commit.go @@ -8,11 +8,11 @@ import ( ) type Commit struct { - SHA string `yaml:"sha"` - Date *time.Time `yaml:"date"` - Author string `yaml:"author"` - Committer string `yaml:"committer"` - Message string `yaml:"message"` + SHA string `yaml:"sha" json:"sha"` + Date *time.Time `yaml:"date" json:"date"` + Author string `yaml:"author" json:"author"` + Committer string `yaml:"committer" json:"committer"` + Message string `yaml:"message" json:"message"` } func New(rc *github.RepositoryCommit) *Commit { diff --git a/pkg/osinfo/osinfo.go b/pkg/osinfo/osinfo.go index e8bca96..e801117 100644 --- a/pkg/osinfo/osinfo.go +++ b/pkg/osinfo/osinfo.go @@ -6,9 +6,9 @@ import ( ) type OSInfo struct { - Name string `yaml:"name"` - Version string `yaml:"version"` - Arch string `yaml:"arch"` + Name string `yaml:"name" json:"name"` + Version string `yaml:"version" json:"version"` + Arch string `yaml:"arch" json:"arch"` } func New() (*OSInfo, error) { diff --git a/pkg/plan/plan.go b/pkg/plan/plan.go index 333da67..7d119e2 100644 --- a/pkg/plan/plan.go +++ b/pkg/plan/plan.go @@ -2,6 +2,7 @@ package plan import ( "bytes" + "encoding/json" "io" "os" @@ -11,11 +12,11 @@ import ( ) type Plan struct { - Build *Build `yaml:"build,omitempty"` - Source *source.Source `yaml:"source,omitempty"` - OS *osinfo.OSInfo `yaml:"os,omitempty"` - Release *Release `yaml:"release,omitempty"` - Output *Output `yaml:"output,omitempty"` + Build *Build `yaml:"build,omitempty" json:"build,omitempty"` + Source *source.Source `yaml:"source,omitempty" json:"source,omitempty"` + OS *osinfo.OSInfo `yaml:"os,omitempty" json:"os,omitempty"` + Release *Release `yaml:"release,omitempty" json:"release,omitempty"` + Output *Output `yaml:"output,omitempty" json:"output,omitempty"` } // Load attempts to loads a plan YAML from given filename. @@ -53,18 +54,37 @@ func (s *Plan) YAML() (string, error) { return buf.String(), nil } +// WriteJSON writes plan in JSON format to given io.Writer. +func (s *Plan) WriteJSON(w io.Writer) error { + enc := json.NewEncoder(w) + enc.SetIndent("", " ") + + return enc.Encode(s) +} + +// JSON returns plan in JSON format. +func (s *Plan) JSON() (string, error) { + var buf bytes.Buffer + err := s.WriteJSON(&buf) + if err != nil { + return "", err + } + + return buf.String(), nil +} + type Build struct { - Name string `yaml:"name,omitempty"` + Name string `yaml:"name,omitempty" json:"name,omitempty"` } type Release struct { - Name string `yaml:"name"` - Title string `yaml:"title,omitempty"` - Draft bool `yaml:"draft,omitempty"` - Prerelease bool `yaml:"prerelease,omitempty"` + Name string `yaml:"name" json:"name"` + Title string `yaml:"title,omitempty" json:"title,omitempty"` + Draft bool `yaml:"draft,omitempty" json:"draft,omitempty"` + Prerelease bool `yaml:"prerelease,omitempty" json:"prerelease,omitempty"` } type Output struct { - Directory string `yaml:"directory,omitempty"` - DiskImage string `yaml:"disk_image,omitempty"` + Directory string `yaml:"directory,omitempty" json:"directory,omitempty"` + DiskImage string `yaml:"disk_image,omitempty" json:"disk_image,omitempty"` } diff --git a/pkg/repository/repository.go b/pkg/repository/repository.go index f29c2a1..501fa28 100644 --- a/pkg/repository/repository.go +++ b/pkg/repository/repository.go @@ -22,8 +22,8 @@ const GitHub Type = "github" // Repository represents basic information about a repository with helper // methods to get various pieces of information from it. type Repository struct { - Type Type `yaml:"type,omitempty"` - Source string `yaml:"source,omitempty"` + Type Type `yaml:"type,omitempty" json:"type,omitempty"` + Source string `yaml:"source,omitempty" json:"source,omitempty"` } func NewGitHub(ownerAndName string) (*Repository, error) { diff --git a/pkg/source/source.go b/pkg/source/source.go index 749f8fa..b315168 100644 --- a/pkg/source/source.go +++ b/pkg/source/source.go @@ -6,12 +6,12 @@ import ( ) type Source struct { - Ref string `yaml:"ref,omitempty"` - Repository *repository.Repository `yaml:"repository,omitempty"` - Commit *commit.Commit `yaml:"commit,omitempty"` - Tarball *Tarball `yaml:"tarball,omitempty"` + Ref string `yaml:"ref,omitempty" json:"ref,omitempty"` + Repository *repository.Repository `yaml:"repository,omitempty" json:"repository,omitempty"` + Commit *commit.Commit `yaml:"commit,omitempty" json:"commit,omitempty"` + Tarball *Tarball `yaml:"tarball,omitempty" json:"tarball,omitempty"` } type Tarball struct { - URL string `yaml:"url,omitempty"` + URL string `yaml:"url,omitempty" json:"url,omitempty"` }