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