fuzzyworld

template / commit

Minor reorganization:

c62570e23b9cf9f1c69c8c7c1ac7dfa36b997627 · Pavel Safronov · 2022-09-23 13:32:28 -0700 · parents 55d96f9cc510

Minor reorganization:
- put all name pattern stuff in one file
- put all profile stuff in one file
- split up the tests similarly
 name_pattern.go                     | 32 ++++++++++++++++++
 new_test.go => name_pattern_test.go | 35 --------------------
 postscript.go                       | 43 ++++++++++++++++++++++++
 new.go => profile.go                | 65 -------------------------------------
 profile_test.go                     | 40 +++++++++++++++++++++++
 5 files changed, 115 insertions(+), 100 deletions(-)

diff --git a/name_pattern.go b/name_pattern.go
new file mode 100644
index 0000000..064c521
--- /dev/null
+++ b/name_pattern.go
@@ -0,0 +1,32 @@
+package main
+
+import "time"
+
+type NamePatternData struct {
+	Year, Month, Day, Hour, Minute, Second string
+	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
+}
diff --git a/new_test.go b/name_pattern_test.go
similarity index 68%
rename from new_test.go
rename to name_pattern_test.go
index c383e76..7540c4a 100644
--- a/new_test.go
+++ b/name_pattern_test.go
@@ -5,41 +5,6 @@ import (
 	"time"
 )
 
-func TestFindProfile(t *testing.T) {
-	config := Config{
-		Profiles: []Profile{
-			{
-				Name: "daily",
-			},
-			{
-				IsDefault: true,
-				Name:      "notes",
-			},
-		},
-	}
-	testSuccess := func(profileName string, expectedProfileName string) {
-		profile, err := findProfile(config, profileName)
-		if err != nil {
-			t.Errorf("Failed to find profile '%v': %v", profileName, err)
-		}
-		if profile.Name != expectedProfileName {
-			t.Errorf("Asked for profile '%v', expected '%v', got '%v'", profileName, expectedProfileName, profile.Name)
-		}
-	}
-	testFailure := func(profileName string) {
-		_, err := findProfile(config, profileName)
-		if err == nil {
-			t.Errorf("Expected to fail to find profile '%v', but succeeded", profileName)
-		}
-	}
-
-	testSuccess("", "notes")
-	testSuccess("daily", "daily")
-	testSuccess("notes", "notes")
-
-	testFailure("non-existent")
-}
-
 func TestCreateNewName(t *testing.T) {
 	// setup code
 
diff --git a/postscript.go b/postscript.go
new file mode 100644
index 0000000..2f6a869
--- /dev/null
+++ b/postscript.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+	"strings"
+	"text/template"
+)
+
+type PostScriptPatternData struct {
+	Name string
+	Path string
+}
+
+func createPostScriptArray(profile Profile, newName string, fullPath string) ([]string, error) {
+	array := make([]string, 0)
+
+	if profile.PostScript == nil || len(profile.PostScript) == 0 {
+		return nil, nil
+	}
+
+	patternData := PostScriptPatternData{
+		Name: newName,
+		Path: fullPath,
+	}
+
+	for _, postScriptPattern := range profile.PostScript {
+
+		template, err := template.New("postscript").Parse(postScriptPattern)
+		if err != nil {
+			return nil, err
+		}
+
+		sb := new(strings.Builder)
+		err = template.Execute(sb, patternData)
+		if err != nil {
+			return nil, err
+		}
+
+		postScript := sb.String()
+		array = append(array, postScript)
+	}
+
+	return array, nil
+}
diff --git a/new.go b/profile.go
similarity index 74%
rename from new.go
rename to profile.go
index ad8f208..b183961 100644
--- a/new.go
+++ b/profile.go
@@ -136,39 +136,6 @@ func processProfile(profile Profile, projectName string) error {
 	return nil
 }
 
-type NamePatternData struct {
-	Year, Month, Day, Hour, Minute, Second string
-	ProjectName                            string
-}
-type PostScriptPatternData struct {
-	Name string
-	Path 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) {
 	for index, namePattern := range profile.NamePatterns {
 
@@ -220,35 +187,3 @@ func createNewNameForProfile(profile Profile, patternData NamePatternData, exist
 	}
 	return "", errors.New("Couldn't find a new name")
 }
-
-func createPostScriptArray(profile Profile, newName string, fullPath string) ([]string, error) {
-	array := make([]string, 0)
-
-	if profile.PostScript == nil || len(profile.PostScript) == 0 {
-		return nil, nil
-	}
-
-	patternData := PostScriptPatternData{
-		Name: newName,
-		Path: fullPath,
-	}
-
-	for _, postScriptPattern := range profile.PostScript {
-
-		template, err := template.New("postscript").Parse(postScriptPattern)
-		if err != nil {
-			return nil, err
-		}
-
-		sb := new(strings.Builder)
-		err = template.Execute(sb, patternData)
-		if err != nil {
-			return nil, err
-		}
-
-		postScript := sb.String()
-		array = append(array, postScript)
-	}
-
-	return array, nil
-}
diff --git a/profile_test.go b/profile_test.go
new file mode 100644
index 0000000..b79cf7a
--- /dev/null
+++ b/profile_test.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+	"testing"
+)
+
+func TestFindProfile(t *testing.T) {
+	config := Config{
+		Profiles: []Profile{
+			{
+				Name: "daily",
+			},
+			{
+				IsDefault: true,
+				Name:      "notes",
+			},
+		},
+	}
+	testSuccess := func(profileName string, expectedProfileName string) {
+		profile, err := findProfile(config, profileName)
+		if err != nil {
+			t.Errorf("Failed to find profile '%v': %v", profileName, err)
+		}
+		if profile.Name != expectedProfileName {
+			t.Errorf("Asked for profile '%v', expected '%v', got '%v'", profileName, expectedProfileName, profile.Name)
+		}
+	}
+	testFailure := func(profileName string) {
+		_, err := findProfile(config, profileName)
+		if err == nil {
+			t.Errorf("Expected to fail to find profile '%v', but succeeded", profileName)
+		}
+	}
+
+	testSuccess("", "notes")
+	testSuccess("daily", "daily")
+	testSuccess("notes", "notes")
+
+	testFailure("non-existent")
+}