-
Notifications
You must be signed in to change notification settings - Fork 115
/
values.go
190 lines (157 loc) · 5.08 KB
/
values.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2011-2018 Paul Ruane.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cli
import (
"fmt"
"github.com/oniony/TMSU/common/log"
"github.com/oniony/TMSU/common/terminal"
"github.com/oniony/TMSU/storage"
"strings"
)
var ValuesCommand = Command{
Name: "values",
Synopsis: "List values",
Usages: []string{"tmsu values [OPTION]... [TAG]..."},
Description: "Lists the values for TAGs. If no TAG is specified then all tags are listed.",
Examples: []string{"$ tmsu values year\n2000\n2001\n2017",
"$ tmsu values\n2000\n2001\n2017\ncheese\nopera",
"$ tmsu values --count year\n3"},
Options: Options{{"--count", "-c", "lists the number of values rather than their names", false, ""},
{"", "-1", "list one value per line", false, ""}},
Exec: valuesExec,
}
// unexported
func valuesExec(options Options, args []string, databasePath string) (error, warnings) {
showCount := options.HasOption("--count")
onePerLine := options.HasOption("-1")
store, err := openDatabase(databasePath)
if err != nil {
return err, nil
}
defer store.Close()
tx, err := store.Begin()
if err != nil {
return err, nil
}
defer tx.Commit()
if len(args) == 0 {
return listAllValues(store, tx, showCount, onePerLine), nil
}
return listValues(store, tx, args, showCount, onePerLine)
}
func listAllValues(store *storage.Storage, tx *storage.Tx, showCount, onePerLine bool) error {
log.Info(2, "retrieving all values.")
if showCount {
count, err := store.ValueCount(tx)
if err != nil {
return fmt.Errorf("could not retrieve value count: %v", err)
}
fmt.Println(count)
} else {
values, err := store.Values(tx)
if err != nil {
return fmt.Errorf("could not retrieve values: %v", err)
}
if onePerLine {
for _, value := range values {
fmt.Println(escape(value.Name))
}
} else {
valueNames := make([]string, len(values))
for index, value := range values {
valueNames[index] = escape(value.Name)
}
terminal.PrintColumns(valueNames)
}
}
return nil
}
func listValues(store *storage.Storage, tx *storage.Tx, args []string, showCount, onePerLine bool) (error, warnings) {
tagNames := make([]string, len(args))
for index, arg := range args {
tagNames[index] = parseTagOrValueName(arg)
}
switch len(tagNames) {
case 0:
return fmt.Errorf("at least one tag must be specified"), nil
case 1:
return listValuesForTag(store, tx, tagNames[0], showCount, onePerLine), nil
default:
return listValuesForTags(store, tx, tagNames, showCount, onePerLine)
}
}
func listValuesForTag(store *storage.Storage, tx *storage.Tx, tagName string, showCount, onePerLine bool) error {
tag, err := store.TagByName(tx, tagName)
if err != nil {
return fmt.Errorf("could not retrieve tag '%v': %v", tagName, err)
}
if tag == nil {
return fmt.Errorf("no such tag, '%v'", tagName)
}
log.Infof(2, "retrieving values for tag '%v'.", tagName)
values, err := store.ValuesByTag(tx, tag.Id)
if err != nil {
return fmt.Errorf("could not retrieve values for tag '%v': %v", tagName, err)
}
if showCount {
fmt.Println(len(values))
} else {
if onePerLine {
for _, value := range values {
fmt.Println(escape(value.Name, '=', ' '))
}
} else {
valueNames := make([]string, len(values))
for index, value := range values {
valueNames[index] = escape(value.Name, '=', ' ')
}
terminal.PrintColumns(valueNames)
}
}
return nil
}
func listValuesForTags(store *storage.Storage, tx *storage.Tx, tagNames []string, showCount, onePerLine bool) (error, warnings) {
warnings := make(warnings, 0, 10)
for _, tagName := range tagNames {
tag, err := store.TagByName(tx, tagName)
if err != nil {
return fmt.Errorf("could not retrieve tag '%v': %v", tagName, err), warnings
}
if tag == nil {
warnings = append(warnings, fmt.Sprintf("no such tag, '%v'.", tagName))
continue
}
log.Infof(2, "retrieving values for tag '%v'.", tagName)
values, err := store.ValuesByTag(tx, tag.Id)
if err != nil {
return fmt.Errorf("could not retrieve values for tag '%v': %v", tagName, err), warnings
}
if showCount {
fmt.Printf("%v: %v\n", tagName, len(values))
} else {
if onePerLine {
fmt.Println(tagName)
for _, value := range values {
fmt.Println(escape(value.Name, '=', ' '))
}
fmt.Println()
} else {
valueNames := make([]string, len(values))
for index, value := range values {
valueNames[index] = escape(value.Name, '=', ' ')
}
fmt.Printf("%v: %v\n", tagName, strings.Join(valueNames, " "))
}
}
}
return nil, warnings
}