Skip to content

Commit

Permalink
feat(changelog): add version flag -v to show version information
Browse files Browse the repository at this point in the history
  • Loading branch information
zbindenren committed Jan 5, 2021
1 parent 0c43f49 commit 4b5090c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
15 changes: 14 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ import (
"github.com/zbindenren/cc/internal/cmd"
)

// nolint: gochecknoglobals
// following variables are set during build by goreleaser
var (
version = "dev"
commit = "12345678"
date = ""
)

func main() {
command := cmd.New()
b, err := cmd.NewBuildInfo(version, date, commit)
if err != nil {
log.Fatal(err)
}

command := cmd.New(*b)
if err := command.Run(); err != nil {
log.Fatal(err)
}
Expand Down
12 changes: 11 additions & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
sinceTagOptName = "since"
initDfltConfigOptName = "init-config"
noPromptOptName = "n"
versionOptName = "v"

dfltChangelogFile = "CHANGELOG.md"
dateFormat = "2006-01-02"
Expand All @@ -37,6 +38,7 @@ const (
type Command struct {
noop bool // for tests
fs *flag.FlagSet
b BuildInfo
// flags
file *string
debug *bool
Expand All @@ -46,14 +48,16 @@ type Command struct {
sinceTag *string
initConfig *bool
noPrompt *bool
version *bool
}

// New creates a new Command.
func New() *Command {
func New(b BuildInfo) *Command {
fs := flag.NewFlagSet("changelog", flag.ExitOnError)

return &Command{
fs: fs,
b: b,
file: fs.String(fileOptName, dfltChangelogFile, "changelog file name"),
debug: fs.Bool(debugOptName, false, "log debug information"),
toStdOut: fs.Bool(stdOutOptName, false, "output changelog to stdout instead to file"),
Expand All @@ -62,6 +66,7 @@ func New() *Command {
sinceTag: fs.String(sinceTagOptName, "", fmt.Sprintf("in combination with -%s: if a tag is specified, the changelog will be created from that tag on", historyOptName)),
initConfig: fs.Bool(initDfltConfigOptName, false, fmt.Sprintf("initialize a default changelog configuration '%s'", config.FileName)),
noPrompt: fs.Bool(noPromptOptName, false, "do not prompt for next version"),
version: fs.Bool(versionOptName, false, "show program version information"),
}
}

Expand All @@ -74,6 +79,11 @@ func (c Command) Run() error {
}
}

if *c.version {
fmt.Println(c.b.Version("changelog"))
return nil
}

// history is always written to stdout
if *c.history {
*c.toStdOut = true
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func setup(t *testing.T, repoName string) (c Command, changelogPath string, clea
noPrompt: newBoolPtr(true),
toStdOut: newBoolPtr(false),
file: newStrPtr(filepath.Join(tmp, repoName, dfltChangelogFile)),
version: newBoolPtr(false),
}

cleanup = func() {
Expand Down

0 comments on commit 4b5090c

Please sign in to comment.