template / commit
Added support of labeling profiles with a particular OS.
config.go | 20 +++++++++++++++---- main.go | 7 ------- new.go | 68 +++++++++++++++++++++++++++++++++++++-------------------------- 3 files changed, 56 insertions(+), 39 deletions(-) diff --git a/config.go b/config.go index f1016bb..3f01dfa 100644 --- a/config.go +++ b/config.go @@ -13,6 +13,14 @@ const ( FolderType = "folder" ) +type OSType string + +const ( + Windows OSType = "Windows" + Mac = "Mac" + All = "All" +) + type Profile struct { IsDefault bool Path string @@ -20,6 +28,7 @@ type Profile struct { NamePatterns []string PostScript []string ScriptShell string + OS OSType // FileTemplatePath string } @@ -33,12 +42,13 @@ func defaultConfig() Config { Profiles: map[string]Profile{ "daily": { IsDefault: true, + OS: "Windows", Path: "C:\\work\\daily-notes", Type: FileType, NamePatterns: []string{ - "{{$index := getUniqueIndex}}" + + "{{$version := getVersion}}" + "{{.Year}}-{{.Month}}-{{.Day}}" + - "{{if (gt $index 0)}}-{{$index}}{{end}}" + + "{{if (gt $version 1)}}v{{$version}}{{end}}" + ".md", }, ScriptShell: "pwsh", @@ -49,13 +59,14 @@ func defaultConfig() Config { }, }, "blog": { + OS: "All", Path: "~/Dropbox/Blog", Type: FileType, NamePatterns: []string{ - "{{$index := getUniqueIndex}}" + + "{{$version := getVersion}}" + "{{.Year}}-{{.Month}}-{{.Day}}" + - "{{if (gt $index 0)}}-{{$index}}{{end}}" + "{{if .ProjectName}}-{{.ProjectName}}{{end}}" + + "{{if (gt $version 1)}}v{{$version}}{{end}}" + ".md", }, ScriptShell: "pwsh", @@ -66,6 +77,7 @@ func defaultConfig() Config { }, }, "work": { + OS: "Windows", Path: "C:/work", Type: FolderType, NamePatterns: []string{ diff --git a/main.go b/main.go index a8ad254..a7cb46a 100644 --- a/main.go +++ b/main.go @@ -83,13 +83,6 @@ func main() { processError(err, "Error while finding profile") } - // change cwd - path := getFullPath(profile.Path) - err = os.Chdir(path) - if err != nil { - processError(err, fmt.Sprintf("Error while changing directory to '%v'", path)) - } - // process profile err = processProfile(profile, projectName) if err != nil { diff --git a/new.go b/new.go index 385a544..734391d 100644 --- a/new.go +++ b/new.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "text/template" "time" @@ -39,11 +40,23 @@ func processProfile(profile Profile, name string) error { return errors.New("No name patterns specified") } - // get current directory - cwd, err := os.Getwd() + profileOs := profile.OS + isWindows := runtime.GOOS == "windows" + shouldRun := profileOs == "All" || + (profileOs == "Windows" && isWindows) || + (profileOs == "Mac" && !isWindows) + if !shouldRun { + fmt.Println("This profile is not supported by this OS") + return nil + } + + // change cwd + path := getFullPath(profile.Path) + err := os.Chdir(path) if err != nil { - return err + processError(err, fmt.Sprintf("Error while changing directory to '%v'", path)) } + cwd := path // get all entities in current directory files, err := os.ReadDir(cwd) @@ -77,22 +90,22 @@ func processProfile(profile Profile, name string) error { return err } - fullPath := filepath.Join(cwd, newName) - - // create new entity + // check newName is created if newName == "" { return errors.New("Couldn't find a good new name") - } else { - if useDirs { - fmt.Printf("Creating directory: %v\n", fullPath) - err = os.Mkdir(fullPath, os.ModePerm) - if err != nil { - return err - } - } else { - fmt.Printf("Creating file: %v\n", fullPath) - os.WriteFile(fullPath, []byte{}, os.ModePerm) + } + + // create new entity + fullPath := filepath.Join(cwd, newName) + if useDirs { + fmt.Printf("Creating directory: %v\n", fullPath) + err = os.Mkdir(fullPath, os.ModePerm) + if err != nil { + return err } + } else { + fmt.Printf("Creating file: %v\n", fullPath) + os.WriteFile(fullPath, []byte{}, os.ModePerm) } // execute PostScript, if present @@ -101,15 +114,14 @@ func processProfile(profile Profile, name string) error { return err } if postScripts != nil && len(postScripts) > 0 { - allArgs := []string{"/C"} - allArgs = append(allArgs, postScripts...) + allArgs := append([]string{"/C"}, postScripts...) shell := profile.ScriptShell - debugProfileScript := fmt.Sprintf("%v %v", shell, strings.Trim(fmt.Sprint(allArgs), "[]")) - fmt.Printf("Executing post script: %v \n", debugProfileScript) cmd := exec.Command(shell, allArgs...) if err := cmd.Run(); err != nil { // post scripts tend to fail (TODO: investigate why), so for now we'll just log the error - fmt.Printf("Error executing post script '%v': %v\n", debugProfileScript, err) + + fmt.Printf("Error executing post script '%v': %v\n", + fmt.Sprintf("%v %v", shell, strings.Trim(fmt.Sprint(allArgs), "[]")), err) } } @@ -155,12 +167,12 @@ func createNamePatternData(now time.Time, projectName string) NamePatternData { func createNewNameForProfile(profile Profile, patternData NamePatternData, existingEntities []string) (string, error) { for index, namePattern := range profile.NamePatterns { - startingIndex := 0 + startingVersion := 1 invokeCount := 0 funcMap := template.FuncMap{ - "getUniqueIndex": func() int { + "getVersion": func() int { invokeCount++ - return startingIndex + return startingVersion }, } template := template.New("template-name").Funcs(funcMap) @@ -187,18 +199,18 @@ func createNewNameForProfile(profile Profile, patternData NamePatternData, exist if !alreadyExists { return newName, nil } - // if index is not being used (template did not invoke getUniqueIndex) + // if index is not being used (template did not invoke getVersion) // then we're done if !useIndex { break } - // if getUniqueIndex was used, but this isn't the last pattern, report an error + // if getVersion was used, but this isn't the last pattern, report an error if useIndex && !isLastPattern { - return "", errors.New("Cannot use function getUniqueIndex in a pattern that is not the last pattern") + return "", errors.New("Cannot use function getVersion in a pattern that is not the last pattern") } - startingIndex++ + startingVersion++ } } return "", errors.New("Couldn't find a new name")