github.com/circl-dev/go-swagger@v0.31.0/generator/templates/cli/operation.gotmpl (about) 1 // Code generated by go-swagger; DO NOT EDIT. 2 3 4 {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }} 5 6 {{- /*TODO: do not hardcode cli pkg*/}} 7 package cli 8 9 // This file was generated by the swagger tool. 10 // Editing this file might prove futile when you re-run the swagger generate command 11 12 import ( 13 {{ imports .DefaultImports }} 14 {{ imports .Imports }} 15 16 "github.com/spf13/cobra" 17 "github.com/circl-dev/runtime" 18 "github.com/go-openapi/swag" 19 httptransport "github.com/circl-dev/runtime/client" 20 ) 21 22 // makeOperation{{pascalize .Package}}{{ pascalize .Name }}Cmd returns a cmd to handle operation {{ camelize .Name }} 23 func makeOperation{{pascalize .Package}}{{ pascalize .Name }}Cmd() (*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 { 54 {{/* Note: dry run is not very useful for now, but useful when validation is added in future*/}} 55 logDebugf("dry-run flag specified. Skip sending request.") 56 return nil 57 } 58 // make request and then print result 59 {{- /*Package string is the operation name*/}} 60 msgStr, err := parseOperation{{pascalize .Package}}{{ pascalize .Name }}Result(appCli.{{- pascalize .Package }}.{{ pascalize .Name }}(params {{ if .HasStreamingResponse }}, &bytes.Buffer{}{{ end }}{{- if .Authorized}}, nil{{ end }})) 61 if err != nil { 62 return err 63 } 64 if !debug{ 65 {{/* In debug mode content should have been printed in transport layer, so do not print again*/}} 66 fmt.Println(msgStr) 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 81 {{/*register functions for each fields in this operation*/}} 82 {{- range .Params }} 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 retreive each field of params*/}} 100 {{- range .Params }} 101 func retrieveOperation{{pascalize $operationGroup }}{{ pascalize $operation }}{{ pascalize .Name }}Flag(m *{{ $operationPkgAlias }}.{{ pascalize $operation }}Params, cmdPrefix string, cmd *cobra.Command) (error,bool){ 102 retAdded := false 103 {{- $flagStr := .Name }} 104 {{- $flagValueVar := printf "%vValue" (camelize .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 }}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 }} := {{if containsPkgStr .GoType}}{{ .GoType }}{{else}}{{ .Pkg }}.{{.GoType}}{{ end }}{} 122 if err := json.Unmarshal([]byte({{ $flagValueVar }}Str), &{{ $flagValueVar }}); err!= nil{ 123 return fmt.Errorf("cannot unmarshal {{ $flagStr }} string in {{.GoType}}: %v", err), false 124 } 125 m.{{ .ID }} = {{- if .IsNullable }}&{{- end }}{{ $flagValueVar }} 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 := printf "%vModel" (camelize $flagValueVar) }} 135 {{ $flagModelVar }} := m.{{ .ID }} 136 if swag.IsZero({{ $flagModelVar }}){ 137 {{ $flagModelVar }} = {{- if .IsNullable }}&{{- end }}{{if containsPkgStr .GoType}}{{ .GoType }}{{else}}{{ .Pkg }}.{{.GoType}}{{ end }}{} 138 } 139 {{- /*Only attach the body struct in params if user passed some flag filling some body fields.*/}} 140 {{- /* add "&" to $flagModelVar when it is not nullable because the retrieve method always expects a pointer */}} 141 err, added := retrieveModel{{ pascalize (dropPackage .GoType) }}Flags(0, {{if not .IsNullable}}&{{end}}{{ $flagModelVar }}, "{{ camelize (dropPackage .GoType) }}", cmd) 142 if err != nil{ 143 return err, false 144 } 145 if added { 146 m.{{.ID}} = {{ $flagModelVar }} 147 } 148 if dryRun && debug { 149 {{/* dry run we don't get trasnport debug strings, so print it here*/}} 150 {{- $bodyDebugVar := printf "%vDebugBytes" (camelize $flagValueVar) }} 151 {{ $bodyDebugVar }}, err := json.Marshal(m.{{.ID}}) 152 if err != nil{ 153 return err, false 154 } 155 logDebugf("{{.ID }} dry-run payload: %v", string({{ $bodyDebugVar }})) 156 } 157 retAdded = retAdded || added 158 {{/*body debug string will be printed in transport layer*/}} 159 {{- end }} 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}}