-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
146 lines (125 loc) · 4.52 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
143
144
145
146
// Copyright 2014-2015 Jakub Matys
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"github.com/docopt/docopt-go"
"io/ioutil"
"log"
"strconv"
)
const usage = `msgpack-cli
Usage:
msgpack-cli encode [<input-file>] [--out=<output-file>] [--disable-int64-conv]
msgpack-cli decode [<input-file>] [--out=<output-file>] [--pp]
msgpack-cli rpc <host> <port> <method> [<params>|--file=<input-file>] [--pp]
[--timeout=<timeout>][--disable-int64-conv]
msgpack-cli -h | --help
msgpack-cli --version
Commands:
encode Encode data from input file (default STDIN) to STDOUT
decode Decode data from input file (default STDIN) to STDOUT
rpc Call RPC method and write result to STDOUT
Options:
-h --help Show this help message and exit
--version Show version
--out=<output-file> Write output data to file instead of STDOUT
--file=<input-file> File where parameters or RPC method are read from
--pp Pretty-print - indent output JSON data
--timeout=<timeout> Timeout of RPC call [default: 30]
--disable-int64-conv Disable the default behaviour such that JSON numbers
are converted to float64 or int64 numbers by their
meaning, all result numbers will have float64 type
Arguments:
<input-file> File where data are read from
<host> Server hostname
<port> Server port
<method> Name of RPC method
<params> Parameters of RPC method in JSON format`
type Options struct {
convertToInt64 bool
indent bool
timeout uint32
}
func main() {
arguments, err := docopt.Parse(usage, nil, true, "msgpack-cli "+__VERSION__, false)
if err != nil {
log.Fatal(fmt.Errorf("Arguments parsing: %s", err))
}
switch {
case arguments["encode"], arguments["decode"]:
var inFilename string
if arguments["<input-file>"] != nil {
inFilename = arguments["<input-file>"].(string)
} else {
inFilename = "-"
}
outFilename, _ := arguments["--out"].(string)
conversionFunc := ConvertJSON2Msgpack
if arguments["decode"].(bool) {
conversionFunc = ConvertMsgpack2JSON
}
options := Options{
convertToInt64: !arguments["--disable-int64-conv"].(bool),
indent: arguments["--pp"].(bool),
}
err = ConvertFormats(inFilename, outFilename, conversionFunc, options)
case arguments["rpc"]:
host := arguments["<host>"].(string)
port := arguments["<port>"].(string)
method := arguments["<method>"].(string)
var params string
params, err = getRPCParams(arguments)
if err != nil {
break
}
var timeout uint32
timeout, err = getTimeout(arguments)
if err != nil {
break
}
options := Options{
convertToInt64: !arguments["--disable-int64-conv"].(bool),
indent: arguments["--pp"].(bool),
timeout: timeout,
}
err = CallRPC(host, port, method, params, options)
default:
panic("unreachable")
}
if err != nil {
log.Fatal(err)
}
}
func getRPCParams(arguments map[string]interface{}) (params string, err error) {
params, _ = arguments["<params>"].(string)
filename, _ := arguments["--file"].(string)
if filename != "" {
buff, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
params = string(buff)
}
return params, nil
}
func getTimeout(arguments map[string]interface{}) (timeout uint32, err error) {
timeout = uint32(30)
if str := arguments["--timeout"].(string); str != "" {
var tmp uint64
tmp, err = strconv.ParseUint(str, 10, 32)
timeout = uint32(tmp)
}
return timeout, err
}