ClientCLIFiles returns the client HTTP CLI support file.
(genpkg string, data *ServicesData)
| 38 | |
| 39 | // ClientCLIFiles returns the client HTTP CLI support file. |
| 40 | func ClientCLIFiles(genpkg string, data *ServicesData) []*codegen.File { |
| 41 | if len(data.Expressions.Services) == 0 { |
| 42 | return nil |
| 43 | } |
| 44 | var ( |
| 45 | cmds []*commandData |
| 46 | svcs []*expr.HTTPServiceExpr |
| 47 | ) |
| 48 | for _, svc := range data.Expressions.Services { |
| 49 | sd := data.Get(svc.Name()) |
| 50 | if len(sd.Endpoints) > 0 { |
| 51 | command := &commandData{ |
| 52 | CommandData: cli.BuildCommandData(sd.Service), |
| 53 | NeedDialer: HasWebSocket(sd), |
| 54 | } |
| 55 | |
| 56 | for _, e := range sd.Endpoints { |
| 57 | sub := buildSubcommandData(sd, e) |
| 58 | command.Subcommands = append(command.Subcommands, sub) |
| 59 | command.CommandData.Subcommands = append(command.CommandData.Subcommands, sub.SubcommandData) |
| 60 | } |
| 61 | |
| 62 | command.Example = command.Subcommands[0].Example |
| 63 | |
| 64 | cmds = append(cmds, command) |
| 65 | svcs = append(svcs, svc) |
| 66 | } |
| 67 | } |
| 68 | files := make([]*codegen.File, 0, len(data.Root.API.Servers)*2) // preallocate for CLI files |
| 69 | for _, svr := range data.Root.API.Servers { |
| 70 | var svrData []*commandData |
| 71 | for _, name := range svr.Services { |
| 72 | for i, svc := range svcs { |
| 73 | if svc.Name() == name { |
| 74 | svrData = append(svrData, cmds[i]) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | files = append(files, endpointParser(genpkg, data.Root, svr, svrData, data)) |
| 79 | } |
| 80 | for i, svc := range svcs { |
| 81 | files = append(files, payloadBuilders(genpkg, svc, cmds[i].CommandData, data)) |
| 82 | } |
| 83 | return files |
| 84 | } |
| 85 | |
| 86 | func buildSubcommandData(sd *ServiceData, e *EndpointData) *subcommandData { |
| 87 | flags, buildFunction := buildFlags(sd, e) |