diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fb74567 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# https://github.com/github/gitignore/blob/main/Go.gitignore + +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +.DS_Store diff --git a/README.md b/README.md new file mode 100644 index 0000000..83f619e --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Jn + +Just a simple utility to create or open a daily note. + +``` +/Users/seanwashbot/journal +└── 2024 + └── 05 + └── 01.md + └── 02.md + └── 03.md +``` + +## Todo + +- [ ] Note template +- [ ] Tests 😏 +- [ ] Raycast script +- [ ] Command to store configs at ~/.jn + - Journal destination + - File extension diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bb747ed --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/seanwash/jn + +go 1.22.2 diff --git a/main.go b/main.go new file mode 100644 index 0000000..946a0e8 --- /dev/null +++ b/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "log" + "os" + "os/exec" + "path" + "time" +) + +func main() { + fileExtension := ".md" + today := time.Now() + entryName := fmt.Sprintf("%02d%s", today.Day(), fileExtension) + homePath := getHomeDir() + rootPath := path.Join(homePath, "journal") + folderPath := path.Join(rootPath, fmt.Sprintf("%d", today.Year()), fmt.Sprintf("%02d", today.Month())) + entryPath := path.Join(folderPath, entryName) + cmd := exec.Command("open", entryPath) + entryExists, _ := exists(entryPath) + + if entryExists { + runCmd(cmd) + } else { + createEntry(folderPath, entryPath) + runCmd(cmd) + } +} + +func createEntry(folderPath string, entryPath string) { + err := os.MkdirAll(folderPath, 0700) + if err != nil { + log.Fatal("Could not create new folder", err) + } + + _, err = os.Create(entryPath) + if err != nil { + log.Fatal("Could not create new entry", err) + } +} + +func exists(path string) (bool, error) { + _, err := os.Stat(path) + if err != nil { + return false, err + } + + return true, nil +} + +func getHomeDir() string { + homePath, err := os.UserHomeDir() + if err != nil { + log.Fatal("Could not get user's home dir: ", err) + } + + return homePath +} + +func runCmd(cmd *exec.Cmd) { + err := cmd.Run() + if err != nil { + log.Fatal("Could not run cmd: ", err) + } +}