envexpander

Expand the unordered referenced variable in the environment variables.

MIT License

Stars
0

envexpander

Expand the unordered referenced variable in the environment variables efficiently.

How does it work?

Expansion is a process of replacing a variable reference with its value.

For example:

A=B
B=${A}
A=B
B=B

This tool can expand the variable reference ${A} to B in the above example.

It is NOT MATTER for this tool whether the definition is ordered. So the following example is also expandable.

A=${B}
B=A123
A=A123
B=A123

For complex examples, it also handles well.

[email protected]
LISTEN_HOST=http://0.0.0.0:${PORT}
DATABASE_URI=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?sslmode=disable
PORT=8080
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_HOST=internal.postgres.host
POSTGRES_PORT=1145
POSTGRES_DB=testdb
[email protected]
LISTEN_HOST=http://0.0.0.0:8080
DATABASE_URI=postgres://postgres:[email protected]:1145/testdb?sslmode=disable
PORT=8080
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_HOST=internal.postgres.host
POSTGRES_PORT=1145
POSTGRES_DB=testdb

For undefined variables, we leave it as it is.

A=${CCC}
A=${CCC}

Benchmark

goos: darwin
goarch: arm64
pkg: github.com/pan93412/envexpander/v3
BenchmarkEnvExpand_V3/simple-16                  2946846               407.5 ns/op
BenchmarkEnvExpand_V3/simple-2-16                2984750               398.1 ns/op
BenchmarkEnvExpand_V3/complex-16                  863078              1422 ns/op
BenchmarkEnvExpand_V3/very-complex-16             482428              2456 ns/op
BenchmarkEnvExpand_V3/unknown-reference-16               4462561               271.5 ns/op
BenchmarkEnvExpand_V3/unknown-reference-2-16             2693028               446.9 ns/op
BenchmarkEnvExpand_V3/realcase-1-16                       404270              2791 ns/op
BenchmarkEnvExpand_V3/simple-3-16                        1742944               698.6 ns/op
BenchmarkEnvExpand_V3/selfreference-16                   4691149               253.2 ns/op
BenchmarkRefV3/simple-16                                57546478                20.14 ns/op
BenchmarkRefV3/simple-2-16                              56987947                20.82 ns/op
BenchmarkRefV3/simple-3-16                              29251585                40.99 ns/op
BenchmarkRefV3/simple-4-16                              28097368                41.83 ns/op
BenchmarkRefV3/invalid-syntax-1-16                      227145477                5.252 ns/op
BenchmarkRefV3/invalid-syntax-2-16                      180704725                6.537 ns/op
BenchmarkRefV3/invalid-syntax-3-16                      47285127                23.72 ns/op
BenchmarkRefV3/invalid-syntax-4-16                      53172633                22.21 ns/op
BenchmarkRefV3/invalid-syntax-5-16                      45558663                26.01 ns/op
BenchmarkRefV3/escape-16                                46021900                23.86 ns/op

Test Coverage

ok      github.com/pan93412/envexpander/v3      0.183s  coverage: 98.6% of statements

Used in

Examples

  • expander.go: Receive the dotenv-like format from stdin and output the expanded result to stdout.

License

MIT License. See LICENSE file.

Package Rankings
Top 6.48% on Proxy.golang.org