This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
356 lines (301 loc) · 8.05 KB
/
cmd.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//go:generate go run manpage_generate.go cmd.go gostore.go config.go
package main
import (
"fmt"
"strings"
yaml "gopkg.in/yaml.v2"
"github.com/pirmd/clapp"
)
func newApp(cfg *Config) *clapp.Command {
cmd := &clapp.Command{
Name: "gostore",
Usage: "A command-line minimalist media collection manager.",
Description: "gostore is a command line tool aiming at providing facilities to manage one or more collections of media files, keeping track of their metadata and/or additional information the user wants to record.",
Config: &clapp.Config{
Unmarshaller: yaml.Unmarshal,
Files: clapp.DefaultConfigFiles("config.yaml"),
Var: cfg,
},
ShowHelp: clapp.ShowUsage,
ShowVersion: clapp.ShowVersion,
}
cmd.Flags = clapp.Flags{
{
Name: "verbose",
Usage: "Show verbose information.",
Var: &cfg.Verbose,
},
{
Name: "debug",
Usage: "Show debug information.",
Var: &cfg.Debug,
},
{
Name: "root",
Usage: "Path to the root of the collection.",
Var: &cfg.Store.Path,
},
{
Name: "pretend",
Usage: "Operations that modify the collection are simulated.",
Var: &cfg.ReadOnly,
},
{
Name: "auto",
Usage: "Perform operations without manual interaction from the user.",
Var: &cfg.UI.Auto,
},
{
Name: "style",
Usage: "Style for printing records' details. Available styles are defined in the configuration file.",
Var: &cfg.UI.OutputFormat,
},
}
var mediaPath []string
cmd.SubCommands.Add(&clapp.Command{
Name: "import",
Usage: "Import a new media into the collection.",
Args: clapp.Args{
{
Name: "media",
Usage: "Media to import into the collection.",
Var: &mediaPath,
},
},
Execute: func() error {
gs, err := openGostore(cfg)
if err != nil {
return err
}
defer gs.Close()
if err := gs.Import(mediaPath); err != nil {
return err
}
return nil
},
})
var recordIDs []string
var recordIDsArg = &clapp.Arg{
Name: "name",
Usage: "Record's name. Name can be specified using a glob pattern.",
Var: &recordIDs,
Optional: true,
}
var sortBy []string
var sortByFlag = &clapp.Flag{
Name: "sort",
Usage: "Sort the search results. Record will first be sorted by the first field. Any items with the same value for that field, are then also sorted by the next field, and so on. The names of fields can be prefixed with the - character, which will cause that field to be reversed (descending order). Special fields are provided '_id' (record's name) and '_score' (search relevance score).",
Var: &sortBy,
}
cmd.SubCommands.Add(&clapp.Command{
Name: "list",
Usage: "List and retrieve information about collection's records. If no pattern is provided, list all records of the collection.",
Args: clapp.Args{
recordIDsArg,
},
Flags: clapp.Flags{
sortByFlag,
},
Execute: func() error {
gs, err := openGostore(cfg)
if err != nil {
return err
}
defer gs.Close()
if len(recordIDs) == 0 {
if err := gs.ListAll(sortBy); err != nil {
return err
}
return nil
}
if err := gs.ListGlob(recordIDs, sortBy); err != nil {
return err
}
return nil
},
})
var query string
cmd.SubCommands.Add(&clapp.Command{
Name: "search",
Usage: "Search the collection's records matching the given query.",
Args: clapp.Args{
{
Name: "query",
Usage: "Query to match records against. Query pattern follows blevesearch query language (https://blevesearch.com/docs/Query-String-Query/).",
Var: &query,
},
},
Flags: clapp.Flags{
sortByFlag,
},
Execute: func() error {
gs, err := openGostore(cfg)
if err != nil {
return err
}
defer gs.Close()
if err := gs.ListQuery(query, sortBy); err != nil {
return err
}
return nil
},
})
var multiEdit bool
cmd.SubCommands.Add(&clapp.Command{
Name: "edit",
Usage: "Edit an existing record from the collection using user defined's editor. If flag '--auto' is used, edition is skipped and nothing happens.",
Flags: clapp.Flags{
{
Name: "multi-edit",
Usage: "Edit multiple records at once instead of individually. Make sure when editing to not modify records order not do delete or add one.",
Var: &multiEdit,
},
{
Name: "import-orphans",
Usage: "Delete any database entry that does not correspond to an existing file in the store's filesystem (so called ghost record)",
Var: &cfg.ImportOrphans,
},
},
Args: clapp.Args{
recordIDsArg,
},
Execute: func() error {
gs, err := openGostore(cfg)
if err != nil {
return err
}
defer gs.Close()
if multiEdit {
if err := gs.MultiEdit(recordIDs); err != nil {
return err
}
return nil
}
if err := gs.Edit(recordIDs); err != nil {
return err
}
return nil
},
})
cmd.SubCommands.Add(&clapp.Command{
Name: "delete",
Usage: "Delete an existing record from the collection.",
Args: clapp.Args{
recordIDsArg,
},
Execute: func() error {
gs, err := openGostore(cfg)
if err != nil {
return err
}
defer gs.Close()
if err := gs.Delete(recordIDs); err != nil {
return err
}
return nil
},
})
var dstFolder string
cmd.SubCommands.Add(&clapp.Command{
Name: "export",
Usage: "Copy a record's media file from the collection to the given destination.",
Args: clapp.Args{
recordIDsArg,
{
Name: "dst",
Usage: "Destination folder where the record needs to be exported to. Default to current working directory.",
Var: &dstFolder,
Optional: true,
},
},
Execute: func() error {
gs, err := openGostore(cfg)
if err != nil {
return err
}
defer gs.Close()
if err := gs.Export(dstFolder, recordIDs); err != nil {
return err
}
return nil
},
})
cmd.SubCommands.Add(&clapp.Command{
Name: "check",
Usage: "Verify collection's consistency and repairs or reports found inconsistencies.",
Flags: clapp.Flags{
{
Name: "delete-ghosts",
Usage: "Delete any database entries that does not correspond to an existing file in the store's filesystem (so called ghost record)",
Var: &cfg.DeleteGhosts,
},
{
Name: "delete-orphans",
Usage: "Delete any file of the store's filesystem that is not recorded in the store's database.",
Var: &cfg.DeleteOrphans,
},
{
Name: "import-orphans",
Usage: "Delete any database entry that does not correspond to an existing file in the store's filesystem (so called ghost record)",
Var: &cfg.ImportOrphans,
},
},
Execute: func() error {
gs, err := openGostore(cfg)
if err != nil {
return err
}
defer gs.Close()
if err := gs.CheckAndRepair(); err != nil {
return err
}
return nil
},
})
cmd.SubCommands.Add(&clapp.Command{
Name: "rebuild-index",
Usage: "Deletes then rebuild the collection's index from scratch. Useful for example to implement a new mapping strategy or if things are really going bad.",
Execute: func() error {
gs, err := openGostore(cfg)
if err != nil {
return err
}
defer gs.Close()
if err := gs.RebuildIndex(); err != nil {
return err
}
return nil
},
})
cmd.SubCommands.Add(&clapp.Command{
Name: "fields",
Usage: "Lists fields names that are available for search or for templates. Some fields might only be available for a given media Type.",
Execute: func() error {
gs, err := openGostore(cfg)
if err != nil {
return err
}
defer gs.Close()
if err := gs.Fields(); err != nil {
return err
}
return nil
},
})
cmd.SubCommands.Add(&clapp.Command{
Name: "config",
Usage: "Lists known modules, analyzers or styles that can be used for gostore configuration or invocation.",
Execute: func() error {
gs, err := openGostore(cfg)
if err != nil {
return err
}
defer gs.Close()
fmt.Printf("Known Modules:\n%s\n", strings.Join(cfg.Modules(), "\n"))
fmt.Printf("\nKnown Analyzers:\n%s\n", strings.Join(cfg.Analyzers(), "\n"))
fmt.Printf("\nKnown Styles:\n%s\n", strings.Join(cfg.Styles(), "\n"))
return nil
},
})
return cmd
}