diff --git a/go.mod b/go.mod index 452c43c..7d3abf6 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 617aac5..367d886 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/cli/action.go b/pkg/cli/action.go index c06aaba..b6239fe 100644 --- a/pkg/cli/action.go +++ b/pkg/cli/action.go @@ -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) diff --git a/pkg/cli/action_generate.go b/pkg/cli/action_generate.go index db4436c..b4fae53 100644 --- a/pkg/cli/action_generate.go +++ b/pkg/cli/action_generate.go @@ -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) } diff --git a/pkg/cli/app.go b/pkg/cli/app.go index 3724e55..57fc5d3 100644 --- a/pkg/cli/app.go +++ b/pkg/cli/app.go @@ -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, },