93 lines
1.8 KiB
Go
93 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
sb := os.Getenv("SECOND_BRAIN")
|
|
ParseFlags(sb)
|
|
}
|
|
|
|
func ParseFlags(sb string) {
|
|
note := sb + "/notes"
|
|
daily := sb + "/periodic/daily"
|
|
templates := sb + "/templates"
|
|
|
|
quickPtr := flag.Bool("q", false, "Open quick file for notes")
|
|
listPtr := flag.Bool("l", false, "List files in notes directory")
|
|
dailyPtr := flag.Bool("d", false, "Create/Edit daily journal entry")
|
|
|
|
flag.Parse()
|
|
|
|
fileName := flag.Args()
|
|
|
|
if *quickPtr {
|
|
file := note + "/quick.md"
|
|
OpenNvim(file)
|
|
os.Exit(0)
|
|
}
|
|
|
|
if *listPtr {
|
|
files, err := os.ReadDir(note)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, f := range files {
|
|
file := f.Name()
|
|
fmt.Println(strings.ReplaceAll(file, ".md", ""))
|
|
}
|
|
os.Exit(0)
|
|
}
|
|
|
|
if *dailyPtr {
|
|
date := time.Now().Local().Format("2006-01-02")
|
|
file := daily + "/" + date + ".md"
|
|
dailyTemplate := templates + "/daily.md"
|
|
|
|
// If the file doesn't exist, create it
|
|
if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {
|
|
// Read template file
|
|
t, _ := os.ReadFile(dailyTemplate)
|
|
|
|
// Replace yesterday and tomorrow with specific dates
|
|
t = bytes.ReplaceAll(t, []byte("YESTERDAY"), []byte(time.Now().Local().AddDate(0, 0, -1).Format("2006-01-02")))
|
|
t = bytes.ReplaceAll(t, []byte("TOMORROW"), []byte(time.Now().Local().AddDate(0, 0, 1).Format("2006-01-02")))
|
|
|
|
// write the file
|
|
os.WriteFile(file, t, 0644)
|
|
}
|
|
|
|
OpenNvim(file)
|
|
os.Exit(0)
|
|
}
|
|
|
|
if len(fileName) == 0 {
|
|
fmt.Println("You didn't specify a note name")
|
|
os.Exit(1)
|
|
}
|
|
|
|
file := note + "/" + fileName[0] + ".md"
|
|
OpenNvim(file)
|
|
}
|
|
|
|
func OpenNvim(file string) {
|
|
nvim := exec.Command("nvim", file)
|
|
nvim.Stdin = os.Stdin
|
|
nvim.Stdout = os.Stdout
|
|
nvim.Stderr = os.Stderr
|
|
|
|
err := nvim.Run()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|