3 | 3 |
package main
|
4 | 4 |
|
5 | 5 |
import (
|
6 | |
"context"
|
7 | |
"flag"
|
8 | |
"fmt"
|
9 | |
"math"
|
10 | |
"net"
|
11 | |
"net/url"
|
12 | |
"os"
|
13 | |
"strconv"
|
14 | |
"strings"
|
15 | |
"github.com/apache/thrift/lib/go/thrift"
|
16 | |
"github.com/go-kit/kit/examples/addsvc/thrift/gen-go/addsvc"
|
|
6 |
"context"
|
|
7 |
"flag"
|
|
8 |
"fmt"
|
|
9 |
"github.com/apache/thrift/lib/go/thrift"
|
|
10 |
"github.com/go-kit/kit/examples/addsvc/thrift/gen-go/addsvc"
|
|
11 |
"math"
|
|
12 |
"net"
|
|
13 |
"net/url"
|
|
14 |
"os"
|
|
15 |
"strconv"
|
|
16 |
"strings"
|
17 | 17 |
)
|
18 | 18 |
|
19 | |
|
20 | 19 |
func Usage() {
|
21 | |
fmt.Fprintln(os.Stderr, "Usage of ", os.Args[0], " [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]:")
|
22 | |
flag.PrintDefaults()
|
23 | |
fmt.Fprintln(os.Stderr, "\nFunctions:")
|
24 | |
fmt.Fprintln(os.Stderr, " SumReply Sum(i64 a, i64 b)")
|
25 | |
fmt.Fprintln(os.Stderr, " ConcatReply Concat(string a, string b)")
|
26 | |
fmt.Fprintln(os.Stderr)
|
27 | |
os.Exit(0)
|
|
20 |
fmt.Fprintln(os.Stderr, "Usage of ", os.Args[0], " [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]:")
|
|
21 |
flag.PrintDefaults()
|
|
22 |
fmt.Fprintln(os.Stderr, "\nFunctions:")
|
|
23 |
fmt.Fprintln(os.Stderr, " SumReply Sum(i64 a, i64 b)")
|
|
24 |
fmt.Fprintln(os.Stderr, " ConcatReply Concat(string a, string b)")
|
|
25 |
fmt.Fprintln(os.Stderr)
|
|
26 |
os.Exit(0)
|
28 | 27 |
}
|
29 | 28 |
|
30 | 29 |
type httpHeaders map[string]string
|
31 | 30 |
|
32 | 31 |
func (h httpHeaders) String() string {
|
33 | |
var m map[string]string = h
|
34 | |
return fmt.Sprintf("%s", m)
|
|
32 |
var m map[string]string = h
|
|
33 |
return fmt.Sprintf("%s", m)
|
35 | 34 |
}
|
36 | 35 |
|
37 | 36 |
func (h httpHeaders) Set(value string) error {
|
38 | |
parts := strings.Split(value, ": ")
|
39 | |
if len(parts) != 2 {
|
40 | |
return fmt.Errorf("header should be of format 'Key: Value'")
|
41 | |
}
|
42 | |
h[parts[0]] = parts[1]
|
43 | |
return nil
|
|
37 |
parts := strings.Split(value, ": ")
|
|
38 |
if len(parts) != 2 {
|
|
39 |
return fmt.Errorf("header should be of format 'Key: Value'")
|
|
40 |
}
|
|
41 |
h[parts[0]] = parts[1]
|
|
42 |
return nil
|
44 | 43 |
}
|
45 | 44 |
|
46 | 45 |
func main() {
|
47 | |
flag.Usage = Usage
|
48 | |
var host string
|
49 | |
var port int
|
50 | |
var protocol string
|
51 | |
var urlString string
|
52 | |
var framed bool
|
53 | |
var useHttp bool
|
54 | |
headers := make(httpHeaders)
|
55 | |
var parsedUrl *url.URL
|
56 | |
var trans thrift.TTransport
|
57 | |
_ = strconv.Atoi
|
58 | |
_ = math.Abs
|
59 | |
flag.Usage = Usage
|
60 | |
flag.StringVar(&host, "h", "localhost", "Specify host and port")
|
61 | |
flag.IntVar(&port, "p", 9090, "Specify port")
|
62 | |
flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)")
|
63 | |
flag.StringVar(&urlString, "u", "", "Specify the url")
|
64 | |
flag.BoolVar(&framed, "framed", false, "Use framed transport")
|
65 | |
flag.BoolVar(&useHttp, "http", false, "Use http")
|
66 | |
flag.Var(headers, "H", "Headers to set on the http(s) request (e.g. -H \"Key: Value\")")
|
67 | |
flag.Parse()
|
68 | |
|
69 | |
if len(urlString) > 0 {
|
70 | |
var err error
|
71 | |
parsedUrl, err = url.Parse(urlString)
|
72 | |
if err != nil {
|
73 | |
fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
|
74 | |
flag.Usage()
|
75 | |
}
|
76 | |
host = parsedUrl.Host
|
77 | |
useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http" || parsedUrl.Scheme == "https"
|
78 | |
} else if useHttp {
|
79 | |
_, err := url.Parse(fmt.Sprint("http://", host, ":", port))
|
80 | |
if err != nil {
|
81 | |
fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
|
82 | |
flag.Usage()
|
83 | |
}
|
84 | |
}
|
85 | |
|
86 | |
cmd := flag.Arg(0)
|
87 | |
var err error
|
88 | |
if useHttp {
|
89 | |
trans, err = thrift.NewTHttpClient(parsedUrl.String())
|
90 | |
if len(headers) > 0 {
|
91 | |
httptrans := trans.(*thrift.THttpClient)
|
92 | |
for key, value := range headers {
|
93 | |
httptrans.SetHeader(key, value)
|
94 | |
}
|
95 | |
}
|
96 | |
} else {
|
97 | |
portStr := fmt.Sprint(port)
|
98 | |
if strings.Contains(host, ":") {
|
99 | |
host, portStr, err = net.SplitHostPort(host)
|
100 | |
if err != nil {
|
101 | |
fmt.Fprintln(os.Stderr, "error with host:", err)
|
102 | |
os.Exit(1)
|
103 | |
}
|
104 | |
}
|
105 | |
trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr))
|
106 | |
if err != nil {
|
107 | |
fmt.Fprintln(os.Stderr, "error resolving address:", err)
|
108 | |
os.Exit(1)
|
109 | |
}
|
110 | |
if framed {
|
111 | |
trans = thrift.NewTFramedTransport(trans)
|
112 | |
}
|
113 | |
}
|
114 | |
if err != nil {
|
115 | |
fmt.Fprintln(os.Stderr, "Error creating transport", err)
|
116 | |
os.Exit(1)
|
117 | |
}
|
118 | |
defer trans.Close()
|
119 | |
var protocolFactory thrift.TProtocolFactory
|
120 | |
switch protocol {
|
121 | |
case "compact":
|
122 | |
protocolFactory = thrift.NewTCompactProtocolFactory()
|
123 | |
break
|
124 | |
case "simplejson":
|
125 | |
protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
|
126 | |
break
|
127 | |
case "json":
|
128 | |
protocolFactory = thrift.NewTJSONProtocolFactory()
|
129 | |
break
|
130 | |
case "binary", "":
|
131 | |
protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
|
132 | |
break
|
133 | |
default:
|
134 | |
fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol)
|
135 | |
Usage()
|
136 | |
os.Exit(1)
|
137 | |
}
|
138 | |
iprot := protocolFactory.GetProtocol(trans)
|
139 | |
oprot := protocolFactory.GetProtocol(trans)
|
140 | |
client := addsvc.NewAddServiceClient(thrift.NewTStandardClient(iprot, oprot))
|
141 | |
if err := trans.Open(); err != nil {
|
142 | |
fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err)
|
143 | |
os.Exit(1)
|
144 | |
}
|
145 | |
|
146 | |
switch cmd {
|
147 | |
case "Sum":
|
148 | |
if flag.NArg() - 1 != 2 {
|
149 | |
fmt.Fprintln(os.Stderr, "Sum requires 2 args")
|
150 | |
flag.Usage()
|
151 | |
}
|
152 | |
argvalue0, err6 := (strconv.ParseInt(flag.Arg(1), 10, 64))
|
153 | |
if err6 != nil {
|
154 | |
Usage()
|
155 | |
return
|
156 | |
}
|
157 | |
value0 := argvalue0
|
158 | |
argvalue1, err7 := (strconv.ParseInt(flag.Arg(2), 10, 64))
|
159 | |
if err7 != nil {
|
160 | |
Usage()
|
161 | |
return
|
162 | |
}
|
163 | |
value1 := argvalue1
|
164 | |
fmt.Print(client.Sum(context.Background(), value0, value1))
|
165 | |
fmt.Print("\n")
|
166 | |
break
|
167 | |
case "Concat":
|
168 | |
if flag.NArg() - 1 != 2 {
|
169 | |
fmt.Fprintln(os.Stderr, "Concat requires 2 args")
|
170 | |
flag.Usage()
|
171 | |
}
|
172 | |
argvalue0 := flag.Arg(1)
|
173 | |
value0 := argvalue0
|
174 | |
argvalue1 := flag.Arg(2)
|
175 | |
value1 := argvalue1
|
176 | |
fmt.Print(client.Concat(context.Background(), value0, value1))
|
177 | |
fmt.Print("\n")
|
178 | |
break
|
179 | |
case "":
|
180 | |
Usage()
|
181 | |
break
|
182 | |
default:
|
183 | |
fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
|
184 | |
}
|
|
46 |
flag.Usage = Usage
|
|
47 |
var host string
|
|
48 |
var port int
|
|
49 |
var protocol string
|
|
50 |
var urlString string
|
|
51 |
var framed bool
|
|
52 |
var useHttp bool
|
|
53 |
headers := make(httpHeaders)
|
|
54 |
var parsedUrl *url.URL
|
|
55 |
var trans thrift.TTransport
|
|
56 |
_ = strconv.Atoi
|
|
57 |
_ = math.Abs
|
|
58 |
flag.Usage = Usage
|
|
59 |
flag.StringVar(&host, "h", "localhost", "Specify host and port")
|
|
60 |
flag.IntVar(&port, "p", 9090, "Specify port")
|
|
61 |
flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)")
|
|
62 |
flag.StringVar(&urlString, "u", "", "Specify the url")
|
|
63 |
flag.BoolVar(&framed, "framed", false, "Use framed transport")
|
|
64 |
flag.BoolVar(&useHttp, "http", false, "Use http")
|
|
65 |
flag.Var(headers, "H", "Headers to set on the http(s) request (e.g. -H \"Key: Value\")")
|
|
66 |
flag.Parse()
|
|
67 |
|
|
68 |
if len(urlString) > 0 {
|
|
69 |
var err error
|
|
70 |
parsedUrl, err = url.Parse(urlString)
|
|
71 |
if err != nil {
|
|
72 |
fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
|
|
73 |
flag.Usage()
|
|
74 |
}
|
|
75 |
host = parsedUrl.Host
|
|
76 |
useHttp = parsedUrl.Scheme == "" || parsedUrl.Scheme == "http" || parsedUrl.Scheme == "https"
|
|
77 |
} else if useHttp {
|
|
78 |
_, err := url.Parse(fmt.Sprint("http://", host, ":", port))
|
|
79 |
if err != nil {
|
|
80 |
fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
|
|
81 |
flag.Usage()
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
cmd := flag.Arg(0)
|
|
86 |
var err error
|
|
87 |
if useHttp {
|
|
88 |
trans, err = thrift.NewTHttpClient(parsedUrl.String())
|
|
89 |
if len(headers) > 0 {
|
|
90 |
httptrans := trans.(*thrift.THttpClient)
|
|
91 |
for key, value := range headers {
|
|
92 |
httptrans.SetHeader(key, value)
|
|
93 |
}
|
|
94 |
}
|
|
95 |
} else {
|
|
96 |
portStr := fmt.Sprint(port)
|
|
97 |
if strings.Contains(host, ":") {
|
|
98 |
host, portStr, err = net.SplitHostPort(host)
|
|
99 |
if err != nil {
|
|
100 |
fmt.Fprintln(os.Stderr, "error with host:", err)
|
|
101 |
os.Exit(1)
|
|
102 |
}
|
|
103 |
}
|
|
104 |
trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr))
|
|
105 |
if err != nil {
|
|
106 |
fmt.Fprintln(os.Stderr, "error resolving address:", err)
|
|
107 |
os.Exit(1)
|
|
108 |
}
|
|
109 |
if framed {
|
|
110 |
trans = thrift.NewTFramedTransport(trans)
|
|
111 |
}
|
|
112 |
}
|
|
113 |
if err != nil {
|
|
114 |
fmt.Fprintln(os.Stderr, "Error creating transport", err)
|
|
115 |
os.Exit(1)
|
|
116 |
}
|
|
117 |
defer trans.Close()
|
|
118 |
var protocolFactory thrift.TProtocolFactory
|
|
119 |
switch protocol {
|
|
120 |
case "compact":
|
|
121 |
protocolFactory = thrift.NewTCompactProtocolFactory()
|
|
122 |
break
|
|
123 |
case "simplejson":
|
|
124 |
protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
|
|
125 |
break
|
|
126 |
case "json":
|
|
127 |
protocolFactory = thrift.NewTJSONProtocolFactory()
|
|
128 |
break
|
|
129 |
case "binary", "":
|
|
130 |
protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
|
|
131 |
break
|
|
132 |
default:
|
|
133 |
fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol)
|
|
134 |
Usage()
|
|
135 |
os.Exit(1)
|
|
136 |
}
|
|
137 |
iprot := protocolFactory.GetProtocol(trans)
|
|
138 |
oprot := protocolFactory.GetProtocol(trans)
|
|
139 |
client := addsvc.NewAddServiceClient(thrift.NewTStandardClient(iprot, oprot))
|
|
140 |
if err := trans.Open(); err != nil {
|
|
141 |
fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err)
|
|
142 |
os.Exit(1)
|
|
143 |
}
|
|
144 |
|
|
145 |
switch cmd {
|
|
146 |
case "Sum":
|
|
147 |
if flag.NArg()-1 != 2 {
|
|
148 |
fmt.Fprintln(os.Stderr, "Sum requires 2 args")
|
|
149 |
flag.Usage()
|
|
150 |
}
|
|
151 |
argvalue0, err6 := (strconv.ParseInt(flag.Arg(1), 10, 64))
|
|
152 |
if err6 != nil {
|
|
153 |
Usage()
|
|
154 |
return
|
|
155 |
}
|
|
156 |
value0 := argvalue0
|
|
157 |
argvalue1, err7 := (strconv.ParseInt(flag.Arg(2), 10, 64))
|
|
158 |
if err7 != nil {
|
|
159 |
Usage()
|
|
160 |
return
|
|
161 |
}
|
|
162 |
value1 := argvalue1
|
|
163 |
fmt.Print(client.Sum(context.Background(), value0, value1))
|
|
164 |
fmt.Print("\n")
|
|
165 |
break
|
|
166 |
case "Concat":
|
|
167 |
if flag.NArg()-1 != 2 {
|
|
168 |
fmt.Fprintln(os.Stderr, "Concat requires 2 args")
|
|
169 |
flag.Usage()
|
|
170 |
}
|
|
171 |
argvalue0 := flag.Arg(1)
|
|
172 |
value0 := argvalue0
|
|
173 |
argvalue1 := flag.Arg(2)
|
|
174 |
value1 := argvalue1
|
|
175 |
fmt.Print(client.Concat(context.Background(), value0, value1))
|
|
176 |
fmt.Print("\n")
|
|
177 |
break
|
|
178 |
case "":
|
|
179 |
Usage()
|
|
180 |
break
|
|
181 |
default:
|
|
182 |
fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
|
|
183 |
}
|
185 | 184 |
}
|