feat: add dry run (#10)

* feat: add --dry-run flag

* feat: use pkg/diff to actually compare old and new changelog

Co-authored-by: Tran Hau <ngtranhau@gmail.com>
main
sudo pacman -Syu 2021-04-14 14:31:00 +07:00 committed by GitHub
parent 20fa3205bf
commit c495ff942c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 0 deletions

1
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/google/go-cmp v0.5.5 // indirect
github.com/haunt98/color v0.1.0
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sebdah/goldie/v2 v2.5.3
github.com/sergi/go-diff v1.2.0 // indirect

2
go.sum
View File

@ -60,6 +60,8 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

View File

@ -27,6 +27,7 @@ type action struct {
output string
filename string
filetype string
dryRun bool
}
}
@ -49,6 +50,7 @@ func (a *action) getFlags(c *cli.Context) {
a.flags.output = a.getFlagValue(c, outputFlag, defaultOutput)
a.flags.filename = a.getFlagValue(c, filenameFlag, defaultFilename)
a.flags.filetype = a.getFlagValue(c, filetypeFlag, defaultFiletype)
a.flags.dryRun = c.Bool(dryRunFlag)
if a.flags.verbose {
a.log("flags %+v", a.flags)

View File

@ -10,6 +10,8 @@ import (
"github.com/haunt98/changeloguru/pkg/changelog"
"github.com/haunt98/changeloguru/pkg/convention"
"github.com/haunt98/changeloguru/pkg/git"
"github.com/haunt98/changeloguru/pkg/markdown"
"github.com/pkg/diff"
"github.com/urfave/cli/v2"
"golang.org/x/mod/semver"
)
@ -112,6 +114,16 @@ func (a *action) generateMarkdownChangelog(output, version string, commits []con
markdownGenerator := changelog.NewMarkdownGenerator(oldData, version, time.Now())
newData := markdownGenerator.Generate(commits, a.flags.scopes)
if a.flags.dryRun {
oldLines := strings.Split(oldData, string(markdown.NewlineToken))
newLines := strings.Split(newData, string(markdown.NewlineToken))
if err := diff.Slices("old", "new", oldLines, newLines, os.Stdout); err != nil {
return fmt.Errorf("failed to diff old and new data: %w", err)
}
return nil
}
if err := os.WriteFile(output, []byte(newData), 0o644); err != nil {
return fmt.Errorf("failed to write file %s: %w", output, err)
}

View File

@ -14,6 +14,7 @@ const (
outputFlag = "output"
filenameFlag = "filename"
filetypeFlag = "filetype"
dryRunFlag = "dry-run"
verboseFlag = "verbose"
// commands
@ -82,6 +83,10 @@ func NewApp() *cli.App {
Usage: "output `FILETYPE`",
DefaultText: defaultFiletype,
},
&cli.BoolFlag{
Name: dryRunFlag,
Usage: "demo run without actually changing anything",
},
},
Action: a.RunGenerate,
},