Skip to content

Commit

Permalink
Add flag for overriding default changelog file
Browse files Browse the repository at this point in the history
  • Loading branch information
ypjama committed Oct 19, 2023
1 parent 2ce5078 commit 2b4f855
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions changes/generate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
added:
- 'New flag for "generate" command: "-c, --changelog" for overriding the default changelog file name'
4 changes: 3 additions & 1 deletion internal/pkg/conflictless/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {})
}
Expand Down
12 changes: 11 additions & 1 deletion internal/pkg/conflictless/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func CLI() {
cfg := Config{
Flags: FlagCollection{
Bump: new(string),
ChangelogFile: new(string),
Command: "",
Directory: new(string),
SkipVersionLinks: false,
Expand All @@ -32,6 +33,7 @@ func CLI() {
ChangelogFile: "CHANGELOG.md",
RepositoryConfigFile: ".git/config",
Changelog: nil,
Directory: "changes",
}
parseCLIFlags(&cfg)

Expand All @@ -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)
}
Expand Down Expand Up @@ -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:
Expand All @@ -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"

Expand Down
56 changes: 56 additions & 0 deletions internal/pkg/conflictless/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
37 changes: 34 additions & 3 deletions internal/pkg/conflictless/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -16,20 +17,50 @@ 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":
cfg.Bump = BumpMinor
case "major":
cfg.Bump = BumpMajor
default:
return fmt.Errorf("%w: %s", ErrInvalidBumpFlag, *cfg.Flags.Bump)
return fmt.Errorf("%w: %s", ErrInvalidBumpFlag, bumpFlag)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/conflictless/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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() {})
}
Expand All @@ -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() {})
}
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/conflictless/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"os"
)

func help() {
// Help prints the help message and exits.
func Help() {
var topic string

args := argsWithoutTestFlags()
Expand Down
8 changes: 7 additions & 1 deletion internal/pkg/conflictless/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down Expand Up @@ -44,8 +48,10 @@ The flags are:
%s
%s
%s
%s
`,
flagDescriptionBump,
flagDescriptionChangelog,
flagDescriptionDir,
flagDescriptionSkipVersionLinks,
)
Expand Down

0 comments on commit 2b4f855

Please sign in to comment.