Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.demo
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ DOT.TED=dotted
SHARP=sharp after a var # is not a comment
# This is a most likely override
SHELL=invalid
YOUR_NAME=Felix
GREETING=Hello ${YOUR_NAME}!
TEST_HOME_PATH=${HOME}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing terminal LF

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
.vscode/settings.json
2 changes: 2 additions & 0 deletions Makefile
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' ' '
Copy link
Owner

Choose a reason for hiding this comment

The 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 ?

34 changes: 30 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The 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")
Expand Down Expand Up @@ -138,7 +162,9 @@ func main() {
}()

env := envFromReader(rc)
Copy link
Owner

Choose a reason for hiding this comment

The 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]
Expand Down