main.go raw
1 // This is just a convenient cli command to automatically generate a new file
2 // for a journal entry with names based on unix timestamps
3 package main
4
5 import (
6 "encoding/json"
7 "fmt"
8 "io/ioutil"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "time"
13 )
14
15 type jrnlCfg struct {
16 Root string
17 }
18
19 func printErrorAndDie(stuff ...interface{}) {
20 fmt.Fprintln(os.Stderr, stuff)
21 os.Exit(1)
22 }
23
24 func main() {
25 var home string
26 var e error
27 if home, e = os.UserHomeDir(); e != nil {
28 os.Exit(1)
29 }
30 var configFile []byte
31 if configFile, e = ioutil.ReadFile(
32 filepath.Join(home, ".jrnl")); e != nil {
33 printErrorAndDie(e, "~/.jrnl configuration file not found")
34 }
35 var cfg jrnlCfg
36 if e = json.Unmarshal(configFile, &cfg); e != nil {
37 printErrorAndDie(e, "~/.jrnl config file did not unmarshal")
38 }
39 filename := filepath.Join(cfg.Root, fmt.Sprintf("jrnl%d.txt",
40 time.Now().Unix()))
41 if e = ioutil.WriteFile(filename,
42 []byte(time.Now().Format(time.RFC1123Z)+"\n\n"),
43 0600,
44 ); e != nil {
45 printErrorAndDie(e,
46 "unable to create file (is your keybase filesystem mounted?")
47 os.Exit(1)
48 }
49 exec.Command("gedit", filename).Run()
50 }
51