-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
302 additions
and
201 deletions.
There are no files selected for viewing
This file contains 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 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 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,97 @@ | ||
package configmap | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func (c *Map) SetFromCLIArgs(key string, args ...string) error { | ||
if len(args) == 0 { | ||
c.Delete(key) | ||
return nil | ||
} | ||
|
||
// in case of schemaless configuration, we don't know the type of the setting | ||
// we will save it as a string or array of strings | ||
if len(c.schema) == 0 { | ||
switch len(args) { | ||
case 1: | ||
c.Set(key, args[0]) | ||
default: | ||
c.Set(key, args) | ||
} | ||
return nil | ||
} | ||
fmt.Println(key, args) | ||
|
||
// Find the correct type for the given setting | ||
valueType, ok := c.schema[key] | ||
if !ok { | ||
return fmt.Errorf("key not found: %s", key) | ||
} | ||
|
||
var value any | ||
isArray := false | ||
{ | ||
var conversionError error | ||
switch valueType.String() { | ||
case "uint": | ||
value, conversionError = strconv.Atoi(args[0]) | ||
case "bool": | ||
value, conversionError = strconv.ParseBool(args[0]) | ||
case "string": | ||
value = args[0] | ||
case "[]string": | ||
value = args | ||
isArray = true | ||
default: | ||
return fmt.Errorf("unhandled type: %s", valueType) | ||
} | ||
if conversionError != nil { | ||
return fmt.Errorf("error setting value: %v", conversionError) | ||
} | ||
} | ||
if !isArray && len(args) != 1 { | ||
return fmt.Errorf("error setting value: key is not an array, but multiple values were provided") | ||
} | ||
|
||
return c.Set(key, value) | ||
} | ||
|
||
func (c *Map) InjectEnvVars(env []string, prefix string) []error { | ||
if prefix != "" { | ||
prefix = strings.ToUpper(prefix) + "_" | ||
} | ||
|
||
errs := []error{} | ||
|
||
envKeyToConfigKey := map[string]string{} | ||
for _, k := range c.AllKeys() { | ||
normalizedKey := prefix + strings.ToUpper(k) | ||
normalizedKey = strings.Replace(normalizedKey, ".", "_", -1) | ||
envKeyToConfigKey[normalizedKey] = k | ||
} | ||
|
||
for _, e := range env { | ||
// Extract key and value from env | ||
parts := strings.SplitN(e, "=", 2) | ||
if len(parts) != 2 { | ||
continue | ||
} | ||
envKey := strings.ToUpper(parts[0]) | ||
envValue := parts[1] | ||
|
||
// Check if the configuration has a matching key | ||
key, ok := envKeyToConfigKey[envKey] | ||
if !ok { | ||
continue | ||
} | ||
|
||
// Update the configuration value | ||
if err := c.SetFromCLIArgs(key, envValue); err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
return errs | ||
} |
This file contains 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 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 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 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 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 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 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 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 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.