-
Notifications
You must be signed in to change notification settings - Fork 1
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
- Change the order in which the environment variables from the .env f… #6
base: main
Are you sure you want to change the base?
Changes from 2 commits
916696a
6955821
ea2e3b1
122a68d
9fa42ee
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 |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
.vscode/settings.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
demo: | ||
LOCAL=demo go run . -f .env.demo env | sort | ||
demo-convert: | ||
LOCAL=demo2 go run . -f .env.demo env | sort | sed -E 's/(.*)/-e \1/' | tr '\n' ' ' | ||
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. missing terminal LF : looks like an editor configuration issue ; maybe add a .editorconfig for that ? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,12 +18,14 @@ const ( | |
CommentRx = `^[\s]*#` | ||
// NameRx is much tighter than Posix, which accepts anything but NUL and '=', | ||
// but laxer than shells, which do not accept dots. Names are assumed to be pre-trimmed. | ||
NameRx = `^[[:alpha:]][-._a-zA-Z0-9]*` | ||
NameRx = `^[[:alpha:]][-._a-zA-Z0-9]*` | ||
EnvKeyReplaceRx = `\$\{[^}]+\}` | ||
) | ||
|
||
var ( | ||
commentRx = regexp.MustCompile(CommentRx) | ||
nameRx = regexp.MustCompile(NameRx) | ||
commentRx = regexp.MustCompile(CommentRx) | ||
nameRx = regexp.MustCompile(NameRx) | ||
envKeyReplaceRx = regexp.MustCompile(EnvKeyReplaceRx) | ||
) | ||
|
||
type env map[string]string | ||
|
@@ -76,6 +78,28 @@ func (e env) Merge(f env) env { | |
return res | ||
} | ||
|
||
// replaces ${ENV_KEY} placeholder in env values | ||
func (e env) ReplaceEnvKeys() env { | ||
res := make(env, len(e)) | ||
for k, v := range e { | ||
res[k] = envKeyReplaceRx.ReplaceAllStringFunc(v, func(part string) string { | ||
plen := len(part) | ||
if plen > 3 { | ||
envKey := part[2 : plen-1] | ||
if envPart, ok := e[envKey]; ok { | ||
return envPart | ||
} else { | ||
log.Printf(`Replacing the variable failed, env key is unknown: "%s"`, part) | ||
} | ||
return part | ||
} | ||
log.Printf(`Replacing the variable failed, env key is empty: "%s"`, part) | ||
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. Probably only in verbose mode ? |
||
return part | ||
}) | ||
} | ||
return res | ||
} | ||
|
||
func readCloser(args []string) (io.ReadCloser, *flag.FlagSet, error) { | ||
if len(args) < 2 { | ||
return nil, nil, errors.New("need at least a command to run") | ||
|
@@ -138,7 +162,9 @@ func main() { | |
}() | ||
|
||
env := envFromReader(rc) | ||
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. As mentioned in the general comment, this reverses the overriding logic, so I think it is better to put it behind a CLI flag. |
||
env = env.Merge(envFromEnv()) | ||
osEnv := envFromEnv() | ||
env = osEnv.Merge(env) | ||
env = env.ReplaceEnvKeys() | ||
toRun := fs.Args() | ||
// Length checked during readCloser(). | ||
name := toRun[0] | ||
|
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.
missing terminal LF