Ai
11 Star 11 Fork 0

Gitee 极速下载/goa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/goadesign/goa
克隆/下载
example_cli.go 2.46 KB
一键复制 编辑 原始数据 按行查看 历史
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 }}
`
)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/goa.git
git@gitee.com:mirrors/goa.git
mirrors
goa
goa
v2.2.5

搜索帮助