feat(plan): allow build plan to be output as YAML or JSON

This commit is contained in:
2021-10-09 20:25:12 +01:00
parent b4c5184cef
commit 1bbfe5d3ea
7 changed files with 71 additions and 30 deletions

View File

@@ -75,6 +75,9 @@ issues:
- source: "`json:"
linters:
- lll
- source: "`yaml:"
linters:
- lll
run:
skip-dirs:

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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"`
}

View File

@@ -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) {

View File

@@ -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"`
}