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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/TylerBrock/colorjson"
"github.com/fatih/color"
"github.com/varlink/go/varlink"
"os"
"strings"
)
var bold = color.New(color.Bold)
var errorBoldRed = bold.Sprint(color.New(color.FgRed).Sprint("Error:"))
var bridge string
func ErrPrintf(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, "%s ", errorBoldRed)
fmt.Fprintf(os.Stderr, format, a...)
}
func print_usage(set *flag.FlagSet, arg_help string) {
if set == nil {
fmt.Fprintf(os.Stderr, "Usage: %s [GLOBAL OPTIONS] COMMAND ...\n", os.Args[0])
} else {
fmt.Fprintf(os.Stderr, "Usage: %s [GLOBAL OPTIONS] %s [OPTIONS] %s\n", os.Args[0], set.Name(), arg_help)
}
fmt.Fprintln(os.Stderr, "\nGlobal Options:")
flag.PrintDefaults()
if set == nil {
fmt.Fprintln(os.Stderr, "\nCommands:")
fmt.Fprintln(os.Stderr, " info\tPrint information about a service")
fmt.Fprintln(os.Stderr, " help\tPrint interface description or service information")
fmt.Fprintln(os.Stderr, " call\tCall a method")
} else {
fmt.Fprintln(os.Stderr, "\nOptions:")
set.PrintDefaults()
}
os.Exit(1)
}
func varlink_call(args []string) {
var err error
var oneway bool
callFlags := flag.NewFlagSet("help", flag.ExitOnError)
callFlags.BoolVar(&oneway, "-oneway", false, "Use bridge for connection")
var help bool
callFlags.BoolVar(&help, "help", false, "Prints help information")
var usage = func() { print_usage(callFlags, "<[ADDRESS/]INTERFACE.METHOD> [ARGUMENTS]") }
callFlags.Usage = usage
_ = callFlags.Parse(args)
if help {
usage()
}
var con *varlink.Connection
var address string
var methodName string
if len(bridge) != 0 {
con, err = varlink.NewBridge(bridge)
if err != nil {
ErrPrintf("Cannot connect with bridge '%s': %v\n", bridge, err)
os.Exit(2)
}
address = "bridge:" + bridge
methodName = callFlags.Arg(0)
} else {
uri := callFlags.Arg(0)
if uri == "" {
usage()
}
li := strings.LastIndex(uri, "/")
if li == -1 {
ErrPrintf("Invalid address '%s'\n", uri)
os.Exit(2)
}
address = uri[:li]
methodName = uri[li+1:]
con, err = varlink.NewConnection(address)
if err != nil {
ErrPrintf("Cannot connect to '%s': %v\n", address, err)
os.Exit(2)
}
}
var parameters string
var params json.RawMessage
parameters = callFlags.Arg(1)
if parameters == "" {
params = nil
} else {
json.Unmarshal([]byte(parameters), ¶ms)
}
var flags uint64
flags = 0
if oneway {
flags |= varlink.Oneway
}
recv, err := con.Send(methodName, params, flags)
var retval map[string]interface{}
// FIXME: Use cont
_, err = recv(&retval)
f := colorjson.NewFormatter()
f.Indent = 2
f.KeyColor = color.New(color.FgCyan)
f.StringColor = color.New(color.FgMagenta)
f.NumberColor = color.New(color.FgMagenta)
f.BoolColor = color.New(color.FgMagenta)
f.NullColor = color.New(color.FgMagenta)
if err != nil {
ErrPrintf("Error calling '%s': %v\n", methodName, err)
switch e := err.(type) {
case *varlink.Error:
println(e.Name)
errorRawParameters := e.Parameters.(*json.RawMessage)
if errorRawParameters == nil {
break
}
var param map[string]interface{}
_ = json.Unmarshal(*errorRawParameters, ¶m)
c, _ := f.Marshal(param)
ErrPrintf("%v\n", string(c))
}
os.Exit(2)
}
c, _ := f.Marshal(retval)
fmt.Println(string(c))
}
func varlink_help(args []string) {
var err error
helpFlags := flag.NewFlagSet("help", flag.ExitOnError)
var help bool
helpFlags.BoolVar(&help, "help", false, "Prints help information")
var usage = func() { print_usage(helpFlags, "<[ADDRESS/]INTERFACE>") }
helpFlags.Usage = usage
_ = helpFlags.Parse(args)
if help {
usage()
}
var con *varlink.Connection
var address string
var interfaceName string
if len(bridge) != 0 {
con, err = varlink.NewBridge(bridge)
if err != nil {
ErrPrintf("Cannot connect with bridge '%s': %v\n", bridge, err)
os.Exit(2)
}
address = "bridge:" + bridge
interfaceName = helpFlags.Arg(0)
} else {
uri := helpFlags.Arg(0)
if uri == "" && bridge == "" {
ErrPrintf("No ADDRESS or activation or bridge\n\n")
usage()
}
li := strings.LastIndex(uri, "/")
if li == -1 {
ErrPrintf("Invalid address '%s'\n", uri)
os.Exit(2)
}
address = uri[:li]
con, err = varlink.NewConnection(address)
if err != nil {
ErrPrintf("Cannot connect to '%s': %v\n", address, err)
os.Exit(2)
}
interfaceName = uri[li+1:]
}
description, err := con.GetInterfaceDescription(interfaceName)
if err != nil {
ErrPrintf("Cannot get interface description for '%s': %v\n", interfaceName, err)
os.Exit(2)
}
fmt.Println(description)
}
func varlink_info(args []string) {
var err error
infoFlags := flag.NewFlagSet("info", flag.ExitOnError)
var help bool
infoFlags.BoolVar(&help, "help", false, "Prints help information")
var usage = func() { print_usage(infoFlags, "[ADDRESS]") }
infoFlags.Usage = usage
_ = infoFlags.Parse(args)
if help {
usage()
}
var con *varlink.Connection
var address string
if len(bridge) != 0 {
con, err = varlink.NewBridge(bridge)
if err != nil {
ErrPrintf("Cannot connect with bridge '%s': %v\n", bridge, err)
os.Exit(2)
}
address = "bridge:" + bridge
} else {
address = infoFlags.Arg(0)
if address == "" && bridge == "" {
ErrPrintf("No ADDRESS or activation or bridge\n\n")
usage()
}
con, err = varlink.NewConnection(address)
if err != nil {
ErrPrintf("Cannot connect to '%s': %v\n", address, err)
os.Exit(2)
}
}
var vendor, product, version, url string
var interfaces []string
err = con.GetInfo(&vendor, &product, &version, &url, &interfaces)
if err != nil {
ErrPrintf("Cannot get info for '%s': %v\n", address, err)
os.Exit(2)
}
fmt.Printf("%s %s\n", bold.Sprint("Vendor:"), vendor)
fmt.Printf("%s %s\n", bold.Sprint("Product:"), product)
fmt.Printf("%s %s\n", bold.Sprint("Version:"), version)
fmt.Printf("%s %s\n", bold.Sprint("URL:"), url)
fmt.Printf("%s\n %s\n\n", bold.Sprint("Interfaces:"), strings.Join(interfaces[:], "\n "))
}
func main() {
var debug bool
var colorMode string
flag.CommandLine.Usage = func() { print_usage(nil, "") }
flag.BoolVar(&debug, "debug", false, "Enable debug output")
flag.StringVar(&bridge, "bridge", "", "Use bridge for connection")
flag.StringVar(&colorMode, "color", "auto", "colorize output [default: auto] [possible values: on, off, auto]")
flag.Parse()
if colorMode != "on" && (os.Getenv("TERM") == "" || colorMode == "off") {
color.NoColor = true // disables colorized output
}
switch flag.Arg(0) {
case "info":
varlink_info(flag.Args()[1:])
case "help":
varlink_help(flag.Args()[1:])
case "call":
varlink_call(flag.Args()[1:])
default:
print_usage(nil, "")
}
}
|