forked from kramphub/kiya
/
cmd_list.go
55 lines (48 loc) · 1.37 KB
/
cmd_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
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
cloudstore "cloud.google.com/go/storage"
"github.com/emicklei/tre"
"github.com/olekukonko/tablewriter"
"golang.org/x/net/context"
"google.golang.org/api/iterator"
)
func commandList(storageService *cloudstore.Client, target profile, filter string) {
ctx := context.Background()
bucket := storageService.Bucket(target.Bucket)
query := &cloudstore.Query{}
it := bucket.Objects(ctx, query)
data := [][]string{}
filteredCount := 0
for {
next, err := it.Next()
if err == iterator.Done {
break
} else if err != nil {
log.Fatal(tre.New(err, "list failed"))
}
if len(filter) > 0 {
if !caseInsensitiveContains(next.Name, filter) {
filteredCount++
continue
}
}
data = append(data, []string{fmt.Sprintf("kiya %s copy %s", target.Label, next.Name), next.Created.Format(time.RFC822), next.Owner})
}
if len(filter) > 0 {
fmt.Printf("Showing %d key(s) matching '%s', skipped %d key(s)\n", len(data), filter, filteredCount)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Copy to clipboard command", "Created", "Creator"})
table.AppendBulk(data)
table.Render() // writes to stdout
}
func caseInsensitiveContains(key, filter string) bool {
key, filter = strings.ToLower(key), strings.ToLower(filter)
return strings.Contains(key, filter)
}