Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 74
Workflow Template Enhancements#324
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
107f0e8b043d06b9d68b966521b094a1913cad495465f9520c2895157400d713e55769f75510ff53059e61ae60db1aeffef82c374a0541d376f92af568f93be2c5a9133d770e2c45d465a517119076e2fe38f13f8c792fbd88b3dabf1d7167e6614ea80a6d5148eba27b539e09fb8005e370432d48f2625706cbb98d2e58f8652d2157f5c0bc488d61ceef564be85f9e9f4341be850bde186f26b96f437f32b9d4bd4c7744c19f4774e878311ffd2423d3c2ee201a5bb9054e66da6ebeFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,6 +2,7 @@ package config | ||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| log "github.com/sirupsen/logrus" | ||
| ) | ||
| @@ -26,6 +27,7 @@ type BuilderVar struct { | ||
| Description string `yaml:"description"` | ||
| ExampleValues []string `yaml:"exampleValues"` | ||
| Type string `yaml:"type"` | ||
| Value string `yaml:"value"` | ||
| } | ||
| type BuilderVarDefault struct { | ||
| @@ -67,21 +69,50 @@ func (d *DraftConfig) GetNameOverride(path string) string { | ||
| } | ||
| // ApplyDefaultVariables will apply the defaults to variables that are not already set | ||
| func (d *DraftConfig) ApplyDefaultVariables(customConfig map[string]string) error { | ||
| func (d *DraftConfig) ApplyDefaultVariables(customInputs map[string]string) error { | ||
| varIdxMap := VariableIdxMap(d.Variables) | ||
| for _, variable := range d.Variables { | ||
| // handle where variable is not set or is set to an empty string from cli handling | ||
| if val, ok := customConfig[variable.Name]; !ok || val == "" { | ||
| if variable.Default.Value == "" { | ||
| return errors.New("variable " + variable.Name + " has no default value") | ||
| if customInputs[variable.Name] == "" { | ||
| if variable.Default.ReferenceVar != "" { | ||
| defaultVal, err := recurseReferenceVars(d.Variables, d.Variables[varIdxMap[variable.Default.ReferenceVar]], customInputs, varIdxMap, d.Variables[varIdxMap[variable.Default.ReferenceVar]], true) | ||
| if err != nil { | ||
| return fmt.Errorf("apply default variables: %w", err) | ||
| } | ||
| log.Infof("Variable %s defaulting to value %s", variable.Name, customInputs[variable.Name]) | ||
| customInputs[variable.Name] = defaultVal | ||
| } | ||
| if customInputs[variable.Name] == "" { | ||
| if variable.Default.Value != "" { | ||
| log.Infof("Variable %s defaulting to value %s", variable.Name, variable.Default.Value) | ||
| customInputs[variable.Name] = variable.Default.Value | ||
| } else { | ||
| return fmt.Errorf("variable %s has no default value", variable.Name) | ||
| } | ||
| } | ||
| log.Infof("Variable %s defaulting to value %s", variable.Name, variable.Default.Value) | ||
| customConfig[variable.Name] = variable.Default.Value | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| // recurseReferenceVars recursively checks each variable's ReferenceVar if it doesn't have a custom input. If there's no more ReferenceVars, it will return the default value of the last ReferenceVar. | ||
| func recurseReferenceVars(variables []BuilderVar, variable BuilderVar, customInputs map[string]string, varIdxMap map[string]int, variableCheck BuilderVar, isFirst bool) (string, error) { | ||
meecethereese marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if !isFirst && variable.Name == variableCheck.Name { | ||
| return "", errors.New("cyclical reference detected") | ||
| } | ||
| if customInputs[variable.Name] != "" { | ||
| return customInputs[variable.Name], nil | ||
| } else if variable.Default.ReferenceVar != "" { | ||
| return recurseReferenceVars(variables, variables[varIdxMap[variable.Default.ReferenceVar]], customInputs, varIdxMap, variableCheck, false) | ||
| } | ||
| return variable.Default.Value, nil | ||
| } | ||
| func VariableIdxMap(variables []BuilderVar) map[string]int { | ||
| varIdxMap := make(map[string]int) | ||
| @@ -92,6 +123,31 @@ func VariableIdxMap(variables []BuilderVar) map[string]int { | ||
| return varIdxMap | ||
| } | ||
| func (d *DraftConfig) VariableMap() (map[string]string, error) { | ||
| envArgs := make(map[string]string) | ||
| for _, variable := range d.Variables { | ||
| envArgs[variable.Name] = variable.Value | ||
| } | ||
| err := d.ApplyDefaultVariables(envArgs) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("creating variable map: %w", err) | ||
| } | ||
| return envArgs, nil | ||
| } | ||
| func (d *DraftConfig) VariableIdxMap() map[string]int { | ||
| varIdxMap := make(map[string]int) | ||
| for i, variable := range d.Variables { | ||
| varIdxMap[variable.Name] = i | ||
| } | ||
| return varIdxMap | ||
| } | ||
| // TemplateVariableRecorder is an interface for recording variables that are used read using draft configs | ||
| type TemplateVariableRecorder interface { | ||
| Record(key, value string) | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.