-
Notifications
You must be signed in to change notification settings - Fork 51
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
Envopts #33
base: master
Are you sure you want to change the base?
Envopts #33
Changes from all commits
48128aa
0c7d9c3
38b4559
bae8a12
49e74a3
4f002f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,20 +11,43 @@ import ( | |
// NewBackend creates a configuration loader that loads from the environment. | ||
// If the key is not found, this backend tries again by turning any kebabcase key to snakecase and | ||
// lowercase letters to uppercase. | ||
func NewBackend() backend.Backend { | ||
func NewBackend(fns ...opt) backend.Backend { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically I'm afraid this is a backwardly incompatible change, as someone might have assigned the function to a variable of type Also, I don't think it's a good idea in general to have unexported functions that use unexported types (it makes the godoc hard to understand and means you can't declare variables of those types). |
||
return backend.Func("env", func(ctx context.Context, key string) ([]byte, error) { | ||
val, ok := os.LookupEnv(key) | ||
key = strings.Replace(key, "-", "_", -1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a backwardly incompatible change. The original logic would succeed if an environment variable exists with exactly the same name, but it won't any longer (I'm surprised the tests pass actually - we should fix that). |
||
val, ok := os.LookupEnv(opts(key, fns...)) | ||
if ok { | ||
return []byte(val), nil | ||
} | ||
|
||
key = strings.Replace(strings.ToUpper(key), "-", "_", -1) | ||
|
||
val, ok = os.LookupEnv(key) | ||
val, ok = os.LookupEnv(opts(strings.ToUpper(key), fns...)) | ||
if ok { | ||
return []byte(val), nil | ||
} | ||
|
||
return nil, backend.ErrNotFound | ||
}) | ||
} | ||
|
||
// WithPrefix adds preffix for searching env variable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
? |
||
func WithPrefix(preffix string) opt { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: |
||
if !strings.HasSuffix(preffix, "_") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's worth bothering with this. It makes the logic more complex and harder to explain, and it's quite possible someone might want to add a prefix without any intervening underscore. |
||
preffix = preffix + "_" | ||
} | ||
return func(key string) string { | ||
return preffix + key | ||
} | ||
} | ||
|
||
// ToUpper uppercases searching env variable | ||
func ToUpper() opt { | ||
return func(key string) string { | ||
return strings.ToUpper(key) | ||
} | ||
} | ||
|
||
type opt func(key string) string | ||
|
||
func opts(key string, fns ...opt) string { | ||
for i := range fns { | ||
key = fns[i](key) | ||
} | ||
return key | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a leftover from another fork.