-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathexample_cli.go
165 lines (154 loc) Β· 3.81 KB
/
example_cli.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package codegen
import (
"os"
"path/filepath"
"strings"
"goa.design/goa/codegen"
"goa.design/goa/codegen/example"
"goa.design/goa/expr"
)
// ExampleCLIFiles returns an example client tool HTTP implementation for each
// server expression.
func ExampleCLIFiles(genpkg string, root *expr.RootExpr) []*codegen.File {
var files []*codegen.File
for _, svr := range root.API.Servers {
if f := exampleCLI(genpkg, root, svr); f != nil {
files = append(files, f)
}
}
return files
}
// exampleCLI returns an example client tool HTTP implementation for the given
// server expression.
func exampleCLI(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr) *codegen.File {
svrdata := example.Servers.Get(svr)
path := filepath.Join("cmd", svrdata.Dir+"-cli", "http.go")
if _, err := os.Stat(path); !os.IsNotExist(err) {
return nil // file already exists, skip it.
}
var (
rootPath string
apiPkg string
scope = codegen.NewNameScope()
)
{
// genpkg is created by path.Join so the separator is / regardless of operating system
idx := strings.LastIndex(genpkg, string("/"))
rootPath = "."
if idx > 0 {
rootPath = genpkg[:idx]
}
apiPkg = scope.Unique(strings.ToLower(codegen.Goify(root.API.Name, false)), "api")
}
specs := []*codegen.ImportSpec{
{Path: "context"},
{Path: "encoding/json"},
{Path: "flag"},
{Path: "fmt"},
{Path: "net/http"},
{Path: "net/url"},
{Path: "os"},
{Path: "strings"},
{Path: "time"},
{Path: "github.com/gorilla/websocket"},
{Path: "goa.design/goa"},
{Path: "goa.design/goa/http", Name: "goahttp"},
{Path: genpkg + "/http/cli/" + svrdata.Dir, Name: "cli"},
{Path: rootPath, Name: apiPkg},
}
var svcData []*ServiceData
for _, svc := range svr.Services {
if data := HTTPServices.Get(svc); data != nil {
svcData = append(svcData, data)
}
}
sections := []*codegen.SectionTemplate{
codegen.Header("", "main", specs),
&codegen.SectionTemplate{Name: "cli-http-start", Source: httpCLIStartT},
&codegen.SectionTemplate{
Name: "cli-http-streaming",
Source: httpCLIStreamingT,
Data: map[string]interface{}{
"Services": svcData,
},
FuncMap: map[string]interface{}{
"needStream": needStream,
},
},
&codegen.SectionTemplate{
Name: "cli-http-end",
Source: httpCLIEndT,
Data: map[string]interface{}{
"Services": svcData,
"APIPkg": apiPkg,
},
FuncMap: map[string]interface{}{
"needStream": needStream,
"hasWebSocket": hasWebSocket,
},
},
&codegen.SectionTemplate{Name: "cli-http-usage", Source: httpCLIUsageT},
}
return &codegen.File{
Path: path,
SectionTemplates: sections,
SkipExist: true,
}
}
const (
httpCLIStartT = `func doHTTP(scheme, host string, timeout int, debug bool) (goa.Endpoint, interface{}, error) {
var (
doer goahttp.Doer
)
{
doer = &http.Client{Timeout: time.Duration(timeout) * time.Second}
if debug {
doer = goahttp.NewDebugDoer(doer)
}
}
`
// input: map[string]interface{}{"Services": []*ServiceData}
httpCLIStreamingT = `{{- if needStream .Services }}
var (
dialer *websocket.Dialer
)
{
dialer = websocket.DefaultDialer
}
{{ end }}
`
// input: map[string]interface{}{"Services": []*ServiceData}
httpCLIEndT = `return cli.ParseEndpoint(
scheme,
host,
doer,
goahttp.RequestEncoder,
goahttp.ResponseDecoder,
debug,
{{- if needStream .Services }}
dialer,
{{- range $svc := .Services }}
{{- if hasWebSocket $svc }}
nil,
{{- end }}
{{- end }}
{{- end }}
{{- range .Services }}
{{- range .Endpoints }}
{{- if .MultipartRequestDecoder }}
{{ $.APIPkg }}.{{ .MultipartRequestEncoder.FuncName }},
{{- end }}
{{- end }}
{{- end }}
)
}
`
httpCLIUsageT = `
func httpUsageCommands() string {
return cli.UsageCommands()
}
func httpUsageExamples() string {
return cli.UsageExamples()
}
`
)