forked from rhysd/notes-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
105 lines (86 loc) · 2.77 KB
/
cmd.go
File metadata and controls
105 lines (86 loc) · 2.77 KB
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
package notes
import (
"github.com/fatih/color"
"github.com/mattn/go-colorable"
"gopkg.in/alecthomas/kingpin.v2"
"os"
)
// parsableCmd is an interface for subcommands of notes command parsed from command line arguments
type parsableCmd interface {
Do() error
defineCLI(*kingpin.Application)
matchesCmdline(string) bool
}
// Cmd is an interface for subcommands of notes command
type Cmd interface {
Do() error
}
// Version is version string of notes command. It conforms semantic versioning
var Version = "1.6.2"
var description = `Simple note taking tool for command line with your favorite editor.
You can manage (create/open/list) notes via this tool on terminal. notes also
optionally can save your notes thanks to Git to avoid losing your notes.
notes is intended to be used nicely with other commands such as grep (or ag, rg),
rm, filtering tools such as fzf or peco and editors which can be started from
command line.
notes is developed at https://github.com/rhysd/notes-cli. If you're seeing a bug or having a feature request,
please create a new issue. Pull requests are more than welcome.`
// ParseCmd parses given arguments as command line options and returns corresponding subcommand instance.
// When no subcommand matches or argus contains invalid argument, it returns an error
func ParseCmd(args []string) (Cmd, error) {
cli := kingpin.New("notes", description)
noColor := cli.Flag("no-color", "Disable color output").Bool()
colorAlways := cli.Flag("color-always", "Enable color output always").Short('A').Bool()
cli.Version(Version)
cli.Author("rhysd <https://github.com/rhysd>")
cli.HelpFlag.Short('h')
c, err := NewConfig()
if err != nil {
return nil, err
}
colorStdout := colorable.NewColorableStdout()
// When `notes` command is run with no argument,
// - if there is no note, show usage help
// - if there is one or more notes, show the list with `list --oneline`
// ref: #2
if len(args) == 0 {
if cats, err := CollectCategories(c, OnlyFirstCategory); err == nil && len(cats) > 0 {
return &ListCmd{
Config: c,
Out: colorStdout,
Oneline: true,
}, nil
}
}
cmds := []parsableCmd{
&NewCmd{Config: c},
&ListCmd{Config: c, Out: colorStdout},
&CategoriesCmd{Config: c, Out: os.Stdout},
&TagsCmd{Config: c, Out: os.Stdout},
&SaveCmd{Config: c},
&ConfigCmd{Config: c, Out: os.Stdout},
&SelfupdateCmd{Out: colorStdout},
}
for _, cmd := range cmds {
cmd.defineCLI(cli)
}
parsed, err := cli.Parse(args)
if err != nil {
if ext, ok := NewExternalCmd(err, args); ok {
return ext, nil
}
return nil, err
}
if *colorAlways {
color.NoColor = false
}
if *noColor {
color.NoColor = true
}
for _, cmd := range cmds {
if cmd.matchesCmdline(parsed) {
return cmd, nil
}
}
panic("FATAL: Unknown command: " + parsed)
}