Skip to content

Commit

Permalink
Always require path parameters as positional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnester committed Jan 17, 2024
1 parent ef67b17 commit adc1044
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 41 deletions.
57 changes: 33 additions & 24 deletions .codegen/service.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -125,33 +125,35 @@ func new{{.PascalName}}() *cobra.Command {
{{- $fullCommandName := (print $serviceName " " .KebabName) -}}
{{- $noPrompt := or .IsCrudCreate (in $excludeFromPrompts $fullCommandName) }}

{{- $hasPosArgs := and (not .MustUseJson) (and .Request (or .Request.IsAllRequiredFieldsPrimitive (eq .PascalName "RunNow"))) -}}
{{- $hasPosArgs := .HasRequiredPositionalArguments -}}
{{- $hasSinglePosArg := and $hasPosArgs (eq 1 (len .Request.RequiredFields)) -}}
{{- $serviceHasNamedIdMap := and (and .Service.List .Service.List.NamedIdMap) (not (eq .PascalName "List")) -}}
{{- $hasIdPrompt := and (not $noPrompt) (and $hasSinglePosArg $serviceHasNamedIdMap) -}}
{{- $wait := and .Wait (and (not .IsCrudRead) (not (eq .SnakeName "get_run"))) -}}
{{- $hasRequiredArgs := and (not $hasIdPrompt) $hasPosArgs -}}
{{- $hasSingleRequiredRequestBodyFieldWithPrompt := and (and $hasIdPrompt .Request) (eq 1 (len .Request.RequiredRequestBodyFields)) -}}
{{- $hasCustomArgHandler := and (not .MustUseJson) (or $hasRequiredArgs (and .CanUseJson .Request.HasRequiredRequestBodyFields)) -}}
{{- $onlyPathArgsRequiredAsPositionalArguments := and .Request (eq (len .RequiredPositionalArguments) (len .Request.RequiredPathFields)) -}}
{{- $hasDifferentArgsWithJsonFlag := and (not $onlyPathArgsRequiredAsPositionalArguments) (and .CanUseJson .Request.HasRequiredRequestBodyFields) -}}
{{- $hasCustomArgHandler := or $hasRequiredArgs $hasDifferentArgsWithJsonFlag -}}

{{- $atleastOneArgumentWithDescription := false -}}
{{- if $hasPosArgs -}}
{{- range .Request.RequiredFields -}}
{{- range .RequiredPositionalArguments -}}
{{- if .HasComment -}}
{{- $atleastOneArgumentWithDescription = true -}}
{{- break -}}
{{- end -}}
{{- end -}}
{{- end -}}

cmd.Use = "{{.KebabName}}{{if $hasPosArgs}}{{range .Request.RequiredFields}} {{.ConstantName}}{{end}}{{end}}"
cmd.Use = "{{.KebabName}}{{if $hasPosArgs}}{{range .RequiredPositionalArguments}} {{.ConstantName}}{{end}}{{end}}"
{{- if .Description }}
cmd.Short = `{{.Summary | without "`"}}`
cmd.Long = `{{.Comment " " 80 | without "`"}}
{{- if $atleastOneArgumentWithDescription }}

Arguments:
{{- range .Request.RequiredFields }}
{{- range .RequiredPositionalArguments }}
{{ .ConstantName }}: {{.Comment " " 80 | without "`"}}
{{- end -}}
{{- end -}}
Expand All @@ -166,7 +168,7 @@ func new{{.PascalName}}() *cobra.Command {
cmd.Annotations = make(map[string]string)
{{ if $hasCustomArgHandler }}
cmd.Args = func(cmd *cobra.Command, args []string) error {
{{- if and .CanUseJson .Request.HasRequiredRequestBodyFields }}
{{- if $hasDifferentArgsWithJsonFlag }}
if cmd.Flags().Changed("json") {
err := cobra.ExactArgs({{len .Request.RequiredPathFields}})(cmd, args)
if err != nil {
Expand All @@ -180,7 +182,7 @@ func new{{.PascalName}}() *cobra.Command {
}
{{- end }}
{{- if $hasRequiredArgs }}
check := cobra.ExactArgs({{len .Request.RequiredFields}})
check := cobra.ExactArgs({{len .RequiredPositionalArguments}})
return check(cmd, args)
{{- else}}
return nil
Expand All @@ -201,7 +203,7 @@ func new{{.PascalName}}() *cobra.Command {
}{{end}}{{ if .MustUseJson }}else {
return fmt.Errorf("please provide command input in JSON format by specifying the --json flag")
}{{- end}}
{{- if (not .MustUseJson) }}
{{- if $hasPosArgs }}
{{- if and .CanUseJson $hasSingleRequiredRequestBodyFieldWithPrompt }} else {
{{- end}}
{{- if $hasIdPrompt}}
Expand All @@ -225,23 +227,9 @@ func new{{.PascalName}}() *cobra.Command {
{{- end -}}

{{$method := .}}
{{- range $arg, $field := .Request.RequiredFields}}
{{- $optionalIfJsonIsUsed := and (not $hasIdPrompt) (and $field.IsRequestBodyField $method.CanUseJson) }}
{{- if $optionalIfJsonIsUsed }}
if !cmd.Flags().Changed("json") {
{{- end }}
{{if not $field.Entity.IsString -}}
_, err = fmt.Sscan(args[{{$arg}}], &{{$method.CamelName}}Req.{{$field.PascalName}})
if err != nil {
return fmt.Errorf("invalid {{$field.ConstantName}}: %s", args[{{$arg}}])
}{{else -}}
{{$method.CamelName}}Req.{{$field.PascalName}} = args[{{$arg}}]
{{- end -}}
{{- if $optionalIfJsonIsUsed }}
}
{{- end }}
{{- range $arg, $field := .RequiredPositionalArguments}}
{{- template "args-scan" (dict "Arg" $arg "Field" $field "Method" $method "HasIdPrompt" $hasIdPrompt)}}
{{- end -}}

{{- if and .CanUseJson $hasSingleRequiredRequestBodyFieldWithPrompt }}
}
{{- end}}
Expand Down Expand Up @@ -338,3 +326,24 @@ func init() {
{{- else}}/* NOT PRIMITIVE */
{{- end -}}
{{- end -}}

{{- define "args-scan" -}}
{{- $field := .Field -}}
{{- $method := .Method -}}
{{- $arg := .Arg -}}
{{- $hasIdPrompt := .HasIdPrompt -}}
{{- $optionalIfJsonIsUsed := and (not $hasIdPrompt) (and $field.IsRequestBodyField $method.CanUseJson) }}
{{- if $optionalIfJsonIsUsed }}
if !cmd.Flags().Changed("json") {
{{- end }}
{{if not $field.Entity.IsString -}}
_, err = fmt.Sscan(args[{{$arg}}], &{{$method.CamelName}}Req.{{$field.PascalName}})
if err != nil {
return fmt.Errorf("invalid {{$field.ConstantName}}: %s", args[{{$arg}}])
}{{else -}}
{{$method.CamelName}}Req.{{$field.PascalName}} = args[{{$arg}}]
{{- end -}}
{{- if $optionalIfJsonIsUsed }}
}
{{- end }}
{{- end -}}
13 changes: 11 additions & 2 deletions cmd/account/budgets/budgets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions cmd/account/workspace-assignment/workspace-assignment.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion cmd/workspace/alerts/alerts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions cmd/workspace/artifact-allowlists/artifact-allowlists.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions cmd/workspace/connections/connections.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions cmd/workspace/dashboard-widgets/dashboard-widgets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions cmd/workspace/query-visualizations/query-visualizations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit adc1044

Please sign in to comment.