代码拉取完成,页面将自动刷新
package codegen
import (
"os"
"path"
"path/filepath"
"strings"
"goa.design/goa/codegen"
"goa.design/goa/codegen/example"
"goa.design/goa/expr"
)
// ExampleCLIFiles returns an example gRPC client tool implementation.
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 {
var (
mainPath string
svrdata = example.Servers.Get(svr)
)
{
mainPath = filepath.Join("cmd", svrdata.Dir+"-cli", "grpc.go")
if _, err := os.Stat(mainPath); !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")
}
var (
specs []*codegen.ImportSpec
)
{
specs = []*codegen.ImportSpec{
{Path: "context"},
{Path: "encoding/json"},
{Path: "flag"},
{Path: "fmt"},
{Path: "google.golang.org/grpc"},
{Path: "os"},
{Path: "time"},
{Path: "goa.design/goa"},
{Path: "goa.design/goa/grpc", Name: "goagrpc"},
{Path: rootPath, Name: apiPkg},
{Path: path.Join(genpkg, "grpc", "cli", svrdata.Dir), Name: "cli"},
}
}
var (
sections []*codegen.SectionTemplate
)
{
sections = []*codegen.SectionTemplate{
codegen.Header("", "main", specs),
&codegen.SectionTemplate{Name: "do-grpc-cli", Source: grpcCLIDoT, Data: svrdata},
}
}
return &codegen.File{Path: mainPath, SectionTemplates: sections, SkipExist: true}
}
const (
grpcCLIDoT = `func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, interface{}, error) {
conn, err := grpc.Dial(host, grpc.WithInsecure())
if err != nil {
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
}
return cli.ParseEndpoint(conn)
}
{{ if eq .DefaultTransport.Type "grpc" }}
func grpcUsageCommands() string {
return cli.UsageCommands()
}
func grpcUsageExamples() string {
return cli.UsageExamples()
}
{{- end }}
`
)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。