-
Notifications
You must be signed in to change notification settings - Fork 4
change behavior of runtime and globals opts for action #98
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| package action | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/launchrctl/launchr/pkg/jsonschema" | ||
| ) | ||
|
|
||
| // FlagsGroup holds definitions, current state, and default values of flags. | ||
| // @todo think about moving it to new input validation service alongside. See notes in actionManagerMap.ValidateFlags. | ||
| type FlagsGroup struct { | ||
| name string | ||
| definitions ParametersList | ||
| values map[string]any | ||
| defaults map[string]any | ||
| } | ||
|
|
||
| // NewFlagsGroup returns a new instance of [FlagsGroup] | ||
| func NewFlagsGroup(name string) *FlagsGroup { | ||
| return &FlagsGroup{ | ||
| name: name, | ||
| definitions: make(ParametersList, 0), | ||
| values: make(map[string]any), | ||
| defaults: make(map[string]any), | ||
| } | ||
| } | ||
|
|
||
| // GetName returns the name of the flags group. | ||
| func (p *FlagsGroup) GetName() string { | ||
| return p.name | ||
| } | ||
|
|
||
| // GetAll returns the latest state of flags. | ||
| func (p *FlagsGroup) GetAll() InputParams { | ||
| result := make(InputParams) | ||
| for name, value := range p.defaults { | ||
| if _, ok := p.values[name]; !ok { | ||
| result[name] = value | ||
| } else { | ||
| result[name] = p.values[name] | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| func (p *FlagsGroup) exists(name string) bool { | ||
| _, ok := p.defaults[name] | ||
| return ok | ||
| } | ||
|
|
||
| // Get returns state of a named flag. | ||
| // Return false if a flag doesn't exist. | ||
| func (p *FlagsGroup) Get(name string) (any, bool) { | ||
| if !p.exists(name) { | ||
| return nil, false | ||
| } | ||
|
|
||
| var value any | ||
| if v, ok := p.values[name]; ok { | ||
| value = v | ||
| } else { | ||
| value = p.defaults[name] | ||
| } | ||
|
|
||
| return value, true | ||
| } | ||
|
|
||
| // Set sets new state value for a flag. Does nothing if flag doesn't exist. | ||
| func (p *FlagsGroup) Set(name string, value any) { | ||
| if !p.exists(name) { | ||
| return | ||
| } | ||
|
|
||
| p.values[name] = value | ||
| } | ||
|
|
||
| // Unset removes the flag value. | ||
| // The default value will be returned during [FlagsGroup.GetAll] if flag is not set. | ||
| func (p *FlagsGroup) Unset(name string) { | ||
| delete(p.values, name) | ||
| } | ||
|
|
||
| // GetDefinitions returns [ParametersList] with flags definitions. | ||
| func (p *FlagsGroup) GetDefinitions() ParametersList { | ||
| return p.definitions | ||
| } | ||
|
|
||
| // AddDefinitions adds new flag definition. | ||
| func (p *FlagsGroup) AddDefinitions(opts ParametersList) { | ||
| registered := make(map[string]struct{}) | ||
|
|
||
| for _, def := range p.definitions { | ||
| registered[def.Name] = struct{}{} | ||
| } | ||
|
|
||
| for _, opt := range opts { | ||
| if opt.Name == "" { | ||
| panic(fmt.Sprintf("%s flag name cannot be empty", p.name)) | ||
| } | ||
|
|
||
| if _, exists := registered[opt.Name]; exists { | ||
| panic(fmt.Sprintf("duplicate %s flag has been detected %s", p.name, opt.Name)) | ||
| } | ||
|
|
||
| p.definitions = append(p.definitions, opt) | ||
| } | ||
|
|
||
| for _, d := range p.definitions { | ||
| p.defaults[d.Name] = d.Default | ||
| } | ||
| } | ||
|
|
||
| // JSONSchema returns JSON schema of a flags group. | ||
| func (p *FlagsGroup) JSONSchema() jsonschema.Schema { | ||
| opts, optsReq := p.definitions.JSONSchema() | ||
| return jsonschema.Schema{ | ||
| Type: jsonschema.Object, | ||
| Required: []string{p.name}, | ||
| Properties: map[string]any{ | ||
| p.name: map[string]any{ | ||
| "type": "object", | ||
| "title": p.name, | ||
| "properties": opts, | ||
| "required": optsReq, | ||
| "additionalProperties": false, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // ValidateFlags validates input flags. | ||
| func (p *FlagsGroup) ValidateFlags(params InputParams) error { | ||
| s := p.JSONSchema() | ||
| return jsonschema.Validate(s, map[string]any{p.name: params}) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.