Skip to content

Commit

Permalink
Update create command
Browse files Browse the repository at this point in the history
  • Loading branch information
ypjama committed Oct 5, 2024
1 parent 6e69432 commit 5560e25
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 31 deletions.
21 changes: 20 additions & 1 deletion internal/pkg/conflictless/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func CLI() {
Flags: FlagCollection{
Bump: new(string),
ChangeFileFormat: new(string),
ChangeFileName: new(string),
ChangelogFile: new(string),
ChangeTypesCsv: new(string),
Command: "",
Expand All @@ -38,7 +39,7 @@ func CLI() {
Bump: defaultBump,
Changelog: nil,
ChangelogFile: "CHANGELOG.md",
ChangesFile: "",
ChangeFile: "",
ChangeTypesCsv: defaultChangeTypesCSV,
ChangeFileFormat: defaultChangeFileFormat,
Directory: defaultDirectory,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, "")
Expand Down
10 changes: 9 additions & 1 deletion internal/pkg/conflictless/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "fmt"
type FlagCollection struct {
Bump *string
ChangeFileFormat *string
ChangeFileName *string
ChangelogFile *string
ChangeTypesCsv *string
Command string
Expand All @@ -19,7 +20,7 @@ type Config struct {
ChangeFileFormat string
Changelog *Changelog
ChangelogFile string
ChangesFile string
ChangeFile string
ChangeTypesCsv string
Directory string
Flags FlagCollection
Expand All @@ -37,6 +38,7 @@ func (cfg *Config) SetGenerateConfigsFromFlags() error {
func (cfg *Config) SetCreateConfigsFromFlags() error {
cfg.SetChangeTypesFromFlags()
cfg.SetDirectoryFromFlags()
cfg.SetChangeFileFromFlags()

return cfg.SetChangeFileFormatFromFlags()
}
Expand Down Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions internal/pkg/conflictless/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
57 changes: 34 additions & 23 deletions internal/pkg/conflictless/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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")
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/conflictless/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions internal/pkg/conflictless/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package conflictless
import (
"fmt"
"os"
"path/filepath"
)

// PrintUsageAndExit prints the usage and exits.
Expand Down Expand Up @@ -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))
}
28 changes: 28 additions & 0 deletions internal/pkg/conflictless/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand All @@ -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
Expand Down Expand Up @@ -77,9 +99,15 @@ func usageTextForCreate() string {
The flags are:
%s
%s
%s
%s
`,
flagDescriptionDirForCreate,
flagDescriptionFormat,
flagDescriptionTypes,
flagDescriptionNameForCreate,
)
}

Expand Down

0 comments on commit 5560e25

Please sign in to comment.