Skip to content

Commit

Permalink
feat(cmd): format option with new YAML & TOML outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Jan 12, 2025
1 parent e44ef80 commit 7ffb283
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
18 changes: 9 additions & 9 deletions changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import (
)

type ChangelogEntry struct {
Version string `json:"version"`
Date time.Time `json:"date"`
CompareURL string `json:"compareUrl"`
Changes map[string][]Change `json:"changes"`
Version string `json:"version" yaml:"version" toml:"version"`
Date time.Time `json:"date" yaml:"date" toml:"date"`
CompareURL string `json:"compareUrl" yaml:"compareUrl" toml:"compareUrl"`
Changes map[string][]Change `json:"changes" yaml:"changes" toml:"changes"`
}

type Change struct {
Scope string `json:"scope,omitempty"`
Description string `json:"description"`
PR string `json:"pr,omitempty"`
Commit string `json:"commit,omitempty"`
CommitBody string `json:"commitBody,omitempty"`
Scope string `json:"scope,omitempty" yaml:"scope,omitempty" toml:"scope,omitempty"`
Description string `json:"description" yaml:"description" toml:"description"`
PR string `json:"pr,omitempty" yaml:"pr,omitempty" toml:"pr,omitempty"`
Commit string `json:"commit,omitempty" yaml:"commit,omitempty" toml:"commit,omitempty"`
CommitBody string `json:"commitBody,omitempty" yaml:"commitBody,omitempty" toml:"commitBody,omitempty"`
}

type Parser struct {
Expand Down
30 changes: 24 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/pelletier/go-toml/v2"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"cl-parse/changelog"
"cl-parse/git"
Expand All @@ -26,6 +29,7 @@ var cmd = &cobra.Command{
latest, _ := cmd.Flags().GetBool("latest")
release, _ := cmd.Flags().GetString("release")
includeBody, _ := cmd.Flags().GetBool("include-body")
format, _ := cmd.Flags().GetString("format")

if ver {
fmt.Printf("cl-parse v%s\n", VERSION)
Expand Down Expand Up @@ -58,25 +62,25 @@ var cmd = &cobra.Command{
fmt.Println("No changelog entries found")
os.Exit(1)
}
jsonData, err := json.MarshalIndent(entries[0], "", " ")
outputData, err := marshalWithFormat(entries[0], format)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(jsonData))
fmt.Println(string(outputData))
return
}

if release != "" {
found := false
for _, entry := range entries {
if entry.Version == release {
jsonData, err := json.MarshalIndent(entry, "", " ")
outputData, err := marshalWithFormat(entry, format)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(jsonData))
fmt.Println(string(outputData))
found = true
break
}
Expand All @@ -89,12 +93,12 @@ var cmd = &cobra.Command{
}

// default to printing all entries
jsonData, err := json.MarshalIndent(entries, "", " ")
outputData, err := marshalWithFormat(entries, format)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(jsonData))
fmt.Println(string(outputData))
},
}

Expand All @@ -110,4 +114,18 @@ func init() {
cmd.Flags().BoolP("latest", "l", false, "display the most recent version from the changelog")
cmd.Flags().StringP("release", "r", "", "display the changelog entry for a specific release")
cmd.Flags().Bool("include-body", false, "include the full commit body in changelog entry")
cmd.Flags().StringP("format", "f", "json", "output format (json, yaml, or toml)")
}

func marshalWithFormat(v interface{}, format string) ([]byte, error) {
switch strings.ToLower(format) {
case "json":
return json.MarshalIndent(v, "", " ")
case "yaml":
return yaml.Marshal(v)
case "toml":
return toml.Marshal(v)
default:
return nil, fmt.Errorf("unsupported format: %s", format)
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mmcloughlin/avo v0.6.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pjbgf/sha1cd v0.3.1 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.3.0 // indirect
Expand All @@ -30,4 +31,5 @@ require (
golang.org/x/sys v0.29.0 // indirect
golang.org/x/tools v0.29.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pjbgf/sha1cd v0.3.1 h1:Dh2GYdpJnO84lIw0LJwTFXjcNbasP/bklicSznyAaPI=
github.com/pjbgf/sha1cd v0.3.1/go.mod h1:Y8t7jSB/dEI/lQE04A1HVKteqjj9bX5O4+Cex0TCu8s=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -81,4 +83,5 @@ gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 7ffb283

Please sign in to comment.