-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (86 loc) · 1.85 KB
/
main.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
/*
A CLI app to store your notes.
written in go.
*/
package main
import (
"bufio"
"log"
"os"
"strings"
"github.com/MeztliRA/gemdot/color"
"github.com/MeztliRA/gemdot/config"
c "github.com/MeztliRA/gemdot/constants"
"github.com/MeztliRA/gemdot/note"
u "github.com/MeztliRA/gemdot/utils"
fc "github.com/fatih/color"
cfg "github.com/olebedev/config"
)
func init() {
// set log
log.SetPrefix("gemdot: ")
log.SetFlags(0)
}
func main() {
// check for files
note.Check()
config.Check()
// read files
notes := note.Read()
config := config.Read()
// read and run user's commands
userCommand(notes, config)
}
func userCommand(notes []string, config *cfg.Config) {
reader := bufio.NewReader(os.Stdin)
// apply user's config
if noColor, err := config.Bool("no-color"); noColor && err == nil {
fc.NoColor = true
} else if err != nil {
fc.NoColor = false
} else {
fc.NoColor = false
}
if greet, err := config.Bool("greeting"); greet && err == nil || err != nil {
u.PrintGreeting()
}
// main loop
firstTime := true
for {
if firstTime {
color.Green(c.CommandMessage)
} else {
color.Green("\n" + c.CommandMessage)
}
// take user input
response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
response = strings.TrimSpace(response)
switch response {
case "View", "view", "VIEW":
note.View(notes)
case "Add", "add", "ADD":
notes = note.Add(notes)
note.Overwrite(notes)
case "Delete", "delete", "DELETE":
notes = note.Delete(notes)
note.Overwrite(notes)
case "Clear", "clear", "CLEAR":
notesGet, cleared := note.Clear()
if cleared {
note.Overwrite(notesGet)
}
case "Version", "version", "VERSION":
u.PrintVersion()
case "Help", "help", "HELP":
u.PrintHelp()
case "Quit", "quit", "QUIT":
os.Exit(0)
default:
color.Red("unknown action")
}
firstTime = false
}
}