-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcmd_tags.go
More file actions
73 lines (62 loc) · 1.82 KB
/
cmd_tags.go
File metadata and controls
73 lines (62 loc) · 1.82 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
package notes
import (
"fmt"
"github.com/pkg/errors"
"io"
"sort"
"strings"
"gopkg.in/alecthomas/kingpin.v2"
)
// TagsCmd represents `notes tags` command. Each public fields represent options of the command
// Out field represents where this command should output.
type TagsCmd struct {
cli *kingpin.CmdClause
Config *Config
// Category is a category name of tags. If this value is empty, tags of all categories will be output
Category string
// Out is a writer to write output of this command. Kind of stdout is expected
Out io.Writer
}
func (cmd *TagsCmd) defineCLI(app *kingpin.Application) {
cmd.cli = app.Command("tags", "List all tags")
cmd.cli.Arg("category", "Show tags of specified category. If not specified, all tags are output").StringVar(&cmd.Category)
}
func (cmd *TagsCmd) matchesCmdline(cmdline string) bool {
return cmd.cli.FullCommand() == cmdline
}
// Do runs `notes tags` command and returns an error if occurs
func (cmd *TagsCmd) Do() error {
saw := map[string]struct{}{}
tags := []string{}
cats, err := CollectCategories(cmd.Config, 0)
if err != nil {
return err
}
if cmd.Category != "" {
// Even if category is specified, we fetch all categories since error message requires
// all category names for suggestion.
cat, ok := cats[cmd.Category]
if !ok {
ns := cats.Names()
return errors.Errorf("Category '%s' does not exist. All categories are %s", cmd.Category, strings.Join(ns, ", "))
}
cats = Categories{cmd.Category: cat}
}
for _, cat := range cats {
notes, err := cat.Notes(cmd.Config)
if err != nil {
return err
}
for _, n := range notes {
for _, tag := range n.Tags {
if _, ok := saw[tag]; !ok {
tags = append(tags, tag)
saw[tag] = struct{}{}
}
}
}
}
sort.Strings(tags)
_, err = fmt.Fprintln(cmd.Out, strings.Join(tags, "\n"))
return err
}