Skip to content

Commit 7ffb283

Browse files
committed
feat(cmd): format option with new YAML & TOML outputs
1 parent e44ef80 commit 7ffb283

4 files changed

Lines changed: 38 additions & 15 deletions

File tree

changelog/changelog.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ import (
1111
)
1212

1313
type ChangelogEntry struct {
14-
Version string `json:"version"`
15-
Date time.Time `json:"date"`
16-
CompareURL string `json:"compareUrl"`
17-
Changes map[string][]Change `json:"changes"`
14+
Version string `json:"version" yaml:"version" toml:"version"`
15+
Date time.Time `json:"date" yaml:"date" toml:"date"`
16+
CompareURL string `json:"compareUrl" yaml:"compareUrl" toml:"compareUrl"`
17+
Changes map[string][]Change `json:"changes" yaml:"changes" toml:"changes"`
1818
}
1919

2020
type Change struct {
21-
Scope string `json:"scope,omitempty"`
22-
Description string `json:"description"`
23-
PR string `json:"pr,omitempty"`
24-
Commit string `json:"commit,omitempty"`
25-
CommitBody string `json:"commitBody,omitempty"`
21+
Scope string `json:"scope,omitempty" yaml:"scope,omitempty" toml:"scope,omitempty"`
22+
Description string `json:"description" yaml:"description" toml:"description"`
23+
PR string `json:"pr,omitempty" yaml:"pr,omitempty" toml:"pr,omitempty"`
24+
Commit string `json:"commit,omitempty" yaml:"commit,omitempty" toml:"commit,omitempty"`
25+
CommitBody string `json:"commitBody,omitempty" yaml:"commitBody,omitempty" toml:"commitBody,omitempty"`
2626
}
2727

2828
type Parser struct {

cmd/cmd.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"encoding/json"
55
"fmt"
66
"os"
7+
"strings"
78

9+
"github.com/pelletier/go-toml/v2"
810
"github.com/spf13/cobra"
11+
"gopkg.in/yaml.v3"
912

1013
"cl-parse/changelog"
1114
"cl-parse/git"
@@ -26,6 +29,7 @@ var cmd = &cobra.Command{
2629
latest, _ := cmd.Flags().GetBool("latest")
2730
release, _ := cmd.Flags().GetString("release")
2831
includeBody, _ := cmd.Flags().GetBool("include-body")
32+
format, _ := cmd.Flags().GetString("format")
2933

3034
if ver {
3135
fmt.Printf("cl-parse v%s\n", VERSION)
@@ -58,25 +62,25 @@ var cmd = &cobra.Command{
5862
fmt.Println("No changelog entries found")
5963
os.Exit(1)
6064
}
61-
jsonData, err := json.MarshalIndent(entries[0], "", " ")
65+
outputData, err := marshalWithFormat(entries[0], format)
6266
if err != nil {
6367
fmt.Println(err)
6468
os.Exit(1)
6569
}
66-
fmt.Println(string(jsonData))
70+
fmt.Println(string(outputData))
6771
return
6872
}
6973

7074
if release != "" {
7175
found := false
7276
for _, entry := range entries {
7377
if entry.Version == release {
74-
jsonData, err := json.MarshalIndent(entry, "", " ")
78+
outputData, err := marshalWithFormat(entry, format)
7579
if err != nil {
7680
fmt.Println(err)
7781
os.Exit(1)
7882
}
79-
fmt.Println(string(jsonData))
83+
fmt.Println(string(outputData))
8084
found = true
8185
break
8286
}
@@ -89,12 +93,12 @@ var cmd = &cobra.Command{
8993
}
9094

9195
// default to printing all entries
92-
jsonData, err := json.MarshalIndent(entries, "", " ")
96+
outputData, err := marshalWithFormat(entries, format)
9397
if err != nil {
9498
fmt.Println(err)
9599
os.Exit(1)
96100
}
97-
fmt.Println(string(jsonData))
101+
fmt.Println(string(outputData))
98102
},
99103
}
100104

@@ -110,4 +114,18 @@ func init() {
110114
cmd.Flags().BoolP("latest", "l", false, "display the most recent version from the changelog")
111115
cmd.Flags().StringP("release", "r", "", "display the changelog entry for a specific release")
112116
cmd.Flags().Bool("include-body", false, "include the full commit body in changelog entry")
117+
cmd.Flags().StringP("format", "f", "json", "output format (json, yaml, or toml)")
118+
}
119+
120+
func marshalWithFormat(v interface{}, format string) ([]byte, error) {
121+
switch strings.ToLower(format) {
122+
case "json":
123+
return json.MarshalIndent(v, "", " ")
124+
case "yaml":
125+
return yaml.Marshal(v)
126+
case "toml":
127+
return toml.Marshal(v)
128+
default:
129+
return nil, fmt.Errorf("unsupported format: %s", format)
130+
}
113131
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
1818
github.com/kevinburke/ssh_config v1.2.0 // indirect
1919
github.com/mmcloughlin/avo v0.6.0 // indirect
20+
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
2021
github.com/pjbgf/sha1cd v0.3.1 // indirect
2122
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
2223
github.com/skeema/knownhosts v1.3.0 // indirect
@@ -30,4 +31,5 @@ require (
3031
golang.org/x/sys v0.29.0 // indirect
3132
golang.org/x/tools v0.29.0 // indirect
3233
gopkg.in/warnings.v0 v0.1.2 // indirect
34+
gopkg.in/yaml.v3 v3.0.1 // indirect
3335
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
3333
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
3434
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
3535
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
36+
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
37+
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
3638
github.com/pjbgf/sha1cd v0.3.1 h1:Dh2GYdpJnO84lIw0LJwTFXjcNbasP/bklicSznyAaPI=
3739
github.com/pjbgf/sha1cd v0.3.1/go.mod h1:Y8t7jSB/dEI/lQE04A1HVKteqjj9bX5O4+Cex0TCu8s=
3840
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -81,4 +83,5 @@ gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
8183
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
8284
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
8385
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
86+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
8487
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)