github.com/go-swagger/go-swagger@v0.31.0/generator/templates/cli/registerflag.gotmpl (about)

     1  {{/*util functions to run or register cmd flags*/}}
     2  
     3  {{ define "flagdescriptionvar" }}
     4      {{- $fullDescription := (escapeBackticks .Description) }}
     5      {{- if .Required}}
     6          {{- $fullDescription = printf "Required. %v" $fullDescription}}
     7      {{- end}}
     8      {{- if .Enum }}
     9          {{- $fullDescription = printf "Enum: %v. %v" (json .Enum) $fullDescription}}
    10      {{- end }}
    11      {{ flagDescriptionVar .Name }} := `{{ $fullDescription }}`
    12  {{ end }}
    13  
    14  {{ define "flagnamevar" }}
    15      var {{ flagNameVar .Name }} string
    16      if cmdPrefix == "" {
    17          {{ flagNameVar .Name }} = "{{ .Name }}"
    18      }else{
    19          {{ flagNameVar .Name }} = fmt.Sprintf("%v.{{ .Name }}", cmdPrefix)
    20      }
    21  {{ end }}
    22  
    23  {{ define "flagdefaultvar" }}
    24      var {{ flagDefaultVar .Name }} {{ .GoType }} {{ if .Default }}= {{ printf "%#v" .Default }}{{ end }}
    25  {{ end }}
    26  
    27  {{/* Not used. CLI does not mark flag as required, and required will be checked by validation in future */}}
    28  {{/* {{ define "requiredregistrator" }}
    29  	if err := cmd.MarkPersistentFlagRequired({{ flagNameVar .Name }}); err != nil{
    30  		return err
    31  	}
    32  {{ end }} */}}
    33  
    34  {{ define "enumcompletion" }} {{/*only used for primitive types. completion type is always string.*/}}
    35  {{ if .Enum }}
    36  if err := cmd.RegisterFlagCompletionFunc({{ flagNameVar .Name }},
    37      func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    38          var res []string
    39          if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil {
    40              panic(err)
    41          }
    42  		return res, cobra.ShellCompDirectiveDefault
    43  	}); err != nil{
    44      return err
    45  }
    46  {{ end }}
    47  {{ end }}
    48  
    49  {{/* intended to be used on struct GenSchema with .IsPrimitive */}}
    50  {{ define "primitiveregistrator" }}
    51      {{- if or (eq .GoType "int64") (eq .GoType "int32") (eq .GoType "string") (eq .GoType "float64") (eq .GoType "float32") (eq .GoType "bool") }}
    52          {{ template "flagdescriptionvar" . }}
    53          {{ template "flagnamevar" . }}
    54          {{ template "flagdefaultvar" . }}
    55          _ = cmd.PersistentFlags().{{ pascalize .GoType }}({{ flagNameVar .Name }}, {{ flagDefaultVar .Name }}, {{ flagDescriptionVar .Name }})
    56          {{ template "enumcompletion" . }}
    57      {{- else if or (eq .GoType "strfmt.DateTime") (eq .GoType "strfmt.UUID") (eq .GoType "strfmt.ObjectId") (eq .GoType "strfmt.ULID") }} {{/* read as string */}}
    58          {{ template "flagdescriptionvar" . }}
    59          {{ template "flagnamevar" . }}
    60          _ = cmd.PersistentFlags().String({{ flagNameVar .Name }}, "", {{ flagDescriptionVar .Name }})
    61          {{ template "enumcompletion" . }}
    62      {{- else }}
    63          // warning: primitive {{.Name}} {{.GoType }} is not supported by go-swagger cli yet
    64      {{- end }}
    65  {{ end }}
    66  
    67  {{ define "arrayregistrator" }}
    68      {{- if or (eq .GoType "[]int64") (eq .GoType "[]int32") (eq .GoType "[]string") (eq .GoType "[]float64") (eq .GoType "[]float32") (eq .GoType "[]bool") }}
    69          {{ template "flagdescriptionvar" . }}
    70          {{ template "flagnamevar" . }}
    71          {{ template "flagdefaultvar" . }}
    72          _ = cmd.PersistentFlags().{{ pascalize .GoType }}Slice({{ flagNameVar .Name }}, {{ flagDefaultVar .Name }}, {{ flagDescriptionVar .Name }})
    73          {{ template "enumcompletion" . }}
    74      {{- else if or (eq .GoType "[]strfmt.DateTime") (eq .GoType "[]strfmt.UUID") (eq .GoType "[]strfmt.ObjectId") (eq .GoType "[]strfmt.ULID") }} {{/* read as string */}}
    75          {{ template "flagdescriptionvar" . }}
    76          {{ template "flagnamevar" . }}
    77          _ = cmd.PersistentFlags().StringSlice({{ flagNameVar .Name }}, []string{}, {{ flagDescriptionVar .Name }})
    78      {{- else }}
    79          // warning: array {{.Name}} {{.GoType }} is not supported by go-swagger cli yet
    80      {{- end }}
    81  {{ end }}
    82  
    83  
    84  {{/* each body parameter gets a string flag to input json raw string */}}
    85  {{ define "modelparamstringregistrator" }}
    86      {{ template "flagnamevar" . }}
    87      _ = cmd.PersistentFlags().String({{ flagNameVar .Name }}, "", `Optional json string for [{{ .Name }}]. {{ escapeBackticks .Description }}`)
    88  {{ end }}
    89  
    90  {{ define "modelparamregistrator" }} {{/* register a param that has a schema */}}
    91      // add flags for body {{/*use go type as the flag prefix. There is no good way to determine the original str case in spec*/}}
    92  	if err := registerModel{{ pascalize (dropPackage .GoType) }}Flags(0, "{{ camelize (dropPackage .GoType) }}", cmd); err != nil {
    93          return err
    94      }
    95  {{ end }}