-
-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathclient-single-server-single-host-with-variables.golden
More file actions
135 lines (123 loc) · 3.64 KB
/
client-single-server-single-host-with-variables.golden
File metadata and controls
135 lines (123 loc) · 3.64 KB
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
func main() {
var (
hostF = flag.String("host", "dev", "Server host (valid values: dev)")
addrF = flag.String("url", "", "URL to service host")
int_F = flag.String("int", "1", "")
uint_F = flag.String("uint", "1", "")
float32_F = flag.String("float32", "1.1", "")
int32_F = flag.String("int32", "1", "")
int64_F = flag.String("int64", "1", "")
uint32_F = flag.String("uint32", "1", "")
uint64_F = flag.String("uint64", "1", "")
float64_F = flag.String("float64", "1", "")
bool_F = flag.String("bool", "true", "")
verboseF = flag.Bool("verbose", false, "Print request and response details")
vF = flag.Bool("v", false, "Print request and response details")
timeoutF = flag.Int("timeout", 30, "Maximum number of seconds to wait for response")
)
flag.Usage = usage
flag.Parse()
var (
addr string
timeout int
debug bool
)
{
addr = *addrF
if addr == "" {
switch *hostF {
case "dev":
addr = "http://example-{int}-{uint}-{float32}:8090"
addr = strings.ReplaceAll(addr, "{int}", *int_F)
addr = strings.ReplaceAll(addr, "{uint}", *uint_F)
addr = strings.ReplaceAll(addr, "{float32}", *float32_F)
addr = strings.ReplaceAll(addr, "{int32}", *int32_F)
addr = strings.ReplaceAll(addr, "{int64}", *int64_F)
addr = strings.ReplaceAll(addr, "{uint32}", *uint32_F)
addr = strings.ReplaceAll(addr, "{uint64}", *uint64_F)
addr = strings.ReplaceAll(addr, "{float64}", *float64_F)
addr = strings.ReplaceAll(addr, "{bool}", *bool_F)
default:
fmt.Fprintf(os.Stderr, "invalid host argument: %q (valid hosts: dev)\n", *hostF)
os.Exit(1)
}
}
timeout = *timeoutF
debug = *verboseF || *vF
}
var (
scheme string
host string
)
{
u, err := url.Parse(addr)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid URL %#v: %s\n", addr, err)
os.Exit(1)
}
scheme = u.Scheme
host = u.Host
}
var (
endpoint goa.Endpoint
payload any
err error
)
{
switch scheme {
case "http", "https":
endpoint, payload, err = doHTTP(scheme, host, timeout, debug)
default:
fmt.Fprintf(os.Stderr, "invalid scheme: %q (valid schemes: http|https)\n", scheme)
os.Exit(1)
}
}
if err != nil {
if errors.Is(err, flag.ErrHelp) {
os.Exit(0)
}
fmt.Fprintln(os.Stderr, err.Error())
fmt.Fprintln(os.Stderr, "run '"+os.Args[0]+" --help' for detailed usage.")
os.Exit(1)
}
data, err := endpoint(context.Background(), payload)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
if data != nil {
m, _ := json.MarshalIndent(data, "", " ")
fmt.Println(string(m))
}
}
func usage() {
fmt.Fprintf(os.Stderr, `%s is a command line client for the SingleServerSingleHostWithVariables API.
Usage:
%s [-host HOST][-url URL][-timeout SECONDS][-verbose|-v][-int INT][-uint UINT][-float32 FLOAT32][-int32 INT32][-int64 INT64][-uint32 UINT32][-uint64 UINT64][-float64 FLOAT64][-bool BOOL] SERVICE ENDPOINT [flags]
-host HOST: server host (dev). valid values: dev
-url URL: specify service URL overriding host URL (http://localhost:8080)
-timeout: maximum number of seconds to wait for response (30)
-verbose|-v: print request and response details (false)
-int: (1)
-uint: (1)
-float32: (1.1)
-int32: (1)
-int64: (1)
-uint32: (1)
-uint64: (1)
-float64: (1)
-bool: (true)
Commands:
%s
Additional help:
%s SERVICE [ENDPOINT] --help
Example:
%s
`, os.Args[0], os.Args[0], indent(httpUsageCommands()), os.Args[0], indent(httpUsageExamples()))
}
func indent(s string) string {
if s == "" {
return ""
}
return " " + strings.ReplaceAll(s, "\n", "\n ")
}