forked from hoop33/limo
/
untag.go
64 lines (53 loc) · 1.53 KB
/
untag.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
package cmd
import (
"fmt"
"github.com/hoop33/limo/config"
"github.com/hoop33/limo/model"
"github.com/spf13/cobra"
)
// UntagCmd tags a star
var UntagCmd = &cobra.Command{
Use: "untag <star> [tag]...",
Short: "Untag a star",
Long: "Untag the star identified by <star> with the tags specified by [tag], or all if [tag] not specified.",
Example: fmt.Sprintf(" %s untag limo gui", config.ProgramName),
Run: func(cmd *cobra.Command, args []string) {
output := getOutput()
if len(args) == 0 {
output.Fatal("You must specify a star (and optionally a tag)")
}
db, err := getDatabase()
fatalOnError(err)
stars, err := model.FuzzyFindStarsByName(db, args[0])
fatalOnError(err)
checkOneStar(args[0], stars)
output.StarLine(&stars[0])
if len(args) == 1 {
// Untag all
fatalOnError(stars[0].RemoveAllTags(db))
output.Info(fmt.Sprintf("Removed all tags"))
} else {
fatalOnError(stars[0].LoadTags(db))
for _, tagName := range args[1:] {
tag, err := model.FindTagByName(db, tagName)
if err != nil {
output.Error(err.Error())
} else if tag == nil {
output.Error(fmt.Sprintf("Tag '%s' does not exist", tagName))
} else if !stars[0].HasTag(tag) {
output.Error(fmt.Sprintf("'%s' isn't tagged with '%s'", *stars[0].FullName, tagName))
} else {
err = stars[0].RemoveTag(db, tag)
if err != nil {
output.Error(err.Error())
} else {
output.Info(fmt.Sprintf("Removed tag '%s'", tag.Name))
}
}
}
}
},
}
func init() {
RootCmd.AddCommand(UntagCmd)
}