2018-08-13 23:40:41 +00:00
|
|
|
package mcfg
|
|
|
|
|
|
|
|
import (
|
2019-02-09 19:08:30 +00:00
|
|
|
"context"
|
2018-08-13 23:40:41 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2019-01-25 03:02:04 +00:00
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
2019-01-25 03:02:04 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/merr"
|
2018-08-13 23:40:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SourceEnv is a Source which will parse configuration from the process
|
|
|
|
// environment.
|
|
|
|
//
|
|
|
|
// Possible Env options are generated by joining a Param's Path and Name with
|
|
|
|
// underscores and making all characters uppercase, as well as changing all
|
|
|
|
// dashes to underscores.
|
|
|
|
//
|
2019-01-08 19:21:55 +00:00
|
|
|
// ctx := mctx.New()
|
|
|
|
// ctx = mctx.ChildOf(ctx, "foo")
|
|
|
|
// ctx = mctx.ChildOf(ctx, "bar")
|
|
|
|
// addr := mcfg.String(ctx, "srv-addr", "", "Some address")
|
2018-08-13 23:40:41 +00:00
|
|
|
// // the Env option to fill addr will be "FOO_BAR_SRV_ADDR"
|
|
|
|
//
|
|
|
|
type SourceEnv struct {
|
2019-01-08 19:21:55 +00:00
|
|
|
// In the format key=value. Defaults to os.Environ() if nil.
|
|
|
|
Env []string
|
2018-08-13 23:40:41 +00:00
|
|
|
|
|
|
|
// If set then all expected Env options must be prefixed with this string,
|
|
|
|
// which will be uppercased and have dashes replaced with underscores like
|
|
|
|
// all the other parts of the option names.
|
|
|
|
Prefix string
|
|
|
|
}
|
|
|
|
|
2019-04-04 14:57:37 +00:00
|
|
|
func (env *SourceEnv) expectedName(path []string, name string) string {
|
2018-08-13 23:40:41 +00:00
|
|
|
out := strings.Join(append(path, name), "_")
|
|
|
|
if env.Prefix != "" {
|
|
|
|
out = env.Prefix + "_" + out
|
|
|
|
}
|
|
|
|
out = strings.Replace(out, "-", "_", -1)
|
|
|
|
out = strings.ToUpper(out)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse implements the method for the Source interface
|
2019-04-04 18:21:44 +00:00
|
|
|
func (env *SourceEnv) Parse(ctx context.Context) (context.Context, []ParamValue, error) {
|
2018-08-13 23:40:41 +00:00
|
|
|
kvs := env.Env
|
|
|
|
if kvs == nil {
|
|
|
|
kvs = os.Environ()
|
|
|
|
}
|
|
|
|
|
2019-04-04 18:21:44 +00:00
|
|
|
params := CollectParams(ctx)
|
2018-08-14 01:02:06 +00:00
|
|
|
pM := map[string]Param{}
|
2019-01-08 19:21:55 +00:00
|
|
|
for _, p := range params {
|
2019-02-05 20:18:17 +00:00
|
|
|
name := env.expectedName(mctx.Path(p.Context), p.Name)
|
2018-08-14 01:02:06 +00:00
|
|
|
pM[name] = p
|
2018-08-13 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pvs := make([]ParamValue, 0, len(kvs))
|
|
|
|
for _, kv := range kvs {
|
|
|
|
split := strings.SplitN(kv, "=", 2)
|
|
|
|
if len(split) != 2 {
|
2019-02-27 18:05:51 +00:00
|
|
|
ctx := mctx.Annotate(context.Background(), "kv", kv)
|
2019-04-04 16:52:43 +00:00
|
|
|
return nil, nil, merr.New("malformed environment key/value pair", ctx)
|
2018-08-13 23:40:41 +00:00
|
|
|
}
|
|
|
|
k, v := split[0], split[1]
|
2018-08-14 01:02:06 +00:00
|
|
|
if p, ok := pM[k]; ok {
|
|
|
|
pvs = append(pvs, ParamValue{
|
2019-01-25 22:33:36 +00:00
|
|
|
Name: p.Name,
|
2019-02-05 20:18:17 +00:00
|
|
|
Path: mctx.Path(p.Context),
|
2018-08-14 01:02:06 +00:00
|
|
|
Value: p.fuzzyParse(v),
|
|
|
|
})
|
2018-08-13 23:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 16:52:43 +00:00
|
|
|
return ctx, pvs, nil
|
2018-08-13 23:40:41 +00:00
|
|
|
}
|