Compare commits

...

5 Commits
main ... main

Author SHA1 Message Date
3a7a190201 fix: if a line contains multiple # characters, there will be issues w… (#238)
* fix: if a line contains multiple # characters, there will be issues when traversing from back to front

* fix: typo
2024-12-16 15:14:19 +11:00
a7f6c4c583 Re-add global env variable substitution (#227)
Co-authored-by: Stanislau Arsoba <sarsoba@klika-tech.com>
2024-11-01 09:24:06 +11:00
32e64fa834 chore: fix typo (#231) 2024-05-20 16:43:01 +10:00
7765d9d198 Fix panic because of wrong function (#223) 2024-01-13 13:49:45 +11:00
383d64cb7e Update cmd.go (#221)
Renuewed Update
2024-01-06 18:10:56 +11:00
5 changed files with 35 additions and 16 deletions

View File

@ -4,7 +4,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"strings" "strings"
"github.com/joho/godotenv" "github.com/joho/godotenv"

View File

@ -1,4 +1,7 @@
# Full line comment # Full line comment
qux=thud # fred # other
thud=fred#qux # other
fred=qux#baz # other # more
foo=bar # baz foo=bar # baz
bar=foo#baz bar=foo#baz
baz="foo"#bar baz="foo"#bar

View File

@ -3,3 +3,4 @@ OPTION_B=${OPTION_A}
OPTION_C=$OPTION_B OPTION_C=$OPTION_B
OPTION_D=${OPTION_A}${OPTION_B} OPTION_D=${OPTION_A}${OPTION_B}
OPTION_E=${OPTION_NOT_DEFINED} OPTION_E=${OPTION_NOT_DEFINED}
OPTION_F=${GLOBAL_OPTION}

View File

@ -207,15 +207,21 @@ func TestLoadQuotedEnv(t *testing.T) {
func TestSubstitutions(t *testing.T) { func TestSubstitutions(t *testing.T) {
envFileName := "fixtures/substitutions.env" envFileName := "fixtures/substitutions.env"
presets := map[string]string{
"GLOBAL_OPTION": "global",
}
expectedValues := map[string]string{ expectedValues := map[string]string{
"OPTION_A": "1", "OPTION_A": "1",
"OPTION_B": "1", "OPTION_B": "1",
"OPTION_C": "1", "OPTION_C": "1",
"OPTION_D": "11", "OPTION_D": "11",
"OPTION_E": "", "OPTION_E": "",
"OPTION_F": "global",
} }
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets) loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets)
} }
func TestExpanding(t *testing.T) { func TestExpanding(t *testing.T) {
@ -471,9 +477,12 @@ func TestErrorParsing(t *testing.T) {
func TestComments(t *testing.T) { func TestComments(t *testing.T) {
envFileName := "fixtures/comments.env" envFileName := "fixtures/comments.env"
expectedValues := map[string]string{ expectedValues := map[string]string{
"foo": "bar", "qux": "thud",
"bar": "foo#baz", "thud": "fred#qux",
"baz": "foo", "fred": "qux#baz",
"foo": "bar",
"bar": "foo#baz",
"baz": "foo",
} }
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets) loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
@ -582,42 +591,42 @@ func TestWhitespace(t *testing.T) {
}{ }{
"Leading whitespace": { "Leading whitespace": {
input: " A=a\n", input: " A=a\n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Leading tab": { "Leading tab": {
input: "\tA=a\n", input: "\tA=a\n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Leading mixed whitespace": { "Leading mixed whitespace": {
input: " \t \t\n\t \t A=a\n", input: " \t \t\n\t \t A=a\n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Leading whitespace before export": { "Leading whitespace before export": {
input: " \t\t export A=a\n", input: " \t\t export A=a\n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Trailing whitespace": { "Trailing whitespace": {
input: "A=a \t \t\n", input: "A=a \t \t\n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Trailing whitespace with export": { "Trailing whitespace with export": {
input: "export A=a\t \t \n", input: "export A=a\t \t \n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"No EOL": { "No EOL": {
input: "A=a", input: "A=a",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Trailing whitespace with no EOL": { "Trailing whitespace with no EOL": {
input: "A=a ", input: "A=a ",
key: "A", key: "A",
value: "a", value: "a",
}, },
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"os"
"regexp" "regexp"
"strings" "strings"
"unicode" "unicode"
@ -142,9 +143,9 @@ func extractVarValue(src []byte, vars map[string]string) (value string, rest []b
} }
// Work backwards to check if the line ends in whitespace then // Work backwards to check if the line ends in whitespace then
// a comment (ie asdasd # some comment) // a comment, ie: foo=bar # baz # other
for i := endOfVar - 1; i >= 0; i-- { for i := 0; i < endOfVar; i++ {
if line[i] == charComment && i > 0 { if line[i] == charComment && i < endOfVar {
if isSpace(line[i-1]) { if isSpace(line[i-1]) {
endOfVar = i endOfVar = i
break break
@ -264,6 +265,12 @@ func expandVariables(v string, m map[string]string) string {
if submatch[1] == "\\" || submatch[2] == "(" { if submatch[1] == "\\" || submatch[2] == "(" {
return submatch[0][1:] return submatch[0][1:]
} else if submatch[4] != "" { } else if submatch[4] != "" {
if val, ok := m[submatch[4]]; ok {
return val
}
if val, ok := os.LookupEnv(submatch[4]); ok {
return val
}
return m[submatch[4]] return m[submatch[4]]
} }
return s return s