-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
128 lines (117 loc) · 2.57 KB
/
manager.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
120
121
122
123
124
125
126
127
128
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"sort"
"strings"
)
// Manager for notes that uses /Documents/notes directory
type Manager struct {
homeDir string
editor string
perm os.FileMode
}
// NewManager creates an instance of the manager
func NewManager() (*Manager, error) {
usr, err := user.Current()
if err != nil {
return nil, err
}
home := fmt.Sprintf("%s/Documents/notes", usr.HomeDir)
var perm os.FileMode = 0744
if err := os.Mkdir(home, perm); !os.IsExist(err) {
return nil, err
}
editor := os.Getenv("EDITOR")
return &Manager{
homeDir: home,
editor: editor,
perm: perm,
}, nil
}
// setHomeDir for the manager. This is used only for testing
// and should not be used for any other reason
func (m *Manager) setHomeDir(dir string) error {
err := os.MkdirAll(dir, m.perm)
if err != nil {
return err
}
m.homeDir = dir
return nil
}
// List the notes available
func (m *Manager) List() ([]string, error) {
noteIDs := make([]string, 0)
err := filepath.Walk(m.homeDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ".md") {
noteID := strings.TrimSuffix(path, ".md")
noteID = strings.TrimPrefix(noteID, m.homeDir+"/")
noteIDs = append(noteIDs, noteID)
}
return nil
})
if err != nil {
return nil, err
}
sort.Strings(noteIDs)
return noteIDs, nil
}
// Remove a note by note id
func (m *Manager) Remove(noteID string) error {
p := m.getNotePath(noteID)
return os.Remove(p)
}
// Create a new note by note id
func (m *Manager) Create(noteID string) error {
ok, err := m.Exists(noteID)
if err != nil {
return err
}
if ok {
return errors.New("cannot create duplicate file")
}
p := m.getNotePath(noteID)
d := filepath.Dir(p)
if err := os.MkdirAll(d, m.perm); err != nil {
return err
}
f, err := os.Create(p)
if err != nil {
return err
}
err = f.Chmod(m.perm)
return err
}
// Exists checks if the note exits
func (m *Manager) Exists(noteID string) (bool, error) {
p := m.getNotePath(noteID)
if _, err := os.Stat(p); os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
} else {
return true, nil
}
}
// Edit the note given a note id
func (m *Manager) Edit(noteID string) error {
p := m.getNotePath(noteID)
if _, err := os.Stat(p); err != nil {
return err
}
cmd := exec.Command(m.editor, p)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (m *Manager) getNotePath(noteID string) string {
return fmt.Sprintf("%s/%s.md", m.homeDir, noteID)
}