template / commit
Fixed issue with post script needing to be split up. Now everything works as expected.
config.go | 96 ++++++++++++++++++++++++++++++----------------------------- main.go | 14 ++++----- postscript.go | 33 +++++++++----------- profile.go | 4 +-- 4 files changed, 72 insertions(+), 75 deletions(-) diff --git a/config.go b/config.go index 98219db..fa7cfe0 100644 --- a/config.go +++ b/config.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "golang.org/x/exp/slices" "gopkg.in/yaml.v3" ) @@ -25,10 +26,11 @@ const ( type Profile struct { IsDefault bool Name string + Description string Path string Type ProfileType NamePatterns []string - PostScripts [][]string + PostScripts []string ScriptShell string OS OSType FileTemplatePath string @@ -36,6 +38,11 @@ type Profile struct { // FileTemplatePath string } +var forbiddenProfileNames = []string{ + "list", + "get", +} + type Config struct { Profiles []Profile } @@ -44,10 +51,11 @@ func defaultConfig() Config { return Config{ Profiles: []Profile{ { - Name: "daily", - OS: "Windows", - Path: "C:\\work\\daily-notes", - Type: FileType, + Name: "daily", + Description: "Creates a MD file in c:\\work\\daily-notes folder in the format YYYY-MM-DD[-vX].md", + OS: "Windows", + Path: "C:\\work\\daily-notes", + Type: FileType, NamePatterns: []string{ "{{$version := getVersion}}" + "{{.Year}}-{{.Month}}-{{.Day}}" + @@ -55,18 +63,16 @@ func defaultConfig() Config { ".md", }, ScriptShell: "pwsh", - PostScripts: [][]string{{ - "code", - ".", - "\"{{.Path}}\"", - }, + PostScripts: []string{ + "code . \"{{.Path}}\"", }, }, { - Name: "note", - OS: "All", - IsDefault: true, - Type: FileType, + Name: "note", + Description: "Creates a MD file in the current folder in the format YYYY-MM-DD[-ProjectName][-vVersion].md", + OS: "All", + IsDefault: true, + Type: FileType, NamePatterns: []string{ "{{$version := getVersion}}" + "{{.Year}}-{{.Month}}-{{.Day}}" + @@ -75,13 +81,13 @@ func defaultConfig() Config { ".md", }, ScriptShell: "pwsh", - PostScripts: [][]string{{ - "code", - "\"{{.Path}}\"", - }}, + PostScripts: []string{ + "code \"{{.Path}}\"", + }, }, { 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", @@ -94,17 +100,16 @@ func defaultConfig() Config { ".md", }, ScriptShell: "pwsh", - PostScripts: [][]string{{ - "code", - ".", - "\"{{.Name}}\"", - }}, + PostScripts: []string{ + "code . \"{{.Name}}\"", + }, }, { - Name: "work", - OS: "Windows", - Path: "C:/work", - Type: FolderType, + Name: "work", + Description: "Creates a folder in the c:/work folder in the format YYYY-MM-DD[-hh][-mm][-ss][-ProjectName]", + OS: "Windows", + Path: "C:/work", + Type: FolderType, NamePatterns: []string{ "{{.Year}}-{{.Month}}-{{.Day}}{{if .ProjectName}}-{{.ProjectName}}{{end}}", "{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}{{if .ProjectName}}-{{.ProjectName}}{{end}}", @@ -112,13 +117,13 @@ func defaultConfig() Config { "{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}-{{.Minute}}-{{.Second}}{{if .ProjectName}}-{{.ProjectName}}{{end}}", }, ScriptShell: "pwsh", - PostScripts: [][]string{{ - "explorer", - "\"{{.Path}}\"", - }}, + PostScripts: []string{ + "explorer \"{{.Path}}\"", + }, }, { Name: "folder", + Description: "Creates a folder in the current folder in the format YYYY-MM-DD[-ProjectName][-vVersion]", Type: FolderType, ScriptShell: "pwsh", NamePatterns: []string{ @@ -131,6 +136,7 @@ func defaultConfig() Config { { Name: "create-react-app", Type: FolderType, + Description: "Creates a folder in the current folder in the format create-react-app[-ProjectName][-vVersion], runs `npx create-react-app` to initialize the folder, and opens the folder in VSCode", ScriptShell: "pwsh", NamePatterns: []string{ "{{$version := getVersion}}" + @@ -138,16 +144,9 @@ func defaultConfig() Config { "{{if .ProjectName}}-{{.ProjectName}}{{end}}" + "{{if (gt $version 1)}}-n{{$version}}{{end}}", }, - PostScripts: [][]string{ - { - "npx", - "create-react-app", - ".", - }, - { - "code", - ".", - }, + PostScripts: []string{ + "npx create-react-app .", + "code .", }, }, }, @@ -156,7 +155,7 @@ func defaultConfig() Config { func validateConfig(config Config) error { if config.Profiles == nil || len(config.Profiles) == 0 { - return fmt.Errorf("No profiles found") + return fmt.Errorf("no profiles found") } foundDefault := false @@ -164,23 +163,26 @@ func validateConfig(config Config) error { if profile.Name == "" { return fmt.Errorf("Profile name is required") } + if slices.Contains(forbiddenProfileNames, profile.Name) { + return fmt.Errorf("Forbidden profile name '%s'", profile.Name) + } if profile.IsDefault { if foundDefault { - return fmt.Errorf("Multiple default profiles found") + return fmt.Errorf("multiple default profiles found") } foundDefault = true } if profile.NamePatterns == nil || len(profile.NamePatterns) == 0 { - return fmt.Errorf("No name patterns found for profile %s", profile.Name) + return fmt.Errorf("no name patterns found for profile %s", profile.Name) } if profile.Type == "" || (profile.Type != FileType && profile.Type != FolderType) { - return fmt.Errorf("Invalid profile type %s for profile %s", profile.Type, profile.Name) + return fmt.Errorf("invalid profile type %s for profile %s", profile.Type, profile.Name) } if profile.Type != FileType && profile.FileTemplatePath != "" { - return fmt.Errorf("File template path is not supported for profile %s, which has Type=%s", profile.Name, profile.Type) + return fmt.Errorf("file template path is not supported for profile %s, which has Type=%s", profile.Name, profile.Type) } if profile.ScriptShell == "" && profile.PostScripts != nil && len(profile.PostScripts) > 0 { - return fmt.Errorf("Script shell is required for profile %s", profile.Name) + return fmt.Errorf("script shell is required for profile %s", profile.Name) } } return nil diff --git a/main.go b/main.go index f7f6d5b..4276b22 100644 --- a/main.go +++ b/main.go @@ -47,13 +47,13 @@ func main() { // create config and save err := saveConfig(configFilePath, defaultConfig()) if err != nil { - return fmt.Errorf("Error creating config: %v", err) + return fmt.Errorf("error creating config: %v", err) } // load config config, err = loadConfig(configFilePath) if err != nil { - return fmt.Errorf("Error loading config: %v", err) + return fmt.Errorf("error loading config: %v", err) } } else { // log successful load @@ -63,7 +63,7 @@ func main() { // validate config err = validateConfig(config) if err != nil { - return fmt.Errorf("Error validating config: %v", err) + return fmt.Errorf("error validating config: %v", err) } // read args @@ -91,7 +91,7 @@ func main() { if profileName == "list" { fmt.Println("Available profiles:") for _, p := range config.Profiles { - fmt.Printf(" %v\n", p.Name) + fmt.Printf(" %v: %v\n", p.Name, p.Description) } return nil } @@ -104,7 +104,7 @@ func main() { encoder := yaml.NewEncoder(&writer) err = encoder.Encode(p) if err != nil { - return fmt.Errorf("Error encoding profile: %v", err) + return fmt.Errorf("error encoding profile: %v", err) } yamlText := writer.String() fmt.Println(yamlText) @@ -116,13 +116,13 @@ func main() { // find profile profile, err := findProfile(config, profileName) if err != nil { - return fmt.Errorf("Error finding profile: %v", err) + return fmt.Errorf("error finding profile: %v", err) } // process profile err = processProfile(profile, projectName) if err != nil { - return fmt.Errorf("Error processing profile: %v", err) + return fmt.Errorf("error processing profile: %v", err) } return nil diff --git a/postscript.go b/postscript.go index e909164..def2ccb 100644 --- a/postscript.go +++ b/postscript.go @@ -10,8 +10,8 @@ type PostScriptPatternData struct { Path string } -func createPostScriptArrays(profile Profile, newName string, fullPath string) ([][]string, error) { - array := make([][]string, 0) +func createPostScriptArrays(profile Profile, newName string, fullPath string) ([]string, error) { + array := make([]string, 0) if profile.PostScripts == nil || len(profile.PostScripts) == 0 { return nil, nil @@ -22,25 +22,20 @@ func createPostScriptArrays(profile Profile, newName string, fullPath string) ([ Path: fullPath, } - for _, postScriptPatterns := range profile.PostScripts { - subarray := make([]string, 0) - for _, postScriptPattern := range postScriptPatterns { - - 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 - } + for _, postScriptPattern := range profile.PostScripts { + template, err := template.New("postscript").Parse(postScriptPattern) + if err != nil { + return nil, err + } - postScript := sb.String() - subarray = append(subarray, postScript) + sb := new(strings.Builder) + err = template.Execute(sb, patternData) + if err != nil { + return nil, err } - array = append(array, subarray) + + postScript := sb.String() + array = append(array, postScript) } return array, nil diff --git a/profile.go b/profile.go index b80d0ac..4d6256b 100644 --- a/profile.go +++ b/profile.go @@ -34,7 +34,7 @@ func findProfile(config Config, profileName string) (Profile, error) { func processProfile(profile Profile, projectName string) error { // validate profile if profile.NamePatterns == nil || len(profile.NamePatterns) == 0 { - return errors.New("No name patterns specified") + return errors.New("no name patterns specified") } // decide if the profile should run on this OS @@ -133,7 +133,7 @@ func processProfile(profile Profile, projectName string) error { return err } for _, postScript := range postScripts { - allArgs := append([]string{"-c"}, postScript...) + allArgs := []string{"-c", postScript} shell := profile.ScriptShell fmt.Printf("Executing post script: %v %v\n", shell, strings.Join(allArgs, " ")) cmd := exec.Command(shell, allArgs...)