/
zet.go
119 lines (95 loc) · 2.71 KB
/
zet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package zet
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/spf13/viper"
)
type ZettelkastenNote struct {
VaultDir string
Filename string
OriginalTags []string
}
func NewZettelkastenNote(vaultDir string, filename string, tags []string) *ZettelkastenNote {
return &ZettelkastenNote{
VaultDir: vaultDir,
Filename: filename,
OriginalTags: tags,
}
}
func (note *ZettelkastenNote) GetFilepath() string {
// Return the filename of the note.
return fmt.Sprintf("%s/%s.md", note.VaultDir, note.Filename)
}
func (note *ZettelkastenNote) FileExists() (bool, string, error) {
noteFilePath := note.GetFilepath()
_, err := os.Stat(noteFilePath)
if err == nil {
return true, noteFilePath, nil
}
if os.IsNotExist(err) {
return false, noteFilePath, nil
}
return false, noteFilePath, err
}
// TODO use templates rather than building frontmatter manually
func (note *ZettelkastenNote) Create() (bool, error) {
// Create the note file.
file, err := os.Create(note.GetFilepath())
if err != nil {
return false, err
}
// Set up the YAML frontmatter.
frontmatter := fmt.Sprintf("---\ntitle: %s\ndate: %s\ntags:\n", note.Filename, time.Now().Format("2006-01-02"))
// Iterate over tags and add them to the frontmatter.
for _, tag := range note.OriginalTags {
frontmatter += fmt.Sprintf(" - %s\n", tag)
}
// Add the rest of the frontmatter.
frontmatter += "---\n\n\n## Links:\n\n"
file.WriteString(frontmatter)
return true, nil
}
func (note *ZettelkastenNote) Open() error {
exists, filePath, existsErr := note.FileExists()
if existsErr != nil {
return existsErr
}
if !exists {
fmt.Println("error: Note with given title does not exist in the vault directory.")
fmt.Println("hint: Try again with a new title, or run 'zet-cli open [title]' again with a create (-c) flag to create an empty note forcefully.")
os.Exit(1)
}
fmt.Println("Opening file:", filePath)
editor := viper.GetString("editor")
editorArgs := viper.GetString("nvimargs")
// We will split the command into arguments
var cmdArgs []string
if editorArgs != "" {
// User specified command
cmdArgs = strings.Fields(editorArgs)
cmdArgs = append([]string{editor, filePath}, cmdArgs...)
} else {
// Default to just opening nvim if no command is specified
cmdArgs = []string{editor, filePath}
}
// Open the note in Editor.
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Start the command and wait for it to finish
err := cmd.Start()
if err != nil {
fmt.Println("Error starting Neovim:", err)
return err
}
err = cmd.Wait()
if err != nil {
fmt.Println("Error waiting for Neovim to close:", err)
return err
}
return nil
}