-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (99 loc) · 2.59 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
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
129
130
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/gosimple/conf"
)
var cfg *conf.Config
var notemanager Config
var aliases NoteAliases
func main() {
// disable timestamp in Fatal Logs
log.SetFlags(0)
// return exit code 1 after runtime.Goexit() function
// used in func Exit(string).
// If runtime finishes normally, we need to manually
// exit with os.Exit(0).
defer os.Exit(1)
notemanager = parseConfig()
var optHelp bool
var optAll bool
fs := flag.NewFlagSet("note", flag.ContinueOnError)
fs.Usage = func() { helpNote() }
fs.BoolVar(&optAll, "a", false, "Select all notes in filter, include deleted")
fs.BoolVar(&optAll, "all", false, "Select all notes in filter, include deleted")
fs.BoolVar(&optHelp, "h", false, "Display usage")
fs.BoolVar(&optHelp, "help", false, "Display usage")
if err := fs.Parse(os.Args[1:]); err != nil {
return
}
if optHelp {
helpNote()
}
// remaining args
rargs := fs.Args()
aliases = make(NoteAliases)
aliases.Load()
// expected cmd syntax: ./note [ FILTER ] cmd args
filter, rargs, err := parseFilter(rargs)
if err != nil {
Exit(`Failed to parse arguments`)
}
if optAll {
filter.IncludeDeleted = true
}
if len(rargs) == 0 {
helpNote()
}
notes, err := notes(filter)
/*
Handlers which pass the filter should be cleaned up, because we
built the note selection already. No need to process and pass the
filter, we can already pass the note selection, i.e. the []Note
slice.
Out commented handlers need to be created.
*/
switch rargs[0] {
case "add":
addHandler(rargs[1:])
case "alias":
aliasHandler(filter, notes, rargs[1:])
case "delete":
deleteHandler(notes, rargs[1:])
case "edit":
editHandler(notes, rargs[1:])
case "file":
fileHandler(notes, rargs[1:])
case "list":
listHandler(filter, rargs[1:])
case "modify":
modifyHandler(filter, notes, rargs[1:])
case "print":
printHandler(notes, rargs[1:])
//case "purge":
// purgeHandler(notes, rargs[1:])
case "read":
readHandler(notes, rargs[1:])
case "search":
searchHandler(filter, rargs[1:])
case "tags":
tagsHandler(filter, rargs[1:])
case "undelete":
undeleteHandler(notes, rargs[1:])
case "version":
fmt.Println(`Notemanager Version 0.63.1-alpha
Author: Leon Kramer <leonkramer@gmail.com>`)
case "versions":
versionsHandler(notes, rargs[1:])
default:
// this handler should be replaced by
// other specific handlers
Exit("Unknown command")
}
// runtime ended properly, so exit with code 0.
// this is necessary as we deferred os.Exit(1) initially,
// so runtime.Goexit() returns a proper exit code.
os.Exit(0)
}