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
|
package main
import (
xenstoreclient "../xenstoreclient"
"fmt"
"os"
"strings"
)
func die(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
fmt.Fprintln(os.Stderr)
os.Exit(1)
}
func usage() {
die(
`Usage: xenstore read key [ key ... ]
write key value [ key value ... ]
rm key [ key ... ]
exists key [ key ... ]`)
}
func new_xs() xenstoreclient.XenStoreClient {
xs, err := xenstoreclient.NewXenstore(0)
if err != nil {
die("xenstore.Open error: %v", err)
}
return xs
}
func xs_read(script_name string, args []string) {
if len(args) == 0 || args[0] == "-h" {
die("Usage: %s key [ key ... ]", script_name)
}
xs := new_xs()
for _, key := range args[:] {
result, err := xs.Read(key)
if err != nil {
die("%s error: %v", script_name, err)
}
fmt.Println(result)
}
}
func xs_write(script_name string, args []string) {
if len(args) == 0 || args[0] == "-h" || len(args)%2 != 0 {
die("Usage: %s key value [ key value ... ]", script_name)
}
xs := new_xs()
for i := 0; i < len(args); i += 2 {
key := args[i]
value := args[i+1]
err := xs.Write(key, value)
if err != nil {
die("%s error: %v", script_name, err)
}
}
}
func xs_rm(script_name string, args []string) {
if len(args) == 0 || args[0] == "-h" {
die("Usage: %s key [ key ... ]", script_name)
}
xs := new_xs()
for _, key := range args[:] {
err := xs.Rm(key)
if err != nil {
die("%s error: %v", script_name, err)
}
}
}
func xs_exists(script_name string, args []string) {
if len(args) == 0 || args[0] == "-h" {
die("Usage: %s key [ key ... ]", script_name)
}
xs := new_xs()
for _, key := range args[:] {
_, err := xs.Read(key)
if err != nil {
die("%s error: %v", script_name, err)
}
}
}
func main() {
var operation string
var args []string
script_name := os.Args[0]
if strings.Contains(script_name, "-") {
operation = script_name[strings.LastIndex(script_name, "-")+1:]
args = os.Args[1:]
} else {
if len(os.Args) < 2 {
usage()
}
operation = os.Args[1]
script_name = script_name + " " + operation
args = os.Args[2:]
}
switch operation {
case "read":
xs_read(script_name, args)
case "write":
xs_write(script_name, args)
case "rm":
xs_rm(script_name, args)
case "exists":
xs_exists(script_name, args)
default:
usage()
}
}
|