-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (117 loc) · 3.19 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"github.com/alexflint/go-arg"
"github.com/femnad/barn/selection"
)
const (
name = "barn"
version = "v0.1.0"
)
type commonArgs struct {
Config string `arg:"-f,--file" default:"~/.config/barn/barn.yml" help:"Config file path"`
}
type commonWithId struct {
commonArgs
Id string `arg:"positional,required" help:"Selection Id"`
}
type bucketsCmd struct {
commonArgs
}
type outputCmd struct {
commonWithId
ShowZeroCounts bool `arg:"-z,--zero" help:"Show entries with zero counts"`
}
type purgeCmd struct {
commonArgs
Ids []string `arg:"positional,required"`
}
type truncateCmd struct {
commonWithId
Pattern []string `arg:"positional,required" help:"Pattern for keys to truncate"`
}
type chooseCmd struct {
commonWithId
ExtraArgs string `arg:"-e,--extra" help:"Extra arguments for the select action"`
Selection string `arg:"positional" help:"ID for selector action"`
}
type args struct {
Buckets *bucketsCmd `arg:"subcommand:buckets" help:"List existing buckets"`
Choose *chooseCmd `arg:"subcommand:choose" help:"Make a selection based on given choices and update counts"`
Output *outputCmd `arg:"subcommand:output" help:"Show stored entries for the given selection ID"`
Purge *purgeCmd `arg:"subcommand:purge" help:"Purge given bucket"`
Truncate *truncateCmd `arg:"subcommand:truncate" help:"Truncate the desired keys for the given bucket"`
}
func (args) Version() string {
return fmt.Sprintf("%s %s", name, version)
}
func showSelections(config, id, extraArgs string) {
err := selection.Show(config, id, extraArgs)
if err != nil {
log.Fatalf("error getting selections for id %s: %v", id, err)
}
}
func markSelection(config, id, choice, extraArgs string) {
exitCode, err := selection.Mark(config, id, choice, extraArgs)
if err != nil {
log.Fatalf("error marking selection as %s for id %s: %v", choice, id, err)
}
if exitCode != 0 {
os.Exit(exitCode)
}
}
func doList(cmd *bucketsCmd) {
err := selection.ListBuckets(cmd.Config)
if err != nil {
log.Fatalf("error listing buckets: %v", err)
}
}
func doOutput(cmd *outputCmd) {
err := selection.Iterate(cmd.Config, cmd.Id, cmd.ShowZeroCounts)
if err != nil {
log.Fatalf("error iterating over bucket %s: %v", cmd.Id, err)
}
}
func doPurge(cmd *purgeCmd) {
err := selection.Purge(cmd.Config, cmd.Ids)
if err != nil {
log.Fatalf("error purging buckets: %v", err)
}
}
func doSelect(cmd *chooseCmd) {
if cmd.Selection == "" {
showSelections(cmd.Config, cmd.Id, cmd.ExtraArgs)
return
}
markSelection(cmd.Config, cmd.Id, cmd.Selection, cmd.ExtraArgs)
}
func doTruncate(cmd *truncateCmd) {
err := selection.Truncate(cmd.Config, cmd.Id, cmd.Pattern)
if err != nil {
log.Fatalf("error truncating keys from bucket %s: %v", cmd.Id, err)
}
}
func main() {
var parsed args
p := arg.MustParse(&parsed)
if p.Subcommand() == nil {
p.WriteHelp(os.Stderr)
os.Exit(1)
}
switch {
case parsed.Buckets != nil:
doList(parsed.Buckets)
case parsed.Choose != nil:
doSelect(parsed.Choose)
case parsed.Output != nil:
doOutput(parsed.Output)
case parsed.Purge != nil:
doPurge(parsed.Purge)
case parsed.Truncate != nil:
doTruncate(parsed.Truncate)
default:
p.WriteHelp(os.Stderr)
}
}