github.com/go-swagger/go-swagger@v0.31.0/generator/templates/cli/operation.gotmpl (about) 1 // Code generated by go-swagger; DO NOT EDIT. 2 3 {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }} 4 5 package cli {{/* TODO: do not hardcode cli pkg */}} 6 7 // This file was generated by the swagger tool. 8 // Editing this file might prove futile when you re-run the swagger generate command 9 10 import ( 11 "fmt" 12 13 {{ imports .DefaultImports }} 14 {{ imports .Imports }} 15 16 "github.com/spf13/cobra" 17 "github.com/go-openapi/runtime" 18 "github.com/go-openapi/swag" 19 httptransport "github.com/go-openapi/runtime/client" 20 ) 21 22 // make{{ cmdName . }} returns a command to handle operation {{ camelize .Name }} 23 func make{{ cmdName . }}() (*cobra.Command, error) { 24 cmd := &cobra.Command{ 25 Use: "{{ .Name }}", 26 Short: `{{ escapeBackticks .Description}}`, 27 RunE: runOperation{{pascalize .Package}}{{ pascalize .Name }}, 28 } 29 30 if err := registerOperation{{pascalize .Package}}{{ pascalize .Name }}ParamFlags(cmd); err != nil{ 31 return nil, err 32 } 33 34 return cmd, nil 35 } 36 37 {{ $operationGroup := .Package }} 38 {{ $operation := .Name }} 39 {{ $operationPkgAlias := .PackageAlias }} 40 // runOperation{{pascalize $operationGroup }}{{ pascalize $operation }} uses cmd flags to call endpoint api 41 func runOperation{{pascalize $operationGroup }}{{ pascalize $operation }}(cmd *cobra.Command, args []string) error{ 42 appCli, err := makeClient(cmd, args) 43 if err != nil { 44 return err 45 } 46 // retrieve flag values from cmd and fill params 47 params := {{ .PackageAlias }}.New{{ pascalize .Name}}Params() 48 {{- range .Params }} 49 if err, _ = retrieveOperation{{pascalize $operationGroup }}{{ pascalize $operation }}{{ pascalize .Name }}Flag(params, "", cmd); err != nil{ 50 return err 51 } 52 {{- end }} {{/*Params*/}} 53 if dryRun { {{/* Note: dry run is not very useful for now, but useful when validation is added in future*/}} 54 logDebugf("dry-run flag specified. Skip sending request.") 55 return nil 56 } 57 // make request and then print result 58 {{- /*Package string is the operation name*/}} 59 msgStr, err := parseOperation{{pascalize .Package}}{{ pascalize .Name }}Result(appCli.{{- pascalize .Package }}.{{ pascalize .Name }}(params {{- if .Authorized}}, nil{{ end }}{{ if .HasStreamingResponse }}, &bytes.Buffer{}{{ end }})) 60 if err != nil { 61 return err 62 } 63 64 if !debug{ {{/* In debug mode content should have been printed in transport layer, so do not print again*/}} 65 fmt.Println(msgStr) 66 } 67 68 return nil 69 } 70 71 // registerOperation{{pascalize $operationGroup }}{{ pascalize $operation }}ParamFlags registers all flags needed to fill params 72 func registerOperation{{pascalize $operationGroup }}{{ pascalize $operation }}ParamFlags(cmd *cobra.Command) error { 73 {{- range .Params }} 74 if err := registerOperation{{pascalize $operationGroup }}{{ pascalize $operation }}{{pascalize .Name }}ParamFlags("", cmd); err != nil{ 75 return err 76 } 77 {{- end }} 78 return nil 79 } 80 {{/*register functions for each fields in this operation*/}} 81 {{- range .Params }} 82 83 func registerOperation{{pascalize $operationGroup }}{{ pascalize $operation }}{{pascalize .Name }}ParamFlags(cmdPrefix string, cmd *cobra.Command) error{ 84 {{- if .IsPrimitive }} 85 {{ template "primitiveregistrator" . }} 86 {{- else if .IsArray }} 87 {{ template "arrayregistrator" . }} 88 {{- else if and .IsBodyParam .Schema (not .IsArray) (not .IsMap) (not .IsStream) }} 89 {{ template "modelparamstringregistrator" . }} 90 {{ template "modelparamregistrator" . }} 91 {{/* Do not mark body flag as required, since the individial flag for body field will be added separately */}} 92 {{- else }} 93 // warning: go type {{ .GoType }} is not supported by go-swagger cli yet. 94 {{- end }} 95 return nil 96 } 97 {{- end }} 98 99 {{/*functions to retrieve each field of params*/}} 100 {{- range .Params }} 101 102 func retrieveOperation{{pascalize $operationGroup }}{{ pascalize $operation }}{{ pascalize .Name }}Flag(m *{{ $operationPkgAlias }}.{{ pascalize $operation }}Params, cmdPrefix string, cmd *cobra.Command) (error,bool){ 103 retAdded := false 104 {{- $flagStr := .Name }} 105 {{- /*only set the param if user set the flag*/}} 106 if cmd.Flags().Changed("{{ $flagStr }}") { 107 {{- if .IsPrimitive }} 108 {{ template "primitiveretriever" . }} 109 {{- else if .IsArray }} 110 {{ template "arrayretriever" . }} 111 {{- else if .IsMap }} 112 // warning: {{ .Name }} map type {{.GoType}} is not supported by go-swagger cli yet 113 {{- else if and .IsBodyParam .Schema .IsComplexObject (not .IsStream) }} 114 {{- /*schema payload can be passed in cmd as a string and here is unmarshalled to model struct and attached in params*/}} 115 // Read {{ $flagStr }} string from cmd and unmarshal 116 {{ flagValueVar .Name }}Str, err := cmd.Flags().GetString("{{ $flagStr }}") 117 if err != nil { 118 return err, false 119 } 120 {{/*Note anonymous body schema is not pointer*/}} 121 {{ flagValueVar .Name }} := {{if containsPkgStr .GoType}}{{ .GoType }}{{else}}{{ .Pkg }}.{{.GoType}}{{ end }}{} 122 if err := json.Unmarshal([]byte({{ flagValueVar .Name }}Str), &{{ flagValueVar .Name }}); err!= nil{ 123 return fmt.Errorf("cannot unmarshal {{ $flagStr }} string in {{.GoType}}: %v", err), false 124 } 125 m.{{ .ID }} = {{- if .IsNullable }}&{{- end }}{{ flagValueVar .Name }} 126 {{- else }} 127 // warning: {{.GoType}} is not supported by go-swagger cli yet 128 {{- end }} {{/*end go type case*/}} 129 } 130 {{- if and .IsBodyParam .Schema .IsComplexObject (not .IsArray) (not .IsMap) (not .IsStream) }} 131 {{- /* Add flags to capture fields in Body. If previously Body struct was constructed in unmarshalling body string, 132 then reuse the struct, otherwise construct an empty value struct to fill. Here body field flags overwrites 133 unmarshalled body string values. */}} 134 {{ flagModelVar .Name }} := m.{{ .ID }} 135 if swag.IsZero({{ flagModelVar .Name }}){ 136 {{ flagModelVar .Name }} = {{- if .IsNullable }}&{{- end }}{{if containsPkgStr .GoType}}{{ .GoType }}{{else}}{{ .Pkg }}.{{.GoType}}{{ end }}{} 137 } 138 {{- /*Only attach the body struct in params if user passed some flag filling some body fields.*/}} 139 {{- /* add "&" to flagModelVar .Name when it is not nullable because the retrieve method always expects a pointer */}} 140 err, added := retrieveModel{{ pascalize (dropPackage .GoType) }}Flags(0, {{if not .IsNullable}}&{{end}}{{ flagModelVar .Name }}, "{{ camelize (dropPackage .GoType) }}", cmd) 141 if err != nil{ 142 return err, false 143 } 144 if added { 145 m.{{.ID}} = {{ flagModelVar .Name }} 146 } 147 148 if dryRun && debug { {{/* dry run we don't get trasnport debug strings, so print it here*/}} 149 {{- $bodyDebugVar := printf "%vDebugBytes" (flagValueVar .Name) }} 150 {{ $bodyDebugVar }}, err := json.Marshal(m.{{.ID}}) 151 if err != nil{ 152 return err, false 153 } 154 logDebugf("{{.ID }} dry-run payload: %v", string({{ $bodyDebugVar }})) 155 } 156 157 retAdded = retAdded || added {{/*body debug string will be printed in transport layer*/}} 158 {{- end }} 159 160 return nil, retAdded 161 } 162 {{- end }} {{/*Params*/}} 163 164 // parseOperation{{pascalize .Package}}{{ pascalize .Name }}Result parses request result and return the string content 165 {{- /*TODO: handle multiple success response case*/}} 166 func parseOperation{{pascalize .Package}}{{ pascalize .Name }}Result({{- if .SuccessResponse }}{{ range $i, $v := .SuccessResponses }} resp{{$i}} *{{$v.Package}}.{{pascalize $v.Name}},{{- end }}{{- end }} respErr error) (string, error){ 167 if respErr != nil { 168 {{- /*error is of type default model. If we can cast, then print the resp.*/}} 169 {{ if .DefaultResponse }} {{with .DefaultResponse}} 170 {{ if .Schema }} 171 var iRespD interface{} = respErr 172 respD, ok := iRespD.(*{{ .Package }}.{{ pascalize .Name }}) 173 if ok { 174 if !swag.IsZero(respD) && !swag.IsZero(respD.Payload) { 175 msgStr,err := json.Marshal(respD.Payload) 176 if err != nil{ 177 return "", err 178 } 179 return string(msgStr), nil 180 } 181 } 182 {{ else }} 183 // Non schema case: warning {{.Name}} is not supported 184 {{ end }} 185 {{ end }} {{ end }} 186 {{- range $i, $v := .Responses }} 187 {{ if .Schema }} 188 var iResp{{$i}} interface{} = respErr 189 resp{{$i}}, ok := iResp{{$i}}.(*{{ .Package }}.{{ pascalize .Name }}) 190 if ok { 191 if !swag.IsZero(resp{{$i}}) && !swag.IsZero(resp{{$i}}.Payload) { 192 msgStr,err := json.Marshal(resp{{$i}}.Payload) 193 if err != nil{ 194 return "", err 195 } 196 return string(msgStr), nil 197 } 198 } 199 {{ else }} 200 // Non schema case: warning {{.Name}} is not supported 201 {{ end }} 202 {{ end }} 203 return "", respErr 204 } 205 {{- range $i, $v := .SuccessResponses }} 206 {{ if .Schema }} 207 {{- with .Schema}} 208 if !swag.IsZero(resp{{$i}}) && !swag.IsZero(resp{{$i}}.Payload) { 209 {{- if or .IsComplexObject .IsArray .IsMap }} 210 msgStr,err := json.Marshal(resp{{$i}}.Payload) 211 if err != nil{ 212 return "", err 213 } 214 {{- else }} 215 msgStr := fmt.Sprintf("%v", resp{{$i}}.Payload) 216 {{- end }} 217 return string(msgStr), nil 218 } 219 {{- end }} 220 {{ else }} 221 // warning: non schema response {{.Name}} is not supported by go-swagger cli yet. 222 {{ end }} 223 {{ end }} 224 return "", nil 225 } 226 227 {{/*for models defined in params, generate their register and retrieve flags functions*/}} 228 {{- range .ExtraSchemas }} 229 {{ template "modelschemacli" . }} 230 {{- end}}