-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow boltrouter config to be set from env (#36)
- Loading branch information
Showing
10 changed files
with
343 additions
and
34 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,117 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/project-n-oss/sidekick/boltrouter" | ||
) | ||
|
||
type Config struct { | ||
BoltRouter boltrouter.Config `yaml:"BoltRouter"` | ||
} | ||
|
||
const configPrefix = "SIDEKICK" | ||
|
||
// UnmarshalConfigFromEnv populates config with values from environment variables. The names of the | ||
// environment variables read are formed by joining the config struct's field names with underscores | ||
// and adding a "SIDEKICK" prefix. For example, if you want to set the Redis address, you would use | ||
// SIDEKICK_BOLTROUTER_FAILOVER=true | ||
func UnmarshalConfigFromEnv(ctx context.Context, config *Config) error { | ||
_, err := unmarshalConfig(configPrefix, reflect.ValueOf(config), func(key string) (*string, error) { | ||
v, ok := os.LookupEnv(key) | ||
if !ok { | ||
return nil, nil | ||
} | ||
return &v, nil | ||
}) | ||
return err | ||
} | ||
|
||
func unmarshalConfig(prefix string, v reflect.Value, lookup func(string) (*string, error)) (didUnmarshal bool, err error) { | ||
if v.Kind() == reflect.Ptr && !v.IsNil() { | ||
if env, err := lookup(prefix); err != nil { | ||
return false, err | ||
} else if env != nil { | ||
switch dest := v.Interface().(type) { | ||
case *bool: | ||
switch *env { | ||
case "false": | ||
*dest = false | ||
case "true": | ||
*dest = true | ||
default: | ||
return false, fmt.Errorf("boolean config must be \"true\" or \"false\"") | ||
} | ||
case *int: | ||
n, err := strconv.Atoi(*env) | ||
if err != nil { | ||
return false, fmt.Errorf("invalid value for integer config") | ||
} | ||
*dest = n | ||
case *string: | ||
*dest = *env | ||
case *[]byte: | ||
buf, err := base64.StdEncoding.DecodeString(*env) | ||
if err != nil { | ||
return false, fmt.Errorf("byte slice configs must be base64 encoded") | ||
} | ||
*dest = buf | ||
case *[]int: | ||
parts := strings.Split(*env, ",") | ||
intParts := make([]int, len(parts)) | ||
for i, part := range parts { | ||
intParts[i], err = strconv.Atoi(strings.TrimSpace(part)) | ||
if err != nil { | ||
return false, fmt.Errorf("invalid value for integer config") | ||
} | ||
} | ||
*dest = intParts | ||
case *[]string: | ||
parts := strings.Split(*env, ",") | ||
for i, part := range parts { | ||
parts[i] = strings.TrimSpace(part) | ||
} | ||
*dest = parts | ||
default: | ||
return false, fmt.Errorf("unsupported environment config type %T", v.Elem().Interface()) | ||
} | ||
return true, nil | ||
} | ||
} | ||
|
||
if v.Kind() == reflect.Ptr { | ||
if !v.IsNil() { | ||
return unmarshalConfig(prefix, v.Elem(), lookup) | ||
} | ||
|
||
new := reflect.New(v.Type().Elem()) | ||
didUnmarshal, err := unmarshalConfig(prefix, new, lookup) | ||
if err != nil { | ||
return false, err | ||
} else if didUnmarshal { | ||
v.Set(new) | ||
} | ||
return didUnmarshal, nil | ||
} | ||
|
||
if v.Kind() == reflect.Struct { | ||
t := v.Type() | ||
for i := 0; i < v.NumField(); i++ { | ||
didUnmarshalField, err := unmarshalConfig(prefix+"_"+strings.ToUpper(t.Field(i).Name), v.Field(i).Addr(), lookup) | ||
if err != nil { | ||
return false, err | ||
} | ||
if didUnmarshalField { | ||
didUnmarshal = true | ||
} | ||
} | ||
} | ||
|
||
return didUnmarshal, nil | ||
} |
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,55 @@ | ||
package cmd | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/project-n-oss/sidekick/boltrouter" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUnmarshalConfig(t *testing.T) { | ||
for name, tc := range map[string]struct { | ||
Environment map[string]string | ||
In Config | ||
Expected *Config | ||
}{ | ||
"EnvOnly": { | ||
Environment: map[string]string{ | ||
"TEST_BOLTROUTER_FAILOVER": "true", | ||
}, | ||
Expected: &Config{ | ||
BoltRouter: boltrouter.Config{ | ||
Failover: true, | ||
}, | ||
}, | ||
}, | ||
"Override": { | ||
Environment: map[string]string{ | ||
"TEST_BOLTROUTER_FAILOVER": "false", | ||
}, | ||
In: Config{ | ||
BoltRouter: boltrouter.Config{ | ||
Failover: true, | ||
}, | ||
}, | ||
Expected: &Config{ | ||
BoltRouter: boltrouter.Config{ | ||
Failover: false, | ||
}, | ||
}, | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
config := tc.In | ||
_, err := unmarshalConfig("TEST", reflect.ValueOf(&config), func(key string) (*string, error) { | ||
if v, ok := tc.Environment[key]; ok { | ||
return &v, nil | ||
} | ||
return nil, nil | ||
}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.Expected, &config) | ||
}) | ||
} | ||
} |
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.