-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcmd_categories.go
45 lines (37 loc) · 1.2 KB
/
cmd_categories.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
package notes
import (
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
"io"
"sort"
"strings"
)
// CategoriesCmd represents `notes categories` command. Each public fields represent options of the command.
// Out field represents where this command should output.
type CategoriesCmd struct {
cli, cliAlias *kingpin.CmdClause
Config *Config
// Out is a writer to write output of this command. Kind of stdout is expected
Out io.Writer
}
func (cmd *CategoriesCmd) defineCLI(app *kingpin.Application) {
cmd.cli = app.Command("categories", "List all categories to stdout (alias: cats)")
cmd.cliAlias = app.Command("cats", "List all categories to stdout. Please do not expect 🐱!").Hidden()
}
func (cmd *CategoriesCmd) matchesCmdline(cmdline string) bool {
return cmd.cli.FullCommand() == cmdline || cmd.cliAlias.FullCommand() == cmdline
}
// Do runs `notes categories` command and returns an error if occurs
func (cmd *CategoriesCmd) Do() error {
cats, err := CollectCategories(cmd.Config, 0)
if err != nil {
return err
}
names := make([]string, 0, len(cats))
for c := range cats {
names = append(names, c)
}
sort.Strings(names)
_, err = fmt.Fprintln(cmd.Out, strings.Join(names, "\n"))
return err
}