gime-flat/ops_tag.go
2018-08-02 15:05:31 -05:00

67 lines
1.2 KiB
Go

package main
import (
"fmt"
"strings"
)
func cmdManageTag(args []string) int {
if len(args) == 0 {
return cmdListTags(args)
}
return cmdEditTag(args)
}
func cmdListTags(args []string) int {
m, err := LoadMetadata()
if err != nil {
fmt.Println("Error loading tags")
return 1
}
fmt.Println("[")
for _, v := range m.Tags {
fmt.Println(" " + v.String())
}
fmt.Println("]")
return 0
}
func cmdEditTag(args []string) int {
m, err := LoadMetadata()
if err != nil {
fmt.Println("Error loading tags")
return 1
}
// The first argument should be the tag name that we want to edit
tagName := args[0]
if tagName[0] == '+' {
tagName = tagName[1:]
}
tg := m.GetTag(tagName)
if tg == nil {
tg = NewTag(tagName, false)
}
for _, v := range args[1:] {
switch v[0] {
case '+':
// Altering tag name
tg.Name = v[1:]
case ':':
// Altering billable flag
if strings.HasPrefix(v, ":non") {
tg.SetBillable(false)
} else if strings.HasPrefix(v, ":bill") {
tg.SetBillable(true)
}
}
}
m.SaveTag(tg)
if err = SaveMetadata(m); err != nil {
fmt.Println("Error saving metadata: " + err.Error())
return 1
}
fmt.Println(tg.String())
return 0
}