template / commit
Removing TemplatePath. File templates are now loaded only from the config.
README.md | 1 - config.go | 15 ++++++--------- config_test.go | 13 ++++--------- default_config.go | 13 ++++++++++++- main.go | 13 ++++++++++++- profile.go | 18 ++---------------- profile_test.go | 27 --------------------------- scripts/clean-install.ps1 | 9 ++------- templates/blog-template.md | 11 ----------- 9 files changed, 38 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 2a1f1db..a7bd8fb 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ This is one of the profiles that comes with the tool. ```yaml - file: - templatepath: "" template: |- # {{.Year}}-{{.Month}}-{{.Day}} diff --git a/config.go b/config.go index 7438b09..e514851 100644 --- a/config.go +++ b/config.go @@ -3,7 +3,6 @@ package main import ( "fmt" "os" - "os/user" "gopkg.in/yaml.v3" ) @@ -65,30 +64,28 @@ func saveConfig(path string, config Config) error { return nil } -func getOrCreateConfig(configFilePath string, usr user.User) (Config, error) { +func getOrCreateConfig(configFilePath string) (Config, error) { emptyConfig := Config{} - fullConfigPath := getFullPath(configFilePath, usr) - // try and load config - config, err := loadConfig(fullConfigPath) + config, err := loadConfig(configFilePath) if err != nil { - fmt.Printf("No config present at '%v', creating and using sample config\n", fullConfigPath) + fmt.Printf("No config present at '%v', creating and using sample config\n", configFilePath) // create config and save - err := saveConfig(fullConfigPath, defaultConfig()) + err := saveConfig(configFilePath, defaultConfig()) if err != nil { return emptyConfig, fmt.Errorf("error creating config: %v", err) } // load config - config, err = loadConfig(fullConfigPath) + config, err = loadConfig(configFilePath) if err != nil { return emptyConfig, fmt.Errorf("error loading config: %v", err) } } else { // log successful load - fmt.Printf("Config loaded from '%v'\n", fullConfigPath) + fmt.Printf("Config loaded from '%v'\n", configFilePath) } // validate config diff --git a/config_test.go b/config_test.go index 6d30a6f..b7b7577 100644 --- a/config_test.go +++ b/config_test.go @@ -3,21 +3,18 @@ package main import ( "errors" "os" - "os/user" "testing" "github.com/stretchr/testify/assert" ) func TestSaveLoadConfig(t *testing.T) { - usr, err := user.Current() - assert.Nil(t, err) config := defaultConfig() configPath := "./test/test-config.yaml" // save config - err = saveConfig(configPath, config) + err := saveConfig(configPath, config) assert.Nil(t, err, "Error saving config") // load config @@ -48,7 +45,7 @@ func TestSaveLoadConfig(t *testing.T) { assert.True(t, errors.Is(err, os.ErrNotExist), "Expected config file to not exist, but it does") // get and create config - loadedConfig, err = getOrCreateConfig(configPath, *usr) + loadedConfig, err = getOrCreateConfig(configPath) assert.Nil(t, err, "Error loading config") // test config @@ -59,7 +56,7 @@ func TestSaveLoadConfig(t *testing.T) { assert.Nil(t, err, "Expected config file to exist, but it does not") // get and create config - loadedConfig, err = getOrCreateConfig(configPath, *usr) + loadedConfig, err = getOrCreateConfig(configPath) assert.Nil(t, err, "Error loading config") // test config @@ -72,10 +69,8 @@ func TestSaveLoadConfig(t *testing.T) { } func TestGetOrCreateConfig(t *testing.T) { - usr, err := user.Current() - assert.Nil(t, err) testFailure := func(configPath string) { - _, err := getOrCreateConfig(configPath, *usr) + _, err := getOrCreateConfig(configPath) if err == nil { t.Errorf("Expected to fail to get or create config, but succeeded") } diff --git a/default_config.go b/default_config.go index 22c9055..24946c1 100644 --- a/default_config.go +++ b/default_config.go @@ -53,7 +53,18 @@ Today's notes OS: "All", Path: "~/Blog", File: File{ - TemplatePath: "~/.templates/blog-template.md", + Template: `# {{.ProjectName}} + +Created by template, on {{.Time}} + +## Spelling + +cspell:words nanite + +## Removed writing + +## Writing +`, }, Type: FileType, NamePatterns: []ParameterizedText{ diff --git a/main.go b/main.go index 855e894..c5952c5 100644 --- a/main.go +++ b/main.go @@ -73,11 +73,12 @@ func main() { } // get config + fullConfigPath := getFullPath(configFilePath, *usr) var config Config if useDefaultConfig { config = defaultConfig() } else { - config, err = getOrCreateConfig(configFilePath, *usr) + config, err = getOrCreateConfig(fullConfigPath) if err != nil { return err } @@ -135,6 +136,16 @@ func main() { return nil } + // reset config to the default + if profileName == "reset" { + fmt.Printf("Resetting config at '%v' to default\n", fullConfigPath) + err = saveConfig(fullConfigPath, defaultConfig()) + if err != nil { + return fmt.Errorf("error saving default config: %v", err) + } + return nil + } + // find profile profile, err := findProfile(config, profileName) if err != nil { diff --git a/profile.go b/profile.go index 7aa0b64..cd89c5f 100644 --- a/profile.go +++ b/profile.go @@ -34,8 +34,7 @@ const ( type ParameterizedText string type File struct { - TemplatePath string - Template ParameterizedText + Template ParameterizedText } type FileInFolder struct { @@ -197,15 +196,8 @@ func processProfile(profile Profile, projectName string, extras map[string]strin } func createFile(file File, fullPath string, projectName string, newName string, extras map[string]string, usr user.User) error { - useTemplateFile := file.TemplatePath != "" useTemplate := file.Template != "" - if useTemplateFile { - fmt.Printf("Creating file '%v' from template: %v\n", fullPath, file.TemplatePath) - err := createFileFromTemplateFile(file.TemplatePath, fullPath, projectName, newName, extras, usr) - if err != nil { - return fmt.Errorf("error creating template file: %v", err) - } - } else if useTemplate { + if useTemplate { fmt.Printf("Creating file '%v' from template string\n", fullPath) templateString, err := createTemplateString(file.Template, projectName, newName, extras) if err != nil { @@ -232,15 +224,9 @@ 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.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.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.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 { return fmt.Errorf("multiple name patterns are not supported for profile %s, which has Type=%s", profile.Name, profile.Type) } diff --git a/profile_test.go b/profile_test.go index 47fe19e..17f726a 100644 --- a/profile_test.go +++ b/profile_test.go @@ -31,14 +31,6 @@ func TestValidateProfile(t *testing.T) { Template: "foo", }, }) - testSuccess(Profile{ - Type: FileType, - Name: "daily", - NamePatterns: []ParameterizedText{"{{.Date}}"}, - File: File{ - TemplatePath: "foo", - }, - }) // empty name testFailure(Profile{ @@ -67,16 +59,6 @@ func TestValidateProfile(t *testing.T) { Name: "daily", NamePatterns: []ParameterizedText{}, }) - // file template and file template path are set - testFailure(Profile{ - Type: FileType, - Name: "daily", - NamePatterns: []ParameterizedText{"test"}, - File: File{ - TemplatePath: "foo", - Template: "bar", - }, - }) // file template set with wrong type testFailure(Profile{ Type: FolderType, @@ -86,15 +68,6 @@ func TestValidateProfile(t *testing.T) { Template: "bar", }, }) - // file template path set with wrong type - testFailure(Profile{ - Type: FolderType, - Name: "daily", - NamePatterns: []ParameterizedText{"test"}, - File: File{ - TemplatePath: "bar", - }, - }) // script type with multiple name patterns testFailure(Profile{ Type: ScriptType, diff --git a/scripts/clean-install.ps1 b/scripts/clean-install.ps1 index 6891efb..059b9db 100644 --- a/scripts/clean-install.ps1 +++ b/scripts/clean-install.ps1 @@ -4,10 +4,5 @@ Set-Location "$scriptPath/.." Write-Host "Installing..." go install -Write-Host "Removing old templates folder..." -Remove-Item ~/.templates -Recurse -Force -ErrorAction Continue -Write-Host "Copying new templates folder..." -Copy-Item .\templates -Destination ~/.templates -Recurse - -Write-Host "Removing old config file..." -Remove-Item ~/.template.yaml -Force -ErrorAction Continue \ No newline at end of file +Write-Host "Saving default config..." +template reset \ No newline at end of file diff --git a/templates/blog-template.md b/templates/blog-template.md deleted file mode 100644 index bccf68f..0000000 --- a/templates/blog-template.md +++ /dev/null @@ -1,11 +0,0 @@ -# {{.ProjectName}} - -Created by template, on {{.Time}} - -## Spelling - -cspell:words nanite - -## Removed writing - -## Writing