-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (89 loc) · 2.2 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
// Copyright (C) Damien Dart, <damiendart@pobox.com>.
// This file is distributed under the MIT licence. For more information,
// please refer to the accompanying "LICENCE" file.
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/damiendart/nt/internal/cli"
"github.com/damiendart/nt/internal/editor"
)
// An Application is used to store any application-wide dependencies.
type Application struct {
Commands map[string]Command
Editor editor.Editor
Logger *log.Logger
NotesDir string
Output io.Writer
}
// Command is implemented by anything that has a Run method. The
// implementation can then be used as nt command.
type Command interface {
Run(app Application, normalisedArgs []string) error
}
func help(cmdMap map[string]Command) string {
return `HELP TEXT GOES HERE`
}
func main() {
var notesDir string
cmdMap := map[string]Command{
"inbox": &InboxCommand{},
"jot": &JotCommand{},
"new": &NewCommand{},
"tags": &TagsCommand{},
}
logger := log.New(os.Stderr, os.Args[0]+": ", 0)
globalOptions, remainingArgs, err := cli.ParseArgs(
os.Args[1:],
cli.Spec{
"?": cli.ValueOptional,
"h": cli.ValueOptional,
"help": cli.ValueOptional,
"notes-dir": cli.ValueRequired,
"version": cli.NoValueAccepted,
},
)
if err != nil {
logger.Fatalf(err.Error())
}
for k, v := range globalOptions {
switch {
case k == "?", k == "h", k == "help":
fmt.Println(help(cmdMap))
os.Exit(0)
case k == "version":
fmt.Println("VERSION GOES HERE")
os.Exit(0)
case k == "notes-dir":
notesDir = v
}
}
if len(remainingArgs) == 0 {
logger.Fatalf("missing command")
}
if notesDir == "" {
if env := os.Getenv("NOTES_ROOT"); env != "" {
notesDir = filepath.Clean(env)
} else {
logger.Fatalf("notes directory not set")
}
}
application := &Application{
Commands: cmdMap,
Editor: &editor.VimEditor{},
Logger: logger,
NotesDir: notesDir,
Output: os.Stdout,
}
command, ok := application.Commands[remainingArgs[0]]
if !ok {
application.Logger.Fatalf("invalid command: %q", remainingArgs[0])
}
err = command.Run(*application, remainingArgs[1:])
if err != nil {
application.Logger.Fatal(err)
}
}