-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
130 lines (104 loc) · 2.31 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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"github.com/ababo/mastore/store"
logpkg "log"
"os"
"path/filepath"
"strings"
)
func exeName() string {
return filepath.Base(os.Args[0])
}
func printUsage() {
usage := "Usage: %s (read|write|test) [options]\n"
fmt.Fprintf(os.Stderr, usage, exeName())
flag.PrintDefaults()
}
func main() {
setInterruptHandler()
flag.Usage = printUsage
fconf := flag.String("config",
exeName()+".config", "Path to config file")
if len(os.Args) < 2 {
printUsage()
os.Exit(1)
}
switch os.Args[1] {
case "read":
read(fconf)
case "write":
write(fconf)
case "test":
test(fconf)
default:
printUsage()
os.Exit(1)
}
}
func readConfig(name string) (*store.Config, error) {
file, err := os.Open(name)
if err != nil {
return nil, err
}
defer file.Close()
var conf store.Config
if err := json.NewDecoder(file).Decode(&conf); err != nil {
return nil, err
}
return &conf, nil
}
func processCommonFlags(fconf *string) (*logpkg.Logger, *store.Store) {
flag.CommandLine.Parse(os.Args[2:])
conf, err := readConfig(*fconf)
if err != nil {
logpkg.Fatalf("failed to read configuration: %s", err)
}
log := logpkg.New(os.Stderr, "", logpkg.Ldate|logpkg.Ltime)
st := store.New(conf, log)
return log, st
}
func readCb(st *store.Store, val string) {
checkInterrupted(nil)
fmt.Println(val)
}
func read(fconf *string) {
fkey := flag.String("key", "", "Key to read values for")
fkeys := flag.Bool("keys", false, "Read all the keys")
flag.CommandLine.Parse(os.Args[2:])
_, st := processCommonFlags(fconf)
if (*fkeys && !st.FindKeys(readCb)) ||
(!*fkeys && !st.FindValues(*fkey, readCb)) {
os.Exit(1)
}
}
func write(fconf *string) {
log, st := processCommonFlags(fconf)
scan := bufio.NewScanner(os.Stdin)
for scan.Scan() {
checkInterrupted(st)
split := strings.SplitN(scan.Text(), "\t", 2)
if len(split) != 2 {
log.Println("key without value, ignored")
continue
}
if !st.AddValue(split[0], split[1]) {
os.Exit(1)
}
}
st.Flush(true)
if err := scan.Err(); err != nil {
os.Exit(1)
}
}
func test(fconf *string) {
fkeys := flag.Int("keys", testKeys, "Total number of keys")
fvals := flag.Int("values", testKeys, "Total number of values")
log, st := processCommonFlags(fconf)
if !doTest(log, st, *fkeys, *fvals) {
os.Exit(1)
}
}