ExampleCLI returns an example client tool HTTP implementation for the given server expression.
(genpkg string, svr *expr.ServerExpr, services *ServicesData)
| 25 | // ExampleCLI returns an example client tool HTTP implementation for the given |
| 26 | // server expression. |
| 27 | func ExampleCLI(genpkg string, svr *expr.ServerExpr, services *ServicesData) *codegen.File { |
| 28 | svrdata := example.Servers.Get(svr, services.Root) |
| 29 | path := filepath.Join("cmd", svrdata.Dir+"-cli", "http.go") |
| 30 | if _, err := os.Stat(path); !os.IsNotExist(err) { |
| 31 | return nil // file already exists, skip it. |
| 32 | } |
| 33 | idx := strings.LastIndex(genpkg, string("/")) |
| 34 | rootPath := "." |
| 35 | if idx > 0 { |
| 36 | rootPath = genpkg[:idx] |
| 37 | } |
| 38 | specs := []*codegen.ImportSpec{ |
| 39 | {Path: "context"}, |
| 40 | {Path: "encoding/json"}, |
| 41 | {Path: "flag"}, |
| 42 | {Path: "fmt"}, |
| 43 | {Path: "net/http"}, |
| 44 | {Path: "net/url"}, |
| 45 | {Path: "os"}, |
| 46 | {Path: "strings"}, |
| 47 | {Path: "time"}, |
| 48 | {Path: "github.com/gorilla/websocket"}, |
| 49 | codegen.GoaImport(""), |
| 50 | codegen.GoaNamedImport("http", "goahttp"), |
| 51 | {Path: genpkg + "/http/cli/" + svrdata.Dir, Name: "cli"}, |
| 52 | } |
| 53 | importScope := codegen.NewNameScope() |
| 54 | for _, svc := range services.Root.Services { |
| 55 | data := services.ServicesData.Get(svc.Name) |
| 56 | specs = append(specs, &codegen.ImportSpec{Path: genpkg + "/" + data.PkgName}) |
| 57 | importScope.Unique(data.PkgName) |
| 58 | } |
| 59 | interceptorsPkg := importScope.Unique("interceptors", "ex") |
| 60 | specs = append(specs, &codegen.ImportSpec{Path: rootPath + "/interceptors", Name: interceptorsPkg}) |
| 61 | apiPkg := importScope.Unique(strings.ToLower(codegen.Goify(services.Root.API.Name, false)), "api") |
| 62 | specs = append(specs, &codegen.ImportSpec{Path: rootPath, Name: apiPkg}) |
| 63 | |
| 64 | var svcData []*ServiceData |
| 65 | for _, svc := range svr.Services { |
| 66 | if data := services.Get(svc); data != nil { |
| 67 | svcData = append(svcData, data) |
| 68 | } |
| 69 | } |
| 70 | sections := []*codegen.SectionTemplate{ |
| 71 | codegen.Header("", "main", specs), |
| 72 | { |
| 73 | Name: "cli-http-start", |
| 74 | Source: httpTemplates.Read(cliStartT), |
| 75 | Data: map[string]any{ |
| 76 | "Services": svcData, |
| 77 | "InterceptorsPkg": interceptorsPkg, |
| 78 | }, |
| 79 | }, |
| 80 | { |
| 81 | Name: "cli-http-streaming", |
| 82 | Source: httpTemplates.Read(cliStreamingT), |
| 83 | Data: map[string]any{ |
| 84 | "Services": svcData, |
no test coverage detected