MCPcopy Create free account
hub / github.com/goadesign/goa / exampleCLI

Function exampleCLI

grpc/codegen/example_cli.go:27–80  ·  view source on GitHub ↗

exampleCLI returns an example client tool HTTP implementation for the given server expression.

(genpkg string, services *ServicesData, svr *expr.ServerExpr)

Source from the content-addressed store, hash-verified

25// exampleCLI returns an example client tool HTTP implementation for the given
26// server expression.
27func exampleCLI(genpkg string, services *ServicesData, svr *expr.ServerExpr) *codegen.File {
28 var (
29 mainPath string
30 rootPath string
31
32 svrdata = example.Servers.Get(svr, services.Root)
33 )
34 mainPath = filepath.Join("cmd", svrdata.Dir+"-cli", "grpc.go")
35 if _, err := os.Stat(mainPath); !os.IsNotExist(err) {
36 return nil // file already exists, skip it.
37 }
38 idx := strings.LastIndex(genpkg, string("/"))
39 rootPath = "."
40 if idx > 0 {
41 rootPath = genpkg[:idx]
42 }
43
44 specs := []*codegen.ImportSpec{
45 {Path: "context"},
46 {Path: "encoding/json"},
47 {Path: "flag"},
48 {Path: "fmt"},
49 {Path: "google.golang.org/grpc"},
50 {Path: "google.golang.org/grpc/credentials/insecure"},
51 {Path: "os"},
52 {Path: "time"},
53 codegen.GoaImport(""),
54 codegen.GoaNamedImport("grpc", "goagrpc"),
55 {Path: rootPath + "/interceptors"},
56 {Path: path.Join(genpkg, "grpc", "cli", svrdata.Dir), Name: "cli"},
57 }
58
59 var svcData []*ServiceData
60 for _, svc := range svr.Services {
61 if data := services.Get(svc); data != nil {
62 svcData = append(svcData, data)
63 }
64 }
65
66 sections := []*codegen.SectionTemplate{
67 codegen.Header("", "main", specs),
68 {
69 Name: "do-grpc-cli",
70 Source: grpcTemplates.Read(grpcDoGRPCCLIT),
71 Data: map[string]any{
72 "DefaultTransport": svrdata.DefaultTransport(),
73 "Services": svcData,
74 "InterceptorsPkg": "interceptors",
75 },
76 },
77 }
78
79 return &codegen.File{Path: mainPath, SectionTemplates: sections, SkipExist: true}
80}

Callers 1

ExampleCLIFilesFunction · 0.70

Calls 6

GoaImportFunction · 0.92
GoaNamedImportFunction · 0.92
HeaderFunction · 0.92
DefaultTransportMethod · 0.80
GetMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected