template / commit
Adding script execution type and other changes:
.vscode/launch.json | 13 +++++++++++++ config.go | 37 ++++++++++++++++++++++++++++++++++++- file.go | 20 +++++++++++++------- profile.go | 25 +++++++++++-------------- utils.go | 9 ++++++--- 5 files changed, 79 insertions(+), 25 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 2f60e7f..99d1872 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -71,6 +71,19 @@ "note", "whoa" ] + }, + { + "name": "Run: template test blah", + "type": "go", + "request": "launch", + "mode": "auto", + "program": ".", + "args": [ + "test", + "blah", + "--value1=1", + "--value2=two" + ] } ] } \ No newline at end of file diff --git a/config.go b/config.go index 9477ccf..2df6583 100644 --- a/config.go +++ b/config.go @@ -14,6 +14,7 @@ type ProfileType string const ( FileType ProfileType = "file" FolderType ProfileType = "folder" + ScriptType ProfileType = "script" ) type OSType string @@ -163,6 +164,40 @@ Today's notes "code .", }, }, + { + Name: "git", + Description: "Creates a folder in the c:/code folder in the format YYYY-MM-DD[-hh][-mm][-ss][-ProjectName]", + OS: "Windows", + Path: "C:/code", + Type: FolderType, + NamePatterns: []string{ + "{{.Year}}-{{.Month}}-{{.Day}}{{if .ProjectName}}-{{.ProjectName}}{{end}}", + "{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}{{if .ProjectName}}-{{.ProjectName}}{{end}}", + "{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}-{{.Minute}}{{if .ProjectName}}-{{.ProjectName}}{{end}}", + "{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}-{{.Minute}}-{{.Second}}{{if .ProjectName}}-{{.ProjectName}}{{end}}", + }, + ScriptShell: "pwsh", + PostScripts: []string{ + "git init", + "code \"{{.Path}}\"", + }, + }, + { + Name: "test", + Description: "Echoes the format YYYY-MM-DD[-hh][-mm][-ss][-ProjectName]", + Type: ScriptType, + NamePatterns: []string{ + "{{.Year}}-{{.Month}}-{{.Day}}{{if .ProjectName}}-{{.ProjectName}}{{end}}", + "{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}{{if .ProjectName}}-{{.ProjectName}}{{end}}", + "{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}-{{.Minute}}{{if .ProjectName}}-{{.ProjectName}}{{end}}", + "{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}-{{.Minute}}-{{.Second}}{{if .ProjectName}}-{{.ProjectName}}{{end}}", + }, + ScriptShell: "pwsh", + PostScripts: []string{ + "echo \"Name={{.Name}}\"", + "echo \"Path={{.Path}}\"", + }, + }, }, } } @@ -192,7 +227,7 @@ func validateConfig(config Config) error { if profile.Path != "" && strings.Contains(profile.Path, "\\") { return fmt.Errorf("invalid path for profile %s: %s", profile.Name, profile.Path) } - if profile.Type == "" || (profile.Type != FileType && profile.Type != FolderType) { + 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 != "" { diff --git a/file.go b/file.go index bdf7952..a82aa49 100644 --- a/file.go +++ b/file.go @@ -32,26 +32,32 @@ func createTemplateFile(srcPath string, destPath string, projectName string, nam filePatternData := createFilePatternData(now, projectName, name) // standardize paths - srcPath = getFullPath(srcPath) - destPath = getFullPath(destPath) + srcPath,err := getFullPath(srcPath) + if err != nil { + return err + } + destPath, err = getFullPath(destPath) + if err != nil { + return err + } // parse template srcName := filepath.Base(srcPath) // srcName is required because we are sending files to ParseFiles template, err := template.New(srcName).ParseFiles(srcPath) if err != nil { - return fmt.Errorf("Error parsing source file '%v': %v", srcPath, err) + return fmt.Errorf("error parsing source file '%v': %v", srcPath, err) } // create output file writer, err := os.Create(destPath) if err != nil { - return fmt.Errorf("Error creating destination file '%v': %v", destPath, err) + return fmt.Errorf("error creating destination file '%v': %v", destPath, err) } // write template to output file err = template.Execute(writer, filePatternData) if err != nil { - return fmt.Errorf("Error executing template: %v", err) + return fmt.Errorf("error executing template: %v", err) } return nil @@ -64,14 +70,14 @@ func createTemplateString(templateText string, projectName string, name string) // parse template template, err := template.New("text-template").Parse(templateText) if err != nil { - return "", fmt.Errorf("Error parsing template text: %v", err) + return "", fmt.Errorf("error parsing template text: %v", err) } // create output string sb := new(strings.Builder) err = template.Execute(sb, filePatternData) if err != nil { - return "", fmt.Errorf("Error creating templated file: %v", err) + return "", fmt.Errorf("error creating templated file: %v", err) } return sb.String(), nil diff --git a/profile.go b/profile.go index 7149110..8208fc2 100644 --- a/profile.go +++ b/profile.go @@ -56,8 +56,11 @@ func processProfile(profile Profile, projectName string) error { } if profile.Path != "" { // change cwd to the profile's path, if one is specified - path := getFullPath(profile.Path) - err := os.Chdir(path) + path, err := getFullPath(profile.Path) + if err != nil { + return fmt.Errorf("unable to get full path for '%v': %v", profile.Path, err) + } + err = os.Chdir(path) if err != nil { return fmt.Errorf("unable to change directory to %v: %v", path, err) } @@ -71,21 +74,13 @@ func processProfile(profile Profile, projectName string) error { } // determine if we're dealing with files or directories - var useDirs bool - switch profile.Type { - case FileType: - useDirs = false - case FolderType: - useDirs = true - default: - return fmt.Errorf("unknown profile type: %v", profile.Type) - } + profileType := profile.Type // filter entities to just files or directories var entities []string for _, file := range files { isDir := file.IsDir() - if isDir == useDirs { + if isDir == (profileType == FolderType) { entities = append(entities, file.Name()) } } @@ -104,7 +99,7 @@ func processProfile(profile Profile, projectName string) error { // create new entity fullPath := filepath.Join(cwd, newName) - if useDirs { + if profileType == FolderType { fmt.Printf("Creating directory: %v\n", fullPath) err = os.Mkdir(fullPath, os.ModePerm) if err != nil { @@ -114,7 +109,7 @@ func processProfile(profile Profile, projectName string) error { if err != nil { return fmt.Errorf("unable to change directory to %v: %v", fullPath, err) } - } else { + } else if profileType == FileType { useTemplateFile := profile.FileTemplatePath != "" useTemplate := profile.FileTemplate != "" if useTemplateFile { @@ -134,6 +129,8 @@ func processProfile(profile Profile, projectName string) error { fmt.Printf("Creating empty file: %v\n", fullPath) os.WriteFile(fullPath, []byte{}, os.ModePerm) } + } else { + fmt.Printf("Profile type is script, doing nothing during main phase\n") } // execute PostScript, if present diff --git a/utils.go b/utils.go index 91e1cac..4cda077 100644 --- a/utils.go +++ b/utils.go @@ -6,8 +6,11 @@ import ( "strings" ) -func getFullPath(path string) string { - usr, _ := user.Current() +func getFullPath(path string) (string, error) { + usr, error := user.Current() + if error != nil { + return "", error + } dir := usr.HomeDir var newPath string if path == "~" { @@ -17,5 +20,5 @@ func getFullPath(path string) string { } else { newPath = path } - return newPath + return newPath, nil }