github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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 {{- $flagValueVar := printf "%vValue" (camelize .Name) }} 106 {{- /*only set the param if user set the flag*/}} 107 if cmd.Flags().Changed("{{ $flagStr }}") { 108 {{- if .IsPrimitive }} 109 {{ template "primitiveretriever" . }} 110 {{- else if .IsArray }} 111 {{ template "arrayretriever" . }} 112 {{- else if .IsMap }} 113 // warning: {{ .Name }} map type {{.GoType}} is not supported by go-swagger cli yet 114 {{- else if and .IsBodyParam .Schema .IsComplexObject (not .IsStream) }} 115 {{- /*schema payload can be passed in cmd as a string and here is unmarshalled to model struct and attached in params*/}} 116 // Read {{ $flagStr }} string from cmd and unmarshal 117 {{ $flagValueVar }}Str, err := cmd.Flags().GetString("{{ $flagStr }}") 118 if err != nil { 119 return err, false 120 } 121 {{/*Note anonymous body schema is not pointer*/}} 122 {{ $flagValueVar }} := {{if containsPkgStr .GoType}}{{ .GoType }}{{else}}{{ .Pkg }}.{{.GoType}}{{ end }}{} 123 if err := json.Unmarshal([]byte({{ $flagValueVar }}Str), &{{ $flagValueVar }}); err!= nil{ 124 return fmt.Errorf("cannot unmarshal {{ $flagStr }} string in {{.GoType}}: %v", err), false 125 } 126 m.{{ .ID }} = {{- if .IsNullable }}&{{- end }}{{ $flagValueVar }} 127 {{- else }} 128 // warning: {{.GoType}} is not supported by go-swagger cli yet 129 {{- end }} {{/*end go type case*/}} 130 } 131 {{- if and .IsBodyParam .Schema .IsComplexObject (not .IsArray) (not .IsMap) (not .IsStream) }} 132 {{- /* Add flags to capture fields in Body. If previously Body struct was constructed in unmarshalling body string, 133 then reuse the struct, otherwise construct an empty value struct to fill. Here body field flags overwrites 134 unmarshalled body string values. */}} 135 {{- $flagModelVar := printf "%vModel" (camelize $flagValueVar) }} 136 {{ $flagModelVar }} := m.{{ .ID }} 137 if swag.IsZero({{ $flagModelVar }}){ 138 {{ $flagModelVar }} = {{- if .IsNullable }}&{{- end }}{{if containsPkgStr .GoType}}{{ .GoType }}{{else}}{{ .Pkg }}.{{.GoType}}{{ end }}{} 139 } 140 {{- /*Only attach the body struct in params if user passed some flag filling some body fields.*/}} 141 {{- /* add "&" to $flagModelVar when it is not nullable because the retrieve method always expects a pointer */}} 142 err, added := retrieveModel{{ pascalize (dropPackage .GoType) }}Flags(0, {{if not .IsNullable}}&{{end}}{{ $flagModelVar }}, "{{ camelize (dropPackage .GoType) }}", cmd) 143 if err != nil{ 144 return err, false 145 } 146 if added { 147 m.{{.ID}} = {{ $flagModelVar }} 148 } 149 150 if dryRun && debug { {{/* dry run we don't get trasnport debug strings, so print it here*/}} 151 {{- $bodyDebugVar := printf "%vDebugBytes" (camelize $flagValueVar) }} 152 {{ $bodyDebugVar }}, err := json.Marshal(m.{{.ID}}) 153 if err != nil{ 154 return err, false 155 } 156 logDebugf("{{.ID }} dry-run payload: %v", string({{ $bodyDebugVar }})) 157 } 158 159 retAdded = retAdded || added {{/*body debug string will be printed in transport layer*/}} 160 {{- end }} 161 162 return nil, retAdded 163 } 164 {{- end }} {{/*Params*/}} 165 166 // parseOperation{{pascalize .Package}}{{ pascalize .Name }}Result parses request result and return the string content 167 {{- /*TODO: handle multiple success response case*/}} 168 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){ 169 if respErr != nil { 170 {{- /*error is of type default model. If we can cast, then print the resp.*/}} 171 {{ if .DefaultResponse }} {{with .DefaultResponse}} 172 {{ if .Schema }} 173 var iRespD interface{} = respErr 174 respD, ok := iRespD.(*{{ .Package }}.{{ pascalize .Name }}) 175 if ok { 176 if !swag.IsZero(respD) && !swag.IsZero(respD.Payload) { 177 msgStr,err := json.Marshal(respD.Payload) 178 if err != nil{ 179 return "", err 180 } 181 return string(msgStr), nil 182 } 183 } 184 {{ else }} 185 // Non schema case: warning {{.Name}} is not supported 186 {{ end }} 187 {{ end }} {{ end }} 188 {{- range $i, $v := .Responses }} 189 {{ if .Schema }} 190 var iResp{{$i}} interface{} = respErr 191 resp{{$i}}, ok := iResp{{$i}}.(*{{ .Package }}.{{ pascalize .Name }}) 192 if ok { 193 if !swag.IsZero(resp{{$i}}) && !swag.IsZero(resp{{$i}}.Payload) { 194 msgStr,err := json.Marshal(resp{{$i}}.Payload) 195 if err != nil{ 196 return "", err 197 } 198 return string(msgStr), nil 199 } 200 } 201 {{ else }} 202 // Non schema case: warning {{.Name}} is not supported 203 {{ end }} 204 {{ end }} 205 return "", respErr 206 } 207 {{- range $i, $v := .SuccessResponses }} 208 {{ if .Schema }} 209 {{- with .Schema}} 210 if !swag.IsZero(resp{{$i}}) && !swag.IsZero(resp{{$i}}.Payload) { 211 {{- if or .IsComplexObject .IsArray .IsMap }} 212 msgStr,err := json.Marshal(resp{{$i}}.Payload) 213 if err != nil{ 214 return "", err 215 } 216 {{- else }} 217 msgStr := fmt.Sprintf("%v", resp{{$i}}.Payload) 218 {{- end }} 219 return string(msgStr), nil 220 } 221 {{- end }} 222 {{ else }} 223 // warning: non schema response {{.Name}} is not supported by go-swagger cli yet. 224 {{ end }} 225 {{ end }} 226 return "", nil 227 } 228 229 {{/*for models defined in params, generate their register and retrieve flags functions*/}} 230 {{- range .ExtraSchemas }} 231 {{ template "modelschemacli" . }} 232 {{- end}}