Prepare configuration scope metadata
This commit is contained in:
38
selectors.go
38
selectors.go
@@ -82,20 +82,20 @@ func configurationSelectionFlags[I any](setConfig, setOverlay func(*I, Supplied[
|
||||
|
||||
func parseString(value string) (string, error) { return value, nil }
|
||||
|
||||
func preflightConfig[C any](suite ConfigSuite[C], input configInput[C], operation ConfigOperation) ([]string, error) {
|
||||
scopes, err := selectConfigScopes(suite, input, operation)
|
||||
func preflightConfig[C any](prepared *preparedConfigSuite[C], input configInput[C], operation ConfigOperation) ([]string, error) {
|
||||
scopes, err := selectConfigScopes(prepared, input, operation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if suite.CheckInput != nil {
|
||||
if err := suite.CheckInput(input.Selection, input.SelectorContext); err != nil {
|
||||
if prepared.suite.CheckInput != nil {
|
||||
if err := prepared.suite.CheckInput(input.Selection, input.SelectorContext); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return scopes, nil
|
||||
}
|
||||
|
||||
func selectConfigScopes[C any](suite ConfigSuite[C], input configInput[C], operation ConfigOperation) ([]string, error) {
|
||||
func selectConfigScopes[C any](prepared *preparedConfigSuite[C], input configInput[C], operation ConfigOperation) ([]string, error) {
|
||||
if err := checkConfigFormat(input.Format); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -110,16 +110,16 @@ func selectConfigScopes[C any](suite ConfigSuite[C], input configInput[C], opera
|
||||
return nil, fmt.Errorf("configuration scope must not be empty")
|
||||
}
|
||||
|
||||
scopes := make([]string, 0, len(suite.Scopes))
|
||||
scopes := make([]string, 0, len(prepared.ordered))
|
||||
if input.Scope == "all" {
|
||||
for _, scope := range orderedConfigScopes(suite.Scopes) {
|
||||
for _, scope := range prepared.ordered {
|
||||
scopes = append(scopes, scope.Name)
|
||||
}
|
||||
} else {
|
||||
scope, ok := findConfigScope(suite.Scopes, input.Scope)
|
||||
scope, ok := prepared.byName[input.Scope]
|
||||
if !ok {
|
||||
choices := make([]string, 0, len(suite.Scopes))
|
||||
for _, available := range orderedConfigScopes(suite.Scopes) {
|
||||
choices := make([]string, 0, len(prepared.ordered))
|
||||
for _, available := range prepared.ordered {
|
||||
choices = append(choices, available.Name)
|
||||
}
|
||||
return nil, fmt.Errorf("unknown configuration scope %q (available: %s)", input.Scope, joinStrings(choices))
|
||||
@@ -132,7 +132,7 @@ func selectConfigScopes[C any](suite ConfigSuite[C], input configInput[C], opera
|
||||
|
||||
if input.Scope == "all" {
|
||||
for _, scopeName := range scopes {
|
||||
scope, _ := findConfigScope(suite.Scopes, scopeName)
|
||||
scope := prepared.byName[scopeName]
|
||||
if !containsOperation(scope.Operations, operation) {
|
||||
return nil, unsupportedConfigOperation(scope, operation)
|
||||
}
|
||||
@@ -156,10 +156,10 @@ func selectConfigScopes[C any](suite ConfigSuite[C], input configInput[C], opera
|
||||
return scopes, nil
|
||||
}
|
||||
|
||||
func contextsForScopes[C any](suite ConfigSuite[C], input configInput[C], scopeNames []string) map[string]C {
|
||||
func contextsForScopes[C any](prepared *preparedConfigSuite[C], input configInput[C], scopeNames []string) map[string]C {
|
||||
base := *new(C)
|
||||
if suite.NewContext != nil {
|
||||
base = suite.NewContext()
|
||||
if prepared.suite.NewContext != nil {
|
||||
base = prepared.suite.NewContext()
|
||||
}
|
||||
contexts := make(map[string]C, len(scopeNames))
|
||||
for _, name := range scopeNames {
|
||||
@@ -176,13 +176,9 @@ func contextsForScopes[C any](suite ConfigSuite[C], input configInput[C], scopeN
|
||||
return contexts
|
||||
}
|
||||
|
||||
func findConfigScope[C any](scopes []ConfigScope[C], name string) (ConfigScope[C], bool) {
|
||||
for _, scope := range scopes {
|
||||
if scope.Name == name {
|
||||
return scope, true
|
||||
}
|
||||
}
|
||||
return ConfigScope[C]{}, false
|
||||
func findConfigScope[C any](prepared *preparedConfigSuite[C], name string) (ConfigScope[C], bool) {
|
||||
scope, ok := prepared.byName[name]
|
||||
return scope, ok
|
||||
}
|
||||
|
||||
func containsOperation(operations []ConfigOperation, requested ConfigOperation) bool {
|
||||
|
||||
Reference in New Issue
Block a user