-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changed service template to correctly handle required positional arguments #405
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks -- this LGTM except for the create case. There the prompt is unexpected.
Can you investigate why this happens?
@pietern see the fix and explanation why code generator worked that way here: databricks/databricks-sdk-go#403 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spot checked most generated commands and looks good!
Could you see if you can keep Args: cobra.MaximumNArgs(),
for commands that have an optional argument? It would we 1 in cases where we can prompt.
.codegen/service.go.tmpl
Outdated
{{- $hasSinglePosArg := and $hasPosArgs (eq 1 (len .Request.RequiredFields)) -}} | ||
{{- $serviceHasNamedIdMap := and .Service.List .Service.List.NamedIdMap -}} | ||
{{- $hasIdPrompt := and $hasSinglePosArg $serviceHasNamedIdMap -}} | ||
{{- $wait := and .Wait (and (not .IsCrudRead) (not (eq .SnakeName "get_run"))) -}} | ||
{{- $canAcceptJson := and .Request (not .Request.IsOnlyPrimitiveFields) -}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't these still be able to accept json if the request is only primitive fields?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically we can allow JSON to be accepted in all cases. Right now if all fields are primitive we don't add --json
flag and instead add a specific flag for each of the fields.
We can change it to always accept JSON though
var {{.CamelName}}Cmd = &cobra.Command{ | ||
Use: "{{.KebabName}}{{if $hasPosArgs}}{{range .Request.RequiredFields}} {{.ConstantName}}{{end}}{{end}}", | ||
{{if .Description -}} | ||
Short: `{{.Summary | without "`"}}`, | ||
Long: `{{.Comment " " 80 | without "`"}}`, | ||
{{end}} | ||
Annotations: map[string]string{},{{if and (not $hasIdPrompt) $hasPosArgs }} | ||
Args: cobra.ExactArgs({{len .Request.RequiredFields}}),{{end}} | ||
Annotations: map[string]string{},{{if $hasRequiredArgs }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, you can use {{-
to strip preceding whitespace (including newlines).
See https://pkg.go.dev/text/template#hdr-Text_and_spaces
This means you could put the if
on the next line for better readability.
} | ||
return check(cmd, args) | ||
}, | ||
PreRunE: root.MustWorkspaceClient, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look fmt'ed.
## Changes CLI: * Add directory tracking to sync ([#425](#425)). * Add fs cat command for dbfs files ([#430](#430)). * Add fs ls command for dbfs ([#429](#429)). * Add fs mkdirs command for dbfs ([#432](#432)). * Add fs rm command for dbfs ([#433](#433)). * Add installation instructions ([#458](#458)). * Add new line to cmdio JSON rendering ([#443](#443)). * Add profile on `databricks auth login` ([#423](#423)). * Add readable console logger ([#370](#370)). * Add workspace export-dir command ([#449](#449)). * Added secrets input prompt for secrets put-secret command ([#413](#413)). * Added spinner when loading command prompts ([#420](#420)). * Better error message if can not load prompts ([#437](#437)). * Changed service template to correctly handle required positional arguments ([#405](#405)). * Do not generate prompts for certain commands ([#438](#438)). * Do not prompt for List methods ([#411](#411)). * Do not use FgWhite and FgBlack for terminal output ([#435](#435)). * Skip path translation of job task for jobs with a Git source ([#404](#404)). * Tweak profile prompt ([#454](#454)). * Update with the latest Go SDK ([#457](#457)). * Use cmdio in version command for `--output` flag ([#419](#419)). Bundles: * Check for nil environment before accessing it ([#453](#453)). Dependencies: * Bump github.com/hashicorp/terraform-json from 0.16.0 to 0.17.0 ([#459](#459)). * Bump github.com/mattn/go-isatty from 0.0.18 to 0.0.19 ([#412](#412)). Internal: * Add Mkdir and ReadDir functions to filer.Filer interface ([#414](#414)). * Add Stat function to filer.Filer interface ([#421](#421)). * Add check for path is a directory in filer.ReadDir ([#426](#426)). * Add fs.FS adapter for the filer interface ([#422](#422)). * Add implementation of filer.Filer for local filesystem ([#460](#460)). * Allow equivalence checking of filer errors to fs errors ([#416](#416)). * Fix locker integration test ([#417](#417)). * Implement DBFS filer ([#139](#139)). * Include recursive deletion in filer interface ([#442](#442)). * Make filer.Filer return fs.DirEntry from ReadDir ([#415](#415)). * Speed up sync integration tests ([#428](#428)).
…vided (#535) ## Changes When there are positional required parameters in the command which can't be unmarshalled from JSON, we should require them despite the fact `--json` flag is provided. The reason is that for some of the command, for example, `databricks groups patch ID` these arguments are actually path arguments in API and can't be set as part of `--json` body provided. Original change which introduced this ignore logic is here: #405 Fixes #533, #537 Note: Code generation is based on the change in this PR: databricks/databricks-sdk-go#536 ## Tests 1. Running `cli groups patch 123 --json {...}` works correctly Backward compatibility tests with previous changes from #405 1. `cli clusters events --json '{"cluster_id": "1029-xxxx"}'` - works, returns list of events 2. `cli clusters events 1029-xxxx` - works, returns list of events 3. `cli clusters events` - works, first prompts for Cluster ID and then returns the list of events
Changes
Change 1
Previously codegen considered that command has positional arguments only if all arguments are primitives.
With this change, we consider command to have positional arguments if all required arguments are primitives.
It helps to solve thew issue when running command like
cli secrets create-scope
,cli users create
with no passed argumentsChange 2
It also makes args optional if there is a prompt for it or JSON can be accepted instead.
All 3 command below will yield the same valid results
Change 3
Also now it always allow
--json
flag and ignore all positional arguments in this caseNote: these code is generated with code generator containing this fix
databricks/databricks-sdk-go#403
Tests
Running
cli secrets create-scope
,cli users create
and etc with no arguments.No panic, instead CLI correctly prompts potential input or shows correct error message.
Before
After