template / commit
Some simplification and minor fixes:
.gitignore | 1 + config.go | 8 ++++---- name_pattern.go | 35 ++++++++++------------------------- name_pattern_test.go | 41 ++++++++++++++++++++++++++++++++++++++--- profile.go | 5 +++-- 5 files changed, 56 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..86785ba --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +template.exe diff --git a/config.go b/config.go index 89416ab..21bff55 100644 --- a/config.go +++ b/config.go @@ -58,7 +58,7 @@ func defaultConfig() Config { PostScript: []string{ "code", ".", - "{{.Path}}", + "\"{{.Path}}\"", }, }, { @@ -76,7 +76,7 @@ func defaultConfig() Config { ScriptShell: "pwsh", PostScript: []string{ "code", - "{{.Path}}", + "\"{{.Path}}\"", }, }, { @@ -96,7 +96,7 @@ func defaultConfig() Config { PostScript: []string{ "code", ".", - "{{.Name}}", + "\"{{.Name}}\"", }, }, { @@ -113,7 +113,7 @@ func defaultConfig() Config { ScriptShell: "pwsh", PostScript: []string{ "explorer", - "{{.Path}}", + "\"{{.Path}}\"", }, }, }, diff --git a/name_pattern.go b/name_pattern.go index 2a79908..d16494a 100644 --- a/name_pattern.go +++ b/name_pattern.go @@ -14,31 +14,7 @@ type NamePatternData struct { ProjectName string } -func createNewName(now time.Time, projectName string, profile Profile, existingEntities []string) (string, error) { - patternInfo := createNamePatternData(now, projectName) - newName, err := createNewNameForProfile(profile, patternInfo, existingEntities) - if err != nil { - return "", err - } else { - return newName, nil - } -} - -func createNamePatternData(now time.Time, projectName string) NamePatternData { - patternInfo := NamePatternData{ - Year: now.Format("2006"), - Month: now.Format("01"), - Day: now.Format("02"), - Hour: now.Format("15"), - Minute: now.Format("04"), - Second: now.Format("05"), - ProjectName: projectName, - } - - return patternInfo -} - -func createNewNameForProfile(profile Profile, patternData NamePatternData, existingEntities []string) (string, error) { +func createNewNameForProfile(now time.Time, projectName string, profile Profile, existingEntities []string) (string, error) { for index, namePattern := range profile.NamePatterns { startingVersion := 1 @@ -50,6 +26,15 @@ func createNewNameForProfile(profile Profile, patternData NamePatternData, exist }, } template := template.New("template-name").Funcs(funcMap) + patternData := NamePatternData{ + Year: now.Format("2006"), + Month: now.Format("01"), + Day: now.Format("02"), + Hour: now.Format("15"), + Minute: now.Format("04"), + Second: now.Format("05"), + ProjectName: projectName, + } for { diff --git a/name_pattern_test.go b/name_pattern_test.go index 7540c4a..61aa852 100644 --- a/name_pattern_test.go +++ b/name_pattern_test.go @@ -19,13 +19,14 @@ func TestCreateNewName(t *testing.T) { "{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}-{{.Minute}}-{{.Second}}-{{.ProjectName}}.txt", }, } + newNameNoProject := "2006-01-02.txt" newName1 := "2006-01-02-test-project.txt" newName2 := "2006-01-02-15-test-project.txt" newName3 := "2006-01-02-15-04-05-test-project.txt" testHappyPath := func(projectName string, expectedName string, existingEntities []string) func(t *testing.T) { return func(t *testing.T) { - newName, err := createNewName(now, projectName, profile, existingEntities) + newName, err := createNewNameForProfile(now, projectName, profile, existingEntities) if err != nil { t.Error("Error creating new name: ", err) } @@ -36,7 +37,7 @@ func TestCreateNewName(t *testing.T) { } testErrorPath := func(projectName string, existingEntities []string) func(t *testing.T) { return func(t *testing.T) { - _, err := createNewName(now, projectName, profile, existingEntities) + _, err := createNewNameForProfile(now, projectName, profile, existingEntities) if err == nil { t.Error("Expected error but got none") } @@ -49,9 +50,43 @@ func TestCreateNewName(t *testing.T) { t.Run("createNewName - happy path", testHappyPath("test-project", newName1, []string{})) t.Run("createNewName - use second pattern", testHappyPath("test-project", newName2, []string{newName1})) t.Run("createNewName - use third pattern", testHappyPath("test-project", newName3, []string{newName1, newName2})) + t.Run("createNewName - happy path", testHappyPath("", newNameNoProject, []string{})) // error path t.Run("createNewName - error path - collision", testErrorPath("test-project", []string{newName1, newName2, newName3})) +} + +func TestCreateNewName_Index(t *testing.T) { + // Mon Jan 2 15:04:05 MST 2006 + now := time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC) + profile := Profile{ + IsDefault: true, + Type: FileType, + NamePatterns: []string{ + "{{$version := getVersion}}{{.Year}}-{{.Month}}-{{.Day}}v{{$version}}.txt", + }, + } + newName1 := "2006-01-02v1.txt" + newName2 := "2006-01-02v2.txt" + newName3 := "2006-01-02v3.txt" - // teardown code + testHappyPath := func(expectedName string, existingEntities []string) func(t *testing.T) { + return func(t *testing.T) { + newName, err := createNewNameForProfile(now, "test-project", profile, existingEntities) + if err != nil { + t.Error("Error creating new name: ", err) + } + if newName != expectedName { + t.Error("Expected name to be ", expectedName, " but got ", newName) + } + } + } + + // test code + + // happy path + t.Run("createNewName - v1", testHappyPath(newName1, []string{})) + t.Run("createNewName - v2", testHappyPath(newName2, []string{newName1})) + t.Run("createNewName - v3", testHappyPath(newName3, []string{newName1, newName2})) + t.Run("createNewName - v2", testHappyPath(newName2, []string{newName1, newName3})) } diff --git a/profile.go b/profile.go index d70b936..8bc19d8 100644 --- a/profile.go +++ b/profile.go @@ -90,7 +90,7 @@ func processProfile(profile Profile, projectName string) error { // generate a new name based on time, profile, and existing entities now := time.Now() - newName, err := createNewName(now, projectName, profile, entities) + newName, err := createNewNameForProfile(now, projectName, profile, entities) if err != nil { return err } @@ -128,8 +128,9 @@ func processProfile(profile Profile, projectName string) error { return err } if postScripts != nil && len(postScripts) > 0 { - allArgs := append([]string{"/C"}, postScripts...) + allArgs := append([]string{"-c"}, postScripts...) shell := profile.ScriptShell + fmt.Printf("Executing post script: %v %v\n", shell, strings.Join(allArgs, " ")) cmd := exec.Command(shell, allArgs...) if err := cmd.Run(); err != nil { // post scripts tend to fail (TODO: investigate why), so for now we'll just log the error