diff --git a/cmd/get_commits.go b/cmd/get_commits.go index 0ce0589..783b376 100644 --- a/cmd/get_commits.go +++ b/cmd/get_commits.go @@ -27,7 +27,7 @@ func getCommits(repo *history.Git) ([]plumbing.Hash, error) { return nil, err } - commits := make([]plumbing.Hash, 0) + var commits []plumbing.Hash // If previous tag is not available, or both tags are the same, // provide empty hash so that all commits are iterated. diff --git a/cmd/publish.go b/cmd/publish.go index 0de2e97..0c66b83 100644 --- a/cmd/publish.go +++ b/cmd/publish.go @@ -30,6 +30,11 @@ var publishCmd = &cobra.Command{ debug = true } + dryRun := false + if cmd.Flag("dry-run").Value.String() == "true" { + dryRun = true + } + repo, err := history.OpenGit(".", debug) if err != nil { @@ -60,6 +65,23 @@ var publishCmd = &cobra.Command{ ) } + sections := text.SplitSections(parsedCommits) + + releaseNotes := text.ReleaseNotes{ + Simple: simpleOutput, + } + + content := releaseNotes.Generate(sections, dryRun) + + if err != nil { + return err + } + + if dryRun { + fmt.Println("my job here is done...") + return nil + } + viper.AutomaticEnv() var releaseService *releaser.Releaser @@ -107,13 +129,7 @@ var publishCmd = &cobra.Command{ return errors.New("Missing release service, please consult documentation on required env vars") } - sections := text.SplitSections(parsedCommits) - - releaseNotes := text.ReleaseNotes{ - Simple: simpleOutput, - } - - err = releaseService.Release(releaseNotes.Generate(sections)) + err = releaseService.Release(content) if err != nil { return err diff --git a/cmd/root.go b/cmd/root.go index 7ff3226..496708b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -20,8 +20,12 @@ var rootCmd = &cobra.Command{ // Verbose is used to allow verbose/debug output for any given command var Verbose bool +// DryRun is used for showing the changelog without actually publishing it +var DryRun bool + func init() { rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output") + rootCmd.PersistentFlags().BoolVarP(&DryRun, "dry-run", "d", false, "dry run") } // Execute just executes the rootCmd for Cobra diff --git a/internal/releaser/releaser.go b/internal/releaser/releaser.go index 4d64415..1cf1940 100644 --- a/internal/releaser/releaser.go +++ b/internal/releaser/releaser.go @@ -22,7 +22,6 @@ type Releaser struct { // Options are used to initialize releaser type Options struct { Provider Provider - DryRun bool Token string Owner string Repo string diff --git a/internal/text/release_notes.go b/internal/text/release_notes.go index 8b4d470..adee478 100644 --- a/internal/text/release_notes.go +++ b/internal/text/release_notes.go @@ -1,6 +1,7 @@ package text import ( + "fmt" "strings" ) @@ -10,7 +11,7 @@ type ReleaseNotes struct { } // Generate generates the output mentioned in the expected-output.md -func (r *ReleaseNotes) Generate(sections map[string][]Commit) string { +func (r *ReleaseNotes) Generate(sections map[string][]Commit, dryRun bool) string { builder := strings.Builder{} // Extra lines at the start to make sure formatting starts correctly builder.WriteString("\n\n") @@ -31,5 +32,9 @@ func (r *ReleaseNotes) Generate(sections map[string][]Commit) string { builder.WriteString(r.buildSection("others", sections["others"])) } + if dryRun { + fmt.Print(builder.String()) + } + return builder.String() } diff --git a/internal/text/release_notes_test.go b/internal/text/release_notes_test.go index d2d3092..55eb9a6 100644 --- a/internal/text/release_notes_test.go +++ b/internal/text/release_notes_test.go @@ -29,7 +29,7 @@ func TestReleaseNotes(t *testing.T) { "others": []Commit{Commit{Category: "other", Scope: "", Heading: "merge master in something"}, Commit{Category: "bs", Scope: "", Heading: "random"}}, } - releaseNotes := notes.Generate(sections) + releaseNotes := notes.Generate(sections, false) assert.Equal(t, expected+"\n", "test heading"+releaseNotes) } @@ -55,7 +55,7 @@ func TestReleaseNotesSimple(t *testing.T) { "others": []Commit{Commit{Category: "other", Scope: "", Heading: "merge master in something"}, Commit{Category: "bs", Scope: "", Heading: "random"}}, } - releaseNotes := notes.Generate(sections) + releaseNotes := notes.Generate(sections, false) assert.Equal(t, expected+"\n", "test heading"+releaseNotes) } @@ -68,7 +68,7 @@ func TestReleaseNotesWithMissingSections(t *testing.T) { "features": []Commit{Commit{Heading: "ci test"}}, } - releaseNotes := notes.Generate(sections) + releaseNotes := notes.Generate(sections, false) assert.Equal(t, expected, releaseNotes) }