From 5560e25e4063daaca8bbbc99f40f49f666472a8e Mon Sep 17 00:00:00 2001 From: ypjama <3822534+ypjama@users.noreply.github.com> Date: Sat, 5 Oct 2024 19:12:06 +0300 Subject: [PATCH] Update create command --- internal/pkg/conflictless/cli.go | 21 ++++++++- internal/pkg/conflictless/config.go | 10 ++++- internal/pkg/conflictless/create.go | 16 ++++--- internal/pkg/conflictless/create_test.go | 57 ++++++++++++++---------- internal/pkg/conflictless/data.go | 2 +- internal/pkg/conflictless/print.go | 6 +++ internal/pkg/conflictless/usage.go | 28 ++++++++++++ 7 files changed, 109 insertions(+), 31 deletions(-) diff --git a/internal/pkg/conflictless/cli.go b/internal/pkg/conflictless/cli.go index 6268b2f..b556eb6 100644 --- a/internal/pkg/conflictless/cli.go +++ b/internal/pkg/conflictless/cli.go @@ -29,6 +29,7 @@ func CLI() { Flags: FlagCollection{ Bump: new(string), ChangeFileFormat: new(string), + ChangeFileName: new(string), ChangelogFile: new(string), ChangeTypesCsv: new(string), Command: "", @@ -38,7 +39,7 @@ func CLI() { Bump: defaultBump, Changelog: nil, ChangelogFile: "CHANGELOG.md", - ChangesFile: "", + ChangeFile: "", ChangeTypesCsv: defaultChangeTypesCSV, ChangeFileFormat: defaultChangeFileFormat, Directory: defaultDirectory, @@ -111,7 +112,10 @@ func parseCLIFlags(cfg *Config) { cmd = flag.NewFlagSet(commandCreate, flag.ExitOnError) cmd.Usage = usageCreateOnError + defineFormatFlags(cfg, cmd) + defineCreateTypeFlags(cfg, cmd) defineDirFlags(cfg, cmd) + defineChangeFileNameFlags(cfg, cmd) } if cmd != nil { @@ -143,6 +147,21 @@ func defineDirFlags(cfg *Config, fs *flag.FlagSet) { fs.StringVar(cfg.Flags.Directory, "d", defaultDir, "") } +func defineFormatFlags(cfg *Config, fs *flag.FlagSet) { + fs.StringVar(cfg.Flags.ChangeFileFormat, "format", defaultChangeFileFormat, "") + fs.StringVar(cfg.Flags.ChangeFileFormat, "f", defaultChangeFileFormat, "") +} + +func defineCreateTypeFlags(cfg *Config, fs *flag.FlagSet) { + fs.StringVar(cfg.Flags.ChangeTypesCsv, "types", defaultChangeTypesCSV, "") + fs.StringVar(cfg.Flags.ChangeTypesCsv, "t", defaultChangeTypesCSV, "") +} + +func defineChangeFileNameFlags(cfg *Config, fs *flag.FlagSet) { + fs.StringVar(cfg.Flags.ChangeFileName, "name", "", "") + fs.StringVar(cfg.Flags.ChangeFileName, "n", "", "") +} + func defineSkipFlags(cfg *Config, fs *flag.FlagSet) { fs.BoolVar(&cfg.Flags.SkipVersionLinks, "skip-version-links", false, "") fs.BoolVar(&cfg.Flags.SkipVersionLinks, "s", false, "") diff --git a/internal/pkg/conflictless/config.go b/internal/pkg/conflictless/config.go index 1c0e05d..4456dc2 100644 --- a/internal/pkg/conflictless/config.go +++ b/internal/pkg/conflictless/config.go @@ -6,6 +6,7 @@ import "fmt" type FlagCollection struct { Bump *string ChangeFileFormat *string + ChangeFileName *string ChangelogFile *string ChangeTypesCsv *string Command string @@ -19,7 +20,7 @@ type Config struct { ChangeFileFormat string Changelog *Changelog ChangelogFile string - ChangesFile string + ChangeFile string ChangeTypesCsv string Directory string Flags FlagCollection @@ -37,6 +38,7 @@ func (cfg *Config) SetGenerateConfigsFromFlags() error { func (cfg *Config) SetCreateConfigsFromFlags() error { cfg.SetChangeTypesFromFlags() cfg.SetDirectoryFromFlags() + cfg.SetChangeFileFromFlags() return cfg.SetChangeFileFormatFromFlags() } @@ -85,6 +87,12 @@ func (cfg *Config) SetChangeTypesFromFlags() { } } +func (cfg *Config) SetChangeFileFromFlags() { + if cfg.Flags.ChangeFileName != nil { + cfg.ChangeFile = *cfg.Flags.ChangeFileName + } +} + func (cfg *Config) SetChangeFileFormatFromFlags() error { if cfg.Flags.ChangeFileFormat == nil { return nil diff --git a/internal/pkg/conflictless/create.go b/internal/pkg/conflictless/create.go index 385dbce..d963fa9 100644 --- a/internal/pkg/conflictless/create.go +++ b/internal/pkg/conflictless/create.go @@ -7,15 +7,21 @@ func Create(cfg *Config) { PrintErrorAndExit(err.Error(), usageCreateOnError) } - filename, err := ParseCurrentGitBranchAsFilename(cfg) - if err != nil { - PrintErrorAndExit(err.Error(), usageCreateOnError) - } + if cfg.ChangeFile == "" { + filename, err := ParseCurrentGitBranchAsFilename(cfg) + if err != nil { + PrintErrorAndExit(err.Error(), usageCreateOnError) + } - cfg.ChangesFile = filename + cfg.ChangeFile = filename + } else { + cfg.ChangeFile += "." + cfg.ChangeFileFormat + } err = createChangeFile(cfg) if err != nil { PrintErrorAndExit(err.Error(), usageCreateOnError) } + + PrintCreateSuccess(cfg) } diff --git a/internal/pkg/conflictless/create_test.go b/internal/pkg/conflictless/create_test.go index b8d0032..3382ef7 100644 --- a/internal/pkg/conflictless/create_test.go +++ b/internal/pkg/conflictless/create_test.go @@ -16,31 +16,27 @@ type createTestCase struct { changeTypesCSV string contains []string notContains []string + name *string } func testCasesForCreate(t *testing.T) []createTestCase { t.Helper() + name := "loving-ladybird" + return []createTestCase{ { description: "yml_format", + name: nil, format: "yml", branchName: "foo-bar-baz", changeTypesCSV: "changed", - contains: []string{ - "---", - "changed:\n -", - }, - notContains: []string{ - "added", - "deprecated", - "removed", - "fixed", - "security", - }, + contains: []string{"---", "changed:\n -"}, + notContains: []string{"added", "deprecated", "removed", "fixed", "security"}, }, { description: "yaml_format", + name: nil, format: "yaml", branchName: "123-create-and-fix-stuff", changeTypesCSV: "security,fixed,added", @@ -50,14 +46,11 @@ func testCasesForCreate(t *testing.T) []createTestCase { "fixed:\n -", "security:\n -", }, - notContains: []string{ - "changed", - "deprecated", - "removed", - }, + notContains: []string{"changed", "deprecated", "removed"}, }, { description: "json_format", + name: nil, format: "json", branchName: "changing-deprecating-and-removing", changeTypesCSV: "changed,deprecated,removed", @@ -68,16 +61,27 @@ func testCasesForCreate(t *testing.T) []createTestCase { "\n \"deprecated\": [\n \"\"\n ]", "\n \"removed\": [\n \"\"\n ]", }, - notContains: []string{ - "added", - "fixed", - "security", - }, + notContains: []string{"added", "fixed", "security"}, + }, + { + description: "name_given", + name: &name, + format: "yml", + branchName: "", + changeTypesCSV: "added", + contains: []string{"---", "added:\n -"}, + notContains: []string{"changed", "deprecated", "removed", "fixed", "security"}, }, } } -func setupCreate(t *testing.T, headFileContents, format, changeTypesCSV string) (string, string, *conflictless.Config) { +func setupCreate( + t *testing.T, + headFileContents, + format, + changeTypesCSV string, + name *string, +) (string, string, *conflictless.Config) { t.Helper() changesDir, err := os.MkdirTemp(os.TempDir(), "changes") @@ -91,6 +95,7 @@ func setupCreate(t *testing.T, headFileContents, format, changeTypesCSV string) cfg.Flags.ChangeTypesCsv = &changeTypesCSV cfg.Flags.Directory = &changesDir cfg.Flags.ChangeFileFormat = &format + cfg.Flags.ChangeFileName = name return changesDir, gitHeadFile.Name(), cfg } @@ -107,13 +112,19 @@ func TestCreate(t *testing.T) { `ref: refs/heads/`+testCase.branchName, testCase.format, testCase.changeTypesCSV, + testCase.name, ) defer os.RemoveAll(changesDir) defer os.Remove(gitHeadFile) conflictless.Create(cfg) - expectedName := filepath.Join(changesDir, testCase.branchName+"."+testCase.format) + filename := testCase.branchName + "." + testCase.format + if testCase.name != nil { + filename = *testCase.name + "." + testCase.format + } + + expectedName := filepath.Join(changesDir, filename) file, err := os.Stat(expectedName) assert.NoError(t, err) diff --git a/internal/pkg/conflictless/data.go b/internal/pkg/conflictless/data.go index 9942470..d94f0bd 100644 --- a/internal/pkg/conflictless/data.go +++ b/internal/pkg/conflictless/data.go @@ -185,7 +185,7 @@ func createChangeFile(cfg *Config) error { return err } - name := filepath.Join(dir, cfg.ChangesFile) + name := filepath.Join(dir, cfg.ChangeFile) if _, err := os.Stat(name); err == nil { return fmt.Errorf("%w: %s", ErrFileAlreadyExists, name) diff --git a/internal/pkg/conflictless/print.go b/internal/pkg/conflictless/print.go index 1a0ff67..e372690 100644 --- a/internal/pkg/conflictless/print.go +++ b/internal/pkg/conflictless/print.go @@ -3,6 +3,7 @@ package conflictless import ( "fmt" "os" + "path/filepath" ) // PrintUsageAndExit prints the usage and exits. @@ -52,3 +53,8 @@ func PrintCheckSuccess(noContent bool) { //nolint:forbidigo fmt.Println(msg) } + +func PrintCreateSuccess(cfg *Config) { + //nolint:forbidigo + fmt.Printf("Created new change-file '%s' successfully!\n", filepath.Join(cfg.Directory, cfg.ChangeFile)) +} diff --git a/internal/pkg/conflictless/usage.go b/internal/pkg/conflictless/usage.go index d15fb85..10adf6a 100644 --- a/internal/pkg/conflictless/usage.go +++ b/internal/pkg/conflictless/usage.go @@ -16,6 +16,27 @@ const ( "-d, --dir\n" + flagDescriptionIndentation + "Directory where to look for change-files (default: changes)" + flagDescriptionFormat = flagIndentation + + "-f, --format\n" + + flagDescriptionIndentation + + "File format and extension yml/yaml/json for the change-file (default: yml)" + flagDescriptionTypes = flagIndentation + + "-t, --types\n" + + flagDescriptionIndentation + + "Types of changes you want for the change-file (default: changed)\n\n" + + flagDescriptionIndentation + + "Multiple values can be given by separating values with commas.\n" + + flagDescriptionIndentation + + "Example: '--format added,changed,deprecated,removed,fixed,security'." + + flagDescriptionIndentation + flagDescriptionNameForCreate = flagIndentation + + "-n, --name\n" + + flagDescriptionIndentation + + "Name for the change-file without file extension\n\n" + + flagDescriptionIndentation + + "If this flag is not given the name will be derived from the name of the\n" + + flagDescriptionIndentation + + "current git branch you're on." flagDescriptionDirForCreate = flagIndentation + "-d, --dir\n" + flagDescriptionIndentation + @@ -36,6 +57,7 @@ func usageText() string { The commands are: check Checks that change-files are valid + create Creates a new change-file generate Generates a version entry to changelog file help Prints this help message @@ -77,9 +99,15 @@ func usageTextForCreate() string { The flags are: +%s +%s +%s %s `, flagDescriptionDirForCreate, + flagDescriptionFormat, + flagDescriptionTypes, + flagDescriptionNameForCreate, ) }