-
Notifications
You must be signed in to change notification settings - Fork 212
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
Allow using default params #1646
Allow using default params #1646
Conversation
Signed-off-by: div_bhasin <[email protected]>
Signed-off-by: div_bhasin <[email protected]>
Signed-off-by: div_bhasin <[email protected]>
Signed-off-by: div_bhasin <[email protected]>
Signed-off-by: div_bhasin <[email protected]>
170abf4
to
1b93de1
Compare
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.
Overall this is going in the right direction! 👍
I want to call out that this is a slightly different set of functionality than what was outlined in #1604. The issue originally asked that when the user selects "use default value" that the parameter would not be included in the parameter set. By not specifying the value, when the parameter set is used, the default value of the bundle is used instead.
This will instead lock in the parameter set to the default value, and if the bundle changes the default value, continue to use the value set.
So you will need to change the implementation to not persist a parameter value when the user selects "use default" instead.
pkg/generator/parameters.go
Outdated
@@ -52,7 +52,9 @@ func (opts *GenerateParametersOptions) genParameterSet(fn generator) (parameters | |||
if parameters.IsInternal(name, opts.Bundle) { | |||
continue | |||
} | |||
c, err := fn(name, surveyParameters) | |||
defaultVal, _ := getDefaultParamValue(opts.Bundle, name) |
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.
If this function returns an error, we should handle it here instead of silently ignoring it.
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.
@carolynvs I don't know what would be the best way to handle this error. In most cases, it means there is no default value found, so in my opinion, we should ignore and move on so the user can select from any of the other options. We can perhaps a log a message saying this param does not have a default value according to the bundle. Returning the error here is currently also causing a test to fail.
pkg/generator/generator.go
Outdated
options := []string{questionSecret, questionValue, questionEnvVar, questionPath, questionCommand} | ||
questionDefault := fmt.Sprintf("use default value (%s)", defaultVal) | ||
|
||
if defaultVal != "" { |
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.
Parameters can default to an empty string, and can also be other types than string. One way to deal with that is to make defaultVal
an interface{}
, and check for null.
When we need to print the value, this is the logic:
porter/pkg/porter/parameters.go
Lines 385 to 395 in 2b176ec
var printedValue string | |
switch val := v.Value.(type) { | |
case string: | |
printedValue = val | |
default: | |
b, err := json.Marshal(v.Value) | |
if err != nil { | |
return "error rendering value" | |
} | |
printedValue = string(b) | |
} |
Which handles when the parameter is a complex type such as an array or object.
pkg/generator/parameters.go
Outdated
return "", fmt.Errorf("parameter definition for %s is empty", name) | ||
} | ||
|
||
if def.Default != nil { |
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.
We can return the default value directly without any comparison or formatting, because we will render the value for display in the genSurvey
function.
Signed-off-by: div_bhasin <[email protected]>
32a8cef
to
cb97d13
Compare
@carolynvs Made the changes that you requested. The first screenshot remains but the second screenshot now changes to: If I am understanding correctly, we should leave the param value empty like above and it will just get filled in with the default at run time. |
Signed-off-by: div_bhasin <[email protected]>
@divbhasin The expectation is that when I generate parameters and select a default, that parameter is not included in the parameter set at all. So in the second screenshot, you would not need the name parameter listed at all. For example, this is what it generates now with your PR:
and this is what we need:
|
Signed-off-by: div_bhasin <[email protected]>
@divbhasin Looks like there is a failing unit test related to your change |
A related PR have been merged, #3100, allowing skipping a value instead of showing the default value |
What does this change
Adds a new default value option to the
porter parameters generate ...
command survey if there is a default value defined for the chosen parameter in the bundle.To check, we can run
porter parameters show ...
and see that the default value has been set:What issue does it fix
Closes #1604
If there is not an existing issue, please make sure we have context on why this change is needed. See our Contributing Guide for examples of when an existing issue isn't necessary.
Notes for the reviewer
Environment variable is still the default option. Not sure if we should the "default value" option the new default for when the survey opens. Also, would appreciate tips on if there are better approaches to organize the code.
Checklist