From 2b4f8554b207868fc4e60083c69dc48387fceecf Mon Sep 17 00:00:00 2001 From: ypjama <3822534+ypjama@users.noreply.github.com> Date: Thu, 19 Oct 2023 22:06:20 +0300 Subject: [PATCH] Add flag for overriding default changelog file --- README.md | 2 + changes/generate.yml | 2 + internal/pkg/conflictless/check.go | 4 +- internal/pkg/conflictless/cli.go | 12 +++++- internal/pkg/conflictless/cli_test.go | 56 +++++++++++++++++++++++++++ internal/pkg/conflictless/config.go | 37 ++++++++++++++++-- internal/pkg/conflictless/generate.go | 6 +-- internal/pkg/conflictless/help.go | 3 +- internal/pkg/conflictless/usage.go | 8 +++- 9 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 changes/generate.yml diff --git a/README.md b/README.md index 7b9c99a..c29b35e 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,8 @@ The flags are: -b, --bump Bump version patch/minor/major (default: minor) + -c, --changelog + Changelog file (default: CHANGELOG.md) -d, --dir Directory where to look for change-files (default: changes) -s, --skip-version-links diff --git a/changes/generate.yml b/changes/generate.yml new file mode 100644 index 0000000..5593527 --- /dev/null +++ b/changes/generate.yml @@ -0,0 +1,2 @@ +added: + - 'New flag for "generate" command: "-c, --changelog" for overriding the default changelog file name' diff --git a/internal/pkg/conflictless/check.go b/internal/pkg/conflictless/check.go index 427ca83..b7a52cb 100644 --- a/internal/pkg/conflictless/check.go +++ b/internal/pkg/conflictless/check.go @@ -2,7 +2,9 @@ package conflictless // Check checks the validity of the change files. func Check(cfg *Config) { - combined, err := scanDir(*cfg.Flags.Directory) + cfg.SetCheckConfigsFromFlags() + + combined, err := scanDir(cfg.Directory) if err != nil { PrintErrorAndExit(err.Error(), func() {}) } diff --git a/internal/pkg/conflictless/cli.go b/internal/pkg/conflictless/cli.go index 2479e19..2f42ff6 100644 --- a/internal/pkg/conflictless/cli.go +++ b/internal/pkg/conflictless/cli.go @@ -24,6 +24,7 @@ func CLI() { cfg := Config{ Flags: FlagCollection{ Bump: new(string), + ChangelogFile: new(string), Command: "", Directory: new(string), SkipVersionLinks: false, @@ -32,6 +33,7 @@ func CLI() { ChangelogFile: "CHANGELOG.md", RepositoryConfigFile: ".git/config", Changelog: nil, + Directory: "changes", } parseCLIFlags(&cfg) @@ -45,7 +47,7 @@ func CLI() { case commandGen: Generate(&cfg) case commandHelp: - help() + Help() default: PrintErrorAndExit(fmt.Sprintf("invalid command: '%s'", cfg.Flags.Command), usageOnError) } @@ -85,6 +87,7 @@ func parseCLIFlags(cfg *Config) { cmd.Usage = usageGenerateOnError defineBumpFlags(cfg, cmd) + defineChangeLogFlags(cfg, cmd) defineDirFlags(cfg, cmd) defineSkipFlags(cfg, cmd) case commandCheck: @@ -102,6 +105,13 @@ func parseCLIFlags(cfg *Config) { } } +func defineChangeLogFlags(cfg *Config, fs *flag.FlagSet) { + defaultChangelogFile := "CHANGELOG.md" + + fs.StringVar(cfg.Flags.ChangelogFile, "changelog", defaultChangelogFile, "") + fs.StringVar(cfg.Flags.ChangelogFile, "c", defaultChangelogFile, "") +} + func defineBumpFlags(cfg *Config, fs *flag.FlagSet) { defaultBumpStr := "minor" diff --git a/internal/pkg/conflictless/cli_test.go b/internal/pkg/conflictless/cli_test.go index 26c6923..f4b877f 100644 --- a/internal/pkg/conflictless/cli_test.go +++ b/internal/pkg/conflictless/cli_test.go @@ -157,3 +157,59 @@ func TestCLIHelp(t *testing.T) { }) } } + +func TestCLIGenerateWithInvalidFlags(t *testing.T) { + t.Parallel() + + if os.Getenv("TEST_CLI_GENERATE_INVALID_FLAGS") != "" { + conflictless.CLI() + + return + } + + stdoutFile := createTempFile(t, os.TempDir(), "test-cli-generate-with-invalid-flags-stdout") + defer os.Remove(stdoutFile.Name()) + + stderrFile := createTempFile(t, os.TempDir(), "test-cli-generate-with-invalid-flags-stderr") + defer os.Remove(stderrFile.Name()) + + //nolint:gosec // this is a test package so G204 doesn't really matter here. + cmd := exec.Command( + os.Args[0], + "-test.run=^TestCLIGenerateWithInvalidFlags$", + "generate", + "--dir", + "rhymenocerous", + "--changelog", + "HIPPOPOTAMUS.md", + "--bump", + "steve", + ) + + cmd.Stdout = stdoutFile + cmd.Stderr = stderrFile + + cmd.Env = append(os.Environ(), "TEST_CLI_GENERATE_INVALID_FLAGS=1") + err := cmd.Run() + + assert.Error(t, err) + + assert.IsType(t, new(exec.ExitError), err) + + exitErr := new(*exec.ExitError) + errors.As(err, exitErr) + + expectedCode := 2 + exitCode := (*exitErr).ExitCode() + + assert.Equal(t, expectedCode, exitCode, "process exited with %d, want exit status %d", expectedCode, exitCode) + + stdoutData, err := os.ReadFile(stdoutFile.Name()) + assert.NoError(t, err) + + stderrData, err := os.ReadFile(stderrFile.Name()) + assert.NoError(t, err) + + assert.Empty(t, string(stdoutData)) + assert.NotEmpty(t, string(stderrData)) +} diff --git a/internal/pkg/conflictless/config.go b/internal/pkg/conflictless/config.go index f6b60d0..9e48469 100644 --- a/internal/pkg/conflictless/config.go +++ b/internal/pkg/conflictless/config.go @@ -5,8 +5,9 @@ import "fmt" // FlagCollection is a collection of flags. type FlagCollection struct { Bump *string - Directory *string + ChangelogFile *string Command string + Directory *string SkipVersionLinks bool } @@ -16,12 +17,42 @@ type Config struct { Bump Bump ChangelogFile string RepositoryConfigFile string + Directory string Changelog *Changelog } +func (cfg *Config) SetGenerateConfigsFromFlags() error { + cfg.SetChangelogFileFromFlags() + cfg.SetDirectoryFromFlags() + + return cfg.SetBumpFromFlags() +} + +func (cfg *Config) SetCheckConfigsFromFlags() { + cfg.SetDirectoryFromFlags() +} + +func (cfg *Config) SetDirectoryFromFlags() { + if cfg.Flags.Directory != nil { + cfg.Directory = *cfg.Flags.Directory + } +} + +func (cfg *Config) SetChangelogFileFromFlags() { + if cfg.Flags.ChangelogFile != nil { + cfg.ChangelogFile = *cfg.Flags.ChangelogFile + } +} + // SetBumpFromFlags sets the bump type by parsing the flag string. func (cfg *Config) SetBumpFromFlags() error { - switch *cfg.Flags.Bump { + bumpFlag := "" + + if cfg.Flags.Bump != nil { + bumpFlag = *cfg.Flags.Bump + } + + switch bumpFlag { case "patch": cfg.Bump = BumpPatch case "minor": @@ -29,7 +60,7 @@ func (cfg *Config) SetBumpFromFlags() error { case "major": cfg.Bump = BumpMajor default: - return fmt.Errorf("%w: %s", ErrInvalidBumpFlag, *cfg.Flags.Bump) + return fmt.Errorf("%w: %s", ErrInvalidBumpFlag, bumpFlag) } return nil diff --git a/internal/pkg/conflictless/generate.go b/internal/pkg/conflictless/generate.go index 372efa3..e6dda7b 100644 --- a/internal/pkg/conflictless/generate.go +++ b/internal/pkg/conflictless/generate.go @@ -10,7 +10,7 @@ import ( // Generate generates a new version section in the changelog. func Generate(cfg *Config) { - err := cfg.SetBumpFromFlags() + err := cfg.SetGenerateConfigsFromFlags() if err != nil { PrintErrorAndExit(err.Error(), usageGenerateOnError) } @@ -20,7 +20,7 @@ func Generate(cfg *Config) { PrintErrorAndExit(err.Error(), func() {}) } - combined, err := scanDir(*cfg.Flags.Directory) + combined, err := scanDir(cfg.Directory) if err != nil { PrintErrorAndExit(err.Error(), func() {}) } @@ -39,7 +39,7 @@ func Generate(cfg *Config) { PrintErrorAndExit(err.Error(), func() {}) } - err = removeChangeFiles(*cfg.Flags.Directory) + err = removeChangeFiles(cfg.Directory) if err != nil { PrintErrorAndExit(err.Error(), func() {}) } diff --git a/internal/pkg/conflictless/help.go b/internal/pkg/conflictless/help.go index 269591b..864ed43 100644 --- a/internal/pkg/conflictless/help.go +++ b/internal/pkg/conflictless/help.go @@ -5,7 +5,8 @@ import ( "os" ) -func help() { +// Help prints the help message and exits. +func Help() { var topic string args := argsWithoutTestFlags() diff --git a/internal/pkg/conflictless/usage.go b/internal/pkg/conflictless/usage.go index 4ed81d4..cfb29ec 100644 --- a/internal/pkg/conflictless/usage.go +++ b/internal/pkg/conflictless/usage.go @@ -8,7 +8,11 @@ import ( const ( flagIndentation = "\t" flagDescriptionIndentation = "\t\t" - flagDescriptionDir = flagIndentation + + flagDescriptionChangelog = flagIndentation + + "-c, --changelog\n" + + flagDescriptionIndentation + + "Changelog file (default: CHANGELOG.md)" + flagDescriptionDir = flagIndentation + "-d, --dir\n" + flagDescriptionIndentation + "Directory where to look for change-files (default: changes)" @@ -44,8 +48,10 @@ The flags are: %s %s %s +%s `, flagDescriptionBump, + flagDescriptionChangelog, flagDescriptionDir, flagDescriptionSkipVersionLinks, )