-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
61 lines (54 loc) · 1.46 KB
/
list.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
package main
import (
"context"
"fmt"
"github.com/jessevdk/go-flags"
)
func list(ctx context.Context, p *flags.Parser) error {
// collects args provided by user
var opts optsList
// populate into struct
ok := opts.set(p)
if !ok {
fmt.Printf("%s cmd line args cannot be parsed!\n", red("ERROR"))
}
// create firestore client using source file's project_id and credentials
conf := fsClientConfig{c: opts.Source}
c := createFirestoreClient(ctx, conf)
// build query //
tests := c.Collection("Tests").Where("Tag", "array-contains", opts.Tag).Limit(opts.Results)
// run query //
testList, err := tests.Documents(ctx).GetAll()
if err != nil {
fmt.Printf("%s querying collection\n", red("ERROR"))
}
fmt.Printf("Search results for tag: %s\n", yellow(opts.Tag))
// pretty print results //
for _, doc := range testList {
data := doc.Data()
name, ok := data["Name"].(string)
if !ok {
fmt.Printf("%s no name key found %v\n", red("ERROR"), data)
continue
}
fmt.Printf("%s : %v\n", blue("name"), green(name))
}
return nil
}
// Methods //
// convert and set user-provided values into opts struct
func (o *optsList) set(p *flags.Parser) (ok bool) {
o.Source, ok = p.Active.FindOptionByLongName("source").Value().(string)
if !ok {
return false
}
o.Results, ok = p.Active.FindOptionByLongName("results").Value().(int)
if !ok {
return false
}
o.Tag, ok = p.Active.FindOptionByLongName("tag").Value().(string)
if !ok {
return false
}
return true
}