fuzzyworld

template / commit

Starting work on creating files inside a templated folder.

b0586433b70ba90381b052ec7af5552365f6a149 · Pavel Safronov · 2022-10-21 00:48:35 -0700 · parents ad5348d57ac9

Starting work on creating files inside a templated folder.
This commit gets all tests working as well.
 config.go         | 10 ++++++----
 config_test.go    | 16 +++++++++++-----
 default_config.go | 27 ++++++++++++++++++---------
 extras_test.go    |  5 +++--
 profile.go        | 49 ++++++++++++++++++++++++++++---------------------
 profile_test.go   | 44 +++++++++++++++++++++++++++-----------------
 6 files changed, 93 insertions(+), 58 deletions(-)

diff --git a/config.go b/config.go
index 8058844..7438b09 100644
--- a/config.go
+++ b/config.go
@@ -12,23 +12,25 @@ type Config struct {
 	Profiles []Profile
 }
 
-func validateConfig(config Config) error {
+func validateConfig(config *Config) error {
 	if config.Profiles == nil || len(config.Profiles) == 0 {
 		return fmt.Errorf("no profiles found")
 	}
 
 	foundDefault := false
-	for _, profile := range config.Profiles {
+	for index, profile := range config.Profiles {
+		profile := profile
 		if profile.IsDefault {
 			if foundDefault {
 				return fmt.Errorf("multiple default profiles found")
 			}
 			foundDefault = true
 		}
-		error := validateProfile(profile)
+		error := validateProfile(&profile)
 		if error != nil {
 			return fmt.Errorf("error validating profile %s: %s", profile.Name, error)
 		}
+		config.Profiles[index] = profile
 	}
 	return nil
 }
@@ -90,7 +92,7 @@ func getOrCreateConfig(configFilePath string, usr user.User) (Config, error) {
 	}
 
 	// validate config
-	err = validateConfig(config)
+	err = validateConfig(&config)
 	if err != nil {
 		return emptyConfig, fmt.Errorf("error validating config: %v", err)
 	}
diff --git a/config_test.go b/config_test.go
index 59a30ff..c214434 100644
--- a/config_test.go
+++ b/config_test.go
@@ -86,7 +86,7 @@ func TestGetOrCreateConfig(t *testing.T) {
 
 func TestValidateDefaultConfig(t *testing.T) {
 	config := defaultConfig()
-	err := validateConfig(config)
+	err := validateConfig(&config)
 	if err != nil {
 		t.Errorf("Expected to validate config, but failed: %v", err)
 	}
@@ -94,7 +94,7 @@ func TestValidateDefaultConfig(t *testing.T) {
 
 func TestValidateConfig(t *testing.T) {
 	testFailure := func(config Config, errorText string) {
-		err := validateConfig(config)
+		err := validateConfig(&config)
 		if err == nil {
 			t.Errorf("Expected to fail to validate config, but succeeded")
 		}
@@ -108,7 +108,9 @@ func TestValidateConfig(t *testing.T) {
 			Name:         "daily",
 			IsDefault:    true,
 			NamePatterns: []string{"daily"},
-			FileTemplate: "foo",
+			File: File{
+				Template: "foo",
+			},
 		},
 	}}, "error validating profile")
 	testFailure(Config{Profiles: []Profile{
@@ -117,14 +119,18 @@ func TestValidateConfig(t *testing.T) {
 			IsDefault:    true,
 			NamePatterns: []string{"daily"},
 			Type:         FileType,
-			FileTemplate: "foo",
+			File: File{
+				Template: "foo",
+			},
 		},
 		{
 			Name:         "daily2",
 			IsDefault:    true,
 			NamePatterns: []string{"daily"},
 			Type:         FileType,
-			FileTemplate: "foo",
+			File: File{
+				Template: "foo",
+			},
 		},
 	}}, "multiple default profiles found")
 }
diff --git a/default_config.go b/default_config.go
index c6ab10d..0de3dfc 100644
--- a/default_config.go
+++ b/default_config.go
@@ -1,7 +1,7 @@
 package main
 
 func defaultConfig() Config {
-	return Config{
+	config := Config{
 		Profiles: []Profile{
 			{
 				Name:         "daily",
@@ -9,7 +9,8 @@ func defaultConfig() Config {
 				Path:         "~/Dropbox/DailyNotes",
 				Type:         FileType,
 				NamePatterns: []string{"{{.Year}}-{{.Month}}-{{.Day}}.md"},
-				FileTemplate: `# {{.Year}}-{{.Month}}-{{.Day}}
+				File: File{
+					Template: `# {{.Year}}-{{.Month}}-{{.Day}}
 
 Today's notes
 
@@ -20,6 +21,7 @@ Today's notes
 3. [ ] Check calendar
 4. [ ] Check yesterday's notes
 5. [ ] Have fun!`,
+				},
 				ScriptShell: "pwsh",
 				PostScripts: []string{
 					"code . \"{{.Path}}\"",
@@ -45,12 +47,14 @@ Today's notes
 				},
 			},
 			{
-				Name:             "blog",
-				Description:      "Creates a MD file in the ~/Dropbox/Blog folder in the format YYYY-MM-DD[-ProjectName][-vVersion].md",
-				OS:               "All",
-				Path:             "~/Dropbox/Blog",
-				FileTemplatePath: "~/.templates/blog-template.md",
-				Type:             FileType,
+				Name:        "blog",
+				Description: "Creates a MD file in the ~/Dropbox/Blog folder in the format YYYY-MM-DD[-ProjectName][-vVersion].md",
+				OS:          "All",
+				Path:        "~/Dropbox/Blog",
+				File: File{
+					TemplatePath: "~/.templates/blog-template.md",
+				},
+				Type: FileType,
 				NamePatterns: []string{
 					"{{$version := getVersion}}" +
 						"{{.Year}}-{{.Month}}-{{.Day}}" +
@@ -85,7 +89,8 @@ Today's notes
 				Description: "Creates a MD file in the ~/Dropbox/Therapy folder in the format YYYY-MM-DD[-vVersion].md",
 				OS:          "All",
 				Path:        "~/Dropbox/Therapy",
-				FileTemplate: `# {{.Year}}-{{.Month}}-{{.Day}}
+				File: File{
+					Template: `# {{.Year}}-{{.Month}}-{{.Day}}
 
 {{if .ProjectName}}Therapy notes on {{.ProjectName}}{{else}}Therapy notes{{end}}
 
@@ -97,6 +102,7 @@ Start writing here...
 
 Sincerely, me
 `,
+				},
 				Type: FileType,
 				NamePatterns: []string{
 					"{{$version := getVersion}}" +
@@ -189,4 +195,7 @@ Sincerely, me
 			},
 		},
 	}
+
+	validateConfig(&config)
+	return config
 }
diff --git a/extras_test.go b/extras_test.go
index 5719ab0..b252313 100644
--- a/extras_test.go
+++ b/extras_test.go
@@ -51,8 +51,9 @@ func TestExtras(t *testing.T) {
 	assert.Equal(t, extras, loadedExtras)
 
 	// test path errors
-	_, error = loadExtras("non-existent-file.yaml")
-	assert.NotNil(t, error, "Expected to fail to load extras, but succeeded")
+	extras, error = loadExtras("non-existent-file.yaml")
+	assert.Nil(t, error, "Expected to load extras")
+	assert.Empty(t, extras, "Expected to load empty extras")
 
 	os.WriteFile(file.Name(), []byte("invalid yaml"), 0644)
 
diff --git a/profile.go b/profile.go
index 888ee70..0c6ba81 100644
--- a/profile.go
+++ b/profile.go
@@ -31,19 +31,23 @@ const (
 	All     OSType = "All"
 )
 
-type Profile struct {
-	IsDefault        bool
-	Name             string
-	Description      string
-	Path             string
-	Type             ProfileType
-	NamePatterns     []string
-	PostScripts      []string
-	ScriptShell      string
-	OS               OSType
-	FileTemplatePath string
-	FileTemplate     string
+type File struct {
+	TemplatePath string
+	Template     string
+}
 
+type Profile struct {
+	File
+	IsDefault    bool
+	Name         string
+	Description  string
+	Path         string
+	Type         ProfileType
+	NamePatterns []string
+	PostScripts  []string
+	ScriptShell  string
+	OS           OSType
+	Files        []File
 	// FileTemplatePath string
 }
 
@@ -141,17 +145,17 @@ func processProfile(profile Profile, projectName string, extras map[string]strin
 			return fmt.Errorf("unable to change directory to %v: %v", fullPath, err)
 		}
 	} else if profileType == FileType {
-		useTemplateFile := profile.FileTemplatePath != ""
-		useTemplate := profile.FileTemplate != ""
+		useTemplateFile := profile.TemplatePath != ""
+		useTemplate := profile.Template != ""
 		if useTemplateFile {
-			fmt.Printf("Creating file '%v' from template: %v\n", fullPath, profile.FileTemplatePath)
-			err := createTemplateFile(profile.FileTemplatePath, fullPath, projectName, newName, extras, usr)
+			fmt.Printf("Creating file '%v' from template: %v\n", fullPath, profile.TemplatePath)
+			err := createTemplateFile(profile.TemplatePath, fullPath, projectName, newName, extras, usr)
 			if err != nil {
 				return fmt.Errorf("error creating template file: %v", err)
 			}
 		} else if useTemplate {
 			fmt.Printf("Creating file '%v' from template string\n", fullPath)
-			templateString, err := createTemplateString(profile.FileTemplate, projectName, newName, extras)
+			templateString, err := createTemplateString(profile.Template, projectName, newName, extras)
 			if err != nil {
 				return fmt.Errorf("error creating template: %v", err)
 			}
@@ -185,7 +189,7 @@ func processProfile(profile Profile, projectName string, extras map[string]strin
 	return nil
 }
 
-func validateProfile(profile Profile) error {
+func validateProfile(profile *Profile) error {
 	if profile.Name == "" {
 		return fmt.Errorf("Profile name is required")
 	}
@@ -198,13 +202,13 @@ func validateProfile(profile Profile) error {
 	if profile.Type == "" || (profile.Type != FileType && profile.Type != FolderType && profile.Type != ScriptType) {
 		return fmt.Errorf("invalid profile type %s for profile %s", profile.Type, profile.Name)
 	}
-	if profile.Type != FileType && profile.FileTemplatePath != "" {
+	if profile.Type != FileType && profile.TemplatePath != "" {
 		return fmt.Errorf("file template path is not supported for profile %s, which has Type=%s", profile.Name, profile.Type)
 	}
-	if profile.Type != FileType && profile.FileTemplate != "" {
+	if profile.Type != FileType && profile.Template != "" {
 		return fmt.Errorf("file template is not supported for profile %s, which has Type=%s", profile.Name, profile.Type)
 	}
-	if profile.Type == FileType && profile.FileTemplatePath != "" && profile.FileTemplate != "" {
+	if profile.Type == FileType && profile.TemplatePath != "" && profile.Template != "" {
 		return fmt.Errorf("both file template path and file template are specified for profile %s", profile.Name)
 	}
 	if profile.Type == ScriptType && profile.NamePatterns != nil && len(profile.NamePatterns) > 1 {
@@ -213,5 +217,8 @@ func validateProfile(profile Profile) error {
 	if profile.ScriptShell == "" && profile.PostScripts != nil && len(profile.PostScripts) > 0 {
 		return fmt.Errorf("script shell is required for profile %s", profile.Name)
 	}
+	if profile.Files == nil {
+		profile.Files = []File{}
+	}
 	return nil
 }
diff --git a/profile_test.go b/profile_test.go
index f2c747f..cc0182a 100644
--- a/profile_test.go
+++ b/profile_test.go
@@ -6,13 +6,13 @@ import (
 
 func TestValidateProfile(t *testing.T) {
 	testSuccess := func(profile Profile) {
-		err := validateProfile(profile)
+		err := validateProfile(&profile)
 		if err != nil {
 			t.Errorf("Expected to validate profile '%v', but failed: %v", profile.Name, err)
 		}
 	}
 	testFailure := func(profile Profile) {
-		err := validateProfile(profile)
+		err := validateProfile(&profile)
 		if err == nil {
 			t.Errorf("Expected to fail to validate profile '%v', but succeeded", profile.Name)
 		}
@@ -27,13 +27,17 @@ func TestValidateProfile(t *testing.T) {
 		Type:         FileType,
 		Name:         "daily",
 		NamePatterns: []string{"{{.Date}}"},
-		FileTemplate: "foo",
+		File: File{
+			Template: "foo",
+		},
 	})
 	testSuccess(Profile{
-		Type:             FileType,
-		Name:             "daily",
-		NamePatterns:     []string{"{{.Date}}"},
-		FileTemplatePath: "foo",
+		Type:         FileType,
+		Name:         "daily",
+		NamePatterns: []string{"{{.Date}}"},
+		File: File{
+			TemplatePath: "foo",
+		},
 	})
 
 	// empty name
@@ -65,25 +69,31 @@ func TestValidateProfile(t *testing.T) {
 	})
 	// file template and file template path are set
 	testFailure(Profile{
-		Type:             FileType,
-		Name:             "daily",
-		NamePatterns:     []string{"test"},
-		FileTemplatePath: "foo",
-		FileTemplate:     "bar",
+		Type:         FileType,
+		Name:         "daily",
+		NamePatterns: []string{"test"},
+		File: File{
+			TemplatePath: "foo",
+			Template:     "bar",
+		},
 	})
 	// file template set with wrong type
 	testFailure(Profile{
 		Type:         FolderType,
 		Name:         "daily",
 		NamePatterns: []string{"test"},
-		FileTemplate: "bar",
+		File: File{
+			Template: "bar",
+		},
 	})
 	// file template path set with wrong type
 	testFailure(Profile{
-		Type:             FolderType,
-		Name:             "daily",
-		NamePatterns:     []string{"test"},
-		FileTemplatePath: "bar",
+		Type:         FolderType,
+		Name:         "daily",
+		NamePatterns: []string{"test"},
+		File: File{
+			TemplatePath: "bar",
+		},
 	})
 	// script type with multiple name patterns
 	testFailure(Profile{