fuzzyworld

template / commit

Getting closer to working patterns.

230da3bece0fd31f31ae445be5d4daa57ce3bc6b · Pavel Safronov · 2022-09-21 18:46:24 -0700 · parents d026838a2cb2

Getting closer to working patterns.
 config.go | 27 ++++++++++++++++-----------
 go.mod    |  1 +
 go.sum    |  2 ++
 main.go   |  8 +++++++-
 new.go    | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 75 insertions(+), 13 deletions(-)

diff --git a/config.go b/config.go
index 93680c3..2aa8cfe 100644
--- a/config.go
+++ b/config.go
@@ -14,29 +14,34 @@ const (
 )
 
 type Profile struct {
-	Path        string
-	Type        ProfileType
-	NamePattern string
+	Path         string
+	Type         ProfileType
+	NamePatterns []string
 	// ReplacementStrings map[string][]string
 	// FileTemplatePath string
 }
 
-type PatternInfo struct {
-	DistinctDate string
-	Name         string
-}
-
 type Config struct {
 	Profiles map[string]Profile
 }
 
+type PatternInfo struct {
+	Year4, Month2, Day2, Hour2, Minute2, Second2 string
+	ProjectName                                  string
+}
+
 func defaultConfig() Config {
 	return Config{
 		Profiles: map[string]Profile{
 			"default": {
-				Path:        "C:/DailyNotes",
-				Type:        FileType,
-				NamePattern: "{{date}}.md",
+				Path: "C:/DailyNotes",
+				Type: FileType,
+				NamePatterns: []string{
+					"{{.Year4}}-{{.Month2}}-{{.Day2}}{{if .ProjectName}}-{{.ProjectName}}{{end}}.md",
+					"{{.Year4}}-{{.Month2}}-{{.Day2}}-{{.Hour2}}{{if .ProjectName}}-{{.ProjectName}}{{end}}.md",
+					"{{.Year4}}-{{.Month2}}-{{.Day2}}-{{.Hour2}}-{{.Minute2}}{{if .ProjectName}}-{{.ProjectName}}{{end}}.md",
+					"{{.Year4}}-{{.Month2}}-{{.Day2}}-{{.Hour2}}-{{.Minute2}}-{{.Second2}}{{if .ProjectName}}-{{.ProjectName}}{{end}}.md",
+				},
 			},
 			"blog": {
 				Path: "~/Dropbox/Blog",
diff --git a/go.mod b/go.mod
index fb6c015..fb5860f 100644
--- a/go.mod
+++ b/go.mod
@@ -4,6 +4,7 @@ go 1.19
 
 require (
 	github.com/urfave/cli/v2 v2.16.3
+	golang.org/x/exp v0.0.0-20220921164117-439092de6870
 	gopkg.in/yaml.v3 v3.0.1
 )
 
diff --git a/go.sum b/go.sum
index 6fad046..059a9a7 100644
--- a/go.sum
+++ b/go.sum
@@ -6,6 +6,8 @@ github.com/urfave/cli/v2 v2.16.3 h1:gHoFIwpPjoyIMbJp/VFd+/vuD0dAgFK4B6DpEMFJfQk=
 github.com/urfave/cli/v2 v2.16.3/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI=
 github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
 github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
+golang.org/x/exp v0.0.0-20220921164117-439092de6870 h1:j8b6j9gzSigH28O5SjSpQSSh9lFd6f5D/q0aHjNTulc=
+golang.org/x/exp v0.0.0-20220921164117-439092de6870/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
diff --git a/main.go b/main.go
index 47060b6..eea92df 100644
--- a/main.go
+++ b/main.go
@@ -78,11 +78,17 @@ func main() {
 			profile, ok := config.Profiles[profileName]
 			if ok {
 				fmt.Println("Found profile!")
-				fmt.Println(profile)
+				// fmt.Println(profile)
 			} else {
 				processError(fmt.Errorf("Unable to find profile '%v'", profileName), "Error finding profile")
 			}
 
+			// cwd, err := os.Getwd()
+			// if err != nil {
+			// 	panic(err)
+			// }
+			processProfile(profile, projectName, profile.Path)
+
 			return nil
 		},
 	}
diff --git a/new.go b/new.go
index 038293d..30e787c 100644
--- a/new.go
+++ b/new.go
@@ -1,8 +1,15 @@
 package main
 
 import (
+	"errors"
 	"fmt"
 	"os"
+	"path"
+	"strings"
+	"text/template"
+	"time"
+
+	"golang.org/x/exp/slices"
 )
 
 func processProfile(profile Profile, name string, cwd string) error {
@@ -29,7 +36,48 @@ func processProfile(profile Profile, name string, cwd string) error {
 		}
 	}
 
-	patternInfo := PatternInfo{}
+	var newName string
+	now := time.Now()
+	patternInfo := PatternInfo{
+		Year4: now.Format("2006"),
+		Month2:   now.Format("01"),
+		Day2:   now.Format("02"),
+		Hour2:   now.Format("15"),
+		Minute2:   now.Format("04"),
+		Second2:   now.Format("04"),
+		ProjectName: name,
+	}
+	for _, namePattern := range profile.NamePatterns {
+		newName = ""
+		t, err := template.New("template-name").Parse(namePattern)
+		if err != nil {
+			panic(err)
+		}
+		sb := new(strings.Builder)
+		err = t.Execute(sb, patternInfo)
+		if err != nil {
+			panic(err)
+		}
+		newName = sb.String()
+		alreadyExists := slices.Contains(entities, newName)
+		if alreadyExists {
+			break
+		}
+	}
+
+	if newName == "" {
+		panic(errors.New("Couldn't find a good new name"))
+	} else {
+		fullName := path.Join(cwd, newName)
+		if useDirs {
+			err = os.Mkdir(fullName, os.ModePerm)
+			if err != nil {
+				panic(err)
+			}
+		} else {
+			os.WriteFile(fullName, []byte{}, os.ModePerm)
+		}
+	}
 
 	return nil
 }