Compare commits

..
3 Commits
Author SHA1 Message Date
decentral1se 4d0ca41daa fix: coop-cloud -> toolshed 2025-01-03 18:18:50 +01:00
p4u1 01bff82843 change module location 2023-11-30 11:05:09 +01:00
p4u1 e396573785 add basic implementation of modifiers 2023-11-30 10:58:50 +01:00
14 changed files with 201 additions and 243 deletions
+4 -4
View File
@@ -8,13 +8,13 @@ jobs:
strategy:
fail-fast: false
matrix:
go: [ '1.26', '1.25', '1.24', '1.23', '1.22' ]
os: [ ubuntu-latest, macOS-latest, windows-2025-vs2026 ]
go: [ '1.20', '1.19', '1.18', '1.17', '1.16' ]
os: [ ubuntu-latest, macOS-latest, windows-latest ]
name: ${{ matrix.os }} Go ${{ matrix.go }} Tests
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v3
- name: Setup go
uses: actions/setup-go@v6
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- run: go test
+4 -4
View File
@@ -38,11 +38,11 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v6
uses: actions/checkout@v3
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -56,7 +56,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v4
uses: github/codeql-action/autobuild@v2
# ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
@@ -69,4 +69,4 @@ jobs:
# ./location_of_script_within_repo/buildscript.sh
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v4
uses: github/codeql-action/analyze@v2
+2 -2
View File
@@ -11,9 +11,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
uses: actions/checkout@v3
- name: Generate build files
uses: thatisuday/go-cross-build@v1.1.0
uses: thatisuday/go-cross-build@v1.0.2
with:
platforms: 'linux/amd64, linux/ppc64le, darwin/amd64, darwin/arm64, windows/amd64'
package: 'cmd/godotenv'
-2
View File
@@ -1,3 +1 @@
.DS_Store
CLAUDE.md
.claude
+10 -15
View File
@@ -20,19 +20,18 @@ As a library
go get github.com/joho/godotenv
```
as a tool dependency:
go >= 1.24
```shell
go get -tool github.com/joho/godotenv/cmd/godotenv
```
or if you want to use it as a bin command
go >= 1.17
```shell
go install github.com/joho/godotenv/cmd/godotenv@latest
```
go < 1.17
```shell
go get github.com/joho/godotenv/cmd/godotenv
```
## Usage
Add your application configuration to your `.env` file in the root of your project:
@@ -100,7 +99,7 @@ as a final aside, if you don't want godotenv munging your env you can just get a
```go
var myEnv map[string]string
myEnv, err = godotenv.Read()
myEnv, err := godotenv.Read()
s3Bucket := myEnv["S3_BUCKET"]
```
@@ -119,10 +118,6 @@ content := getRemoteFileContent()
myEnv, err := godotenv.Unmarshal(content)
```
### Variable name compatibility
Ruby's dotenv only allows `[A-Za-z0-9_.]` in key names, while Node's dotenv also permits `-`. godotenv has matched the Node charset since v1.4.0, so keys like `MY-VAR` parse cleanly here and in Node but will error under Ruby's dotenv.
### Precedence & Conventions
Existing envs take precedence of envs that are loaded later.
@@ -137,12 +132,12 @@ if "" == env {
env = "development"
}
godotenv.Load() // The Original .env
godotenv.Load(".env." + env)
godotenv.Load(".env." + env + ".local")
if "test" != env {
godotenv.Load(".env.local")
}
godotenv.Load(".env." + env + ".local")
godotenv.Load(".env." + env)
godotenv.Load() // The Original .env
```
If you need to, you can also use `godotenv.Overload()` to defy this convention
+1 -1
View File
@@ -8,7 +8,7 @@ package autoload
And bob's your mother's brother
*/
import "github.com/joho/godotenv"
import "git.coopcloud.tech/toolshed/godotenv"
func init() {
godotenv.Load()
+2 -1
View File
@@ -4,9 +4,10 @@ import (
"flag"
"fmt"
"log"
"strings"
"github.com/joho/godotenv"
"git.coopcloud.tech/toolshed/godotenv"
)
func main() {
-3
View File
@@ -1,7 +1,4 @@
# Full line comment
qux=thud # fred # other
thud=fred#qux # other
fred=qux#baz # other # more
foo=bar # baz
bar=foo#baz
baz="foo"#bar
-3
View File
@@ -1,3 +0,0 @@
OPTION_A=abc
OPTION-B=def
-1
View File
@@ -3,4 +3,3 @@ OPTION_B=${OPTION_A}
OPTION_C=$OPTION_B
OPTION_D=${OPTION_A}${OPTION_B}
OPTION_E=${OPTION_NOT_DEFINED}
OPTION_F=${GLOBAL_OPTION}
+2 -2
View File
@@ -1,3 +1,3 @@
module github.com/joho/godotenv
module git.coopcloud.tech/toolshed/godotenv
go 1.13
go 1.12
+21 -34
View File
@@ -20,17 +20,18 @@ import (
"os"
"os/exec"
"sort"
"strconv"
"strings"
)
const doubleQuoteSpecialChars = "\\\n\r\"!$`"
// Parse reads an env file from io.Reader, returning a map of keys and values.
func Parse(r io.Reader) (map[string]string, error) {
func Parse(r io.Reader) (map[string]string, map[string]map[string]string, error) {
var buf bytes.Buffer
_, err := io.Copy(&buf, r)
if err != nil {
return nil, err
return nil, nil, err
}
return UnmarshalBytes(buf.Bytes())
@@ -84,12 +85,13 @@ func Overload(filenames ...string) (err error) {
// Read all env (with same file loading semantics as Load) but return values as
// a map rather than automatically writing values into env
func Read(filenames ...string) (envMap map[string]string, err error) {
func Read(filenames ...string) (envMap map[string]string, modMap map[string]map[string]string, err error) {
filenames = filenamesOrDefault(filenames)
envMap = make(map[string]string)
modMap = make(map[string]map[string]string)
for _, filename := range filenames {
individualEnvMap, individualErr := readFile(filename)
individualEnvMap, individualModMap, individualErr := readFile(filename)
if individualErr != nil {
err = individualErr
@@ -99,22 +101,27 @@ func Read(filenames ...string) (envMap map[string]string, err error) {
for key, value := range individualEnvMap {
envMap[key] = value
}
for key, value := range individualModMap {
modMap[key] = value
}
}
return
}
// Unmarshal reads an env file from a string, returning a map of keys and values.
func Unmarshal(str string) (envMap map[string]string, err error) {
func Unmarshal(str string) (envMap map[string]string, modifierMap map[string]map[string]string, err error) {
return UnmarshalBytes([]byte(str))
}
// UnmarshalBytes parses env file from byte slice of chars, returning a map of keys and values.
func UnmarshalBytes(src []byte) (map[string]string, error) {
out := make(map[string]string)
err := parseBytes(src, out)
func UnmarshalBytes(src []byte) (map[string]string, map[string]map[string]string, error) {
vars := make(map[string]string)
modifiers := make(map[string]map[string]string)
err := parseBytes(src, vars, modifiers)
return out, err
return vars, modifiers, err
}
// Exec loads env vars from the specified filenames (empty map falls back to default)
@@ -158,33 +165,13 @@ func Write(envMap map[string]string, filename string) error {
return file.Sync()
}
// isInt checks if the string may be serialized as a number value, leading
// "-" symbol is allowed for negative numbers, leading "+" sign is not. The
// length of the value is not limited.
func isInt(s string) bool {
s = strings.TrimPrefix(s, "-")
if len(s) == 0 {
return false
}
for _, r := range s {
if '0' <= r && r <= '9' {
continue
}
return false
}
return true
}
// Marshal outputs the given environment as a dotenv-formatted environment file.
// Each line is in the format: KEY="VALUE" where VALUE is backslash-escaped.
func Marshal(envMap map[string]string) (string, error) {
lines := make([]string, 0, len(envMap))
for k, v := range envMap {
if isInt(v) {
lines = append(lines, fmt.Sprintf(`%s=%s`, k, v))
if d, err := strconv.Atoi(v); err == nil {
lines = append(lines, fmt.Sprintf(`%s=%d`, k, d))
} else {
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v)))
}
@@ -201,7 +188,7 @@ func filenamesOrDefault(filenames []string) []string {
}
func loadFile(filename string, overload bool) error {
envMap, err := readFile(filename)
envMap, _, err := readFile(filename)
if err != nil {
return err
}
@@ -222,7 +209,7 @@ func loadFile(filename string, overload bool) error {
return nil
}
func readFile(filename string) (envMap map[string]string, err error) {
func readFile(filename string) (envMap map[string]string, modMap map[string]map[string]string, err error) {
file, err := os.Open(filename)
if err != nil {
return
@@ -241,7 +228,7 @@ func doubleQuoteEscape(line string) string {
if c == '\r' {
toReplace = `\r`
}
line = strings.ReplaceAll(line, string(c), toReplace)
line = strings.Replace(line, string(c), toReplace, -1)
}
return line
}
+87 -105
View File
@@ -2,7 +2,6 @@ package godotenv
import (
"bytes"
"errors"
"fmt"
"os"
"reflect"
@@ -13,8 +12,7 @@ import (
var noopPresets = make(map[string]string)
func parseAndCompare(t *testing.T, rawEnvLine string, expectedKey string, expectedValue string) {
result, err := Unmarshal(rawEnvLine)
result, _, err := Unmarshal(rawEnvLine)
if err != nil {
t.Errorf("Expected %q to parse as %q: %q, errored %q", rawEnvLine, expectedKey, expectedValue, err)
return
@@ -89,7 +87,7 @@ func TestReadPlainEnv(t *testing.T) {
"OPTION_H": "1 2",
}
envMap, err := Read(envFileName)
envMap, _, err := Read(envFileName)
if err != nil {
t.Error("Error reading file")
}
@@ -106,7 +104,7 @@ func TestReadPlainEnv(t *testing.T) {
}
func TestParse(t *testing.T) {
envMap, err := Parse(bytes.NewReader([]byte("ONE=1\nTWO='2'\nTHREE = \"3\"")))
envMap, _, err := Parse(bytes.NewReader([]byte("ONE=1\nTWO='2'\nTHREE = \"3\"")))
expectedValues := map[string]string{
"ONE": "1",
"TWO": "2",
@@ -185,31 +183,6 @@ func TestLoadEqualsEnv(t *testing.T) {
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
}
func TestLoadHyphenEnv(t *testing.T) {
envFileName := "fixtures/hyphen.env"
expectedValues := map[string]string{
"OPTION_A": "abc",
"OPTION-B": "def",
}
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
}
func TestKeyNameCharsetRejectsDisallowed(t *testing.T) {
// Locks in the variable-name charset to [A-Za-z0-9_.-]. If you widen
// this further, delete the relevant case here on purpose.
disallowed := []string{
"FOO+BAR=baz",
"FOO@BAR=baz",
"FOO/BAR=baz",
}
for _, input := range disallowed {
if _, err := Unmarshal(input); err == nil {
t.Errorf("expected error parsing %q, got nil", input)
}
}
}
func TestLoadQuotedEnv(t *testing.T) {
envFileName := "fixtures/quoted.env"
expectedValues := map[string]string{
@@ -233,21 +206,15 @@ func TestLoadQuotedEnv(t *testing.T) {
func TestSubstitutions(t *testing.T) {
envFileName := "fixtures/substitutions.env"
presets := map[string]string{
"GLOBAL_OPTION": "global",
}
expectedValues := map[string]string{
"OPTION_A": "1",
"OPTION_B": "1",
"OPTION_C": "1",
"OPTION_D": "11",
"OPTION_E": "",
"OPTION_F": "global",
}
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets)
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
}
func TestExpanding(t *testing.T) {
@@ -300,7 +267,7 @@ func TestExpanding(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
env, err := Parse(strings.NewReader(tt.input))
env, _, err := Parse(strings.NewReader(tt.input))
if err != nil {
t.Errorf("Error: %s", err.Error())
}
@@ -318,7 +285,7 @@ func TestVariableStringValueSeparator(t *testing.T) {
want := map[string]string{
"TEST_URLS": "stratum+tcp://stratum.antpool.com:3333\nstratum+tcp://stratum.antpool.com:443",
}
got, err := Parse(strings.NewReader(input))
got, _, err := Parse(strings.NewReader(input))
if err != nil {
t.Error(err)
}
@@ -368,16 +335,13 @@ func TestParsing(t *testing.T) {
// parses escaped double quotes
parseAndCompare(t, `FOO="escaped\"bar"`, "FOO", `escaped"bar`)
// a trailing escaped backslash does not escape the closing quote
parseAndCompare(t, `FOO="bar\\"`, "FOO", `bar\`)
// parses single quotes inside double quotes
parseAndCompare(t, `FOO="'d'"`, "FOO", `'d'`)
// parses yaml style options
parseAndCompare(t, "OPTION_A: 1", "OPTION_A", "1")
//parses yaml values with equal signs
// parses yaml values with equal signs
parseAndCompare(t, "OPTION_A: Foo=bar", "OPTION_A", "Foo=bar")
// parses non-yaml options with colons
@@ -429,7 +393,7 @@ func TestParsing(t *testing.T) {
parseAndCompare(t, `FOO="ba#r"`, "FOO", "ba#r")
parseAndCompare(t, "FOO='ba#r'", "FOO", "ba#r")
//newlines and backslashes should be escaped
// newlines and backslashes should be escaped
parseAndCompare(t, `FOO="bar\n\ b\az"`, "FOO", "bar\n baz")
parseAndCompare(t, `FOO="bar\\\n\ b\az"`, "FOO", "bar\\\n baz")
parseAndCompare(t, `FOO="bar\\r\ b\az"`, "FOO", "bar\\r baz")
@@ -444,7 +408,7 @@ func TestParsing(t *testing.T) {
// it 'throws an error if line format is incorrect' do
// expect{env('lol$wut')}.to raise_error(Dotenv::FormatError)
badlyFormattedLine := "lol$wut"
_, err := Unmarshal(badlyFormattedLine)
_, _, err := Unmarshal(badlyFormattedLine)
if err == nil {
t.Errorf("Expected \"%v\" to return error, but it didn't", badlyFormattedLine)
}
@@ -470,10 +434,6 @@ func TestLinesToIgnore(t *testing.T) {
"Indented comment": {
input: "\t # comment",
},
"Multiple consecutive comments": {
input: "# one\n# two\n# three\nFOO=bar",
want: "FOO=bar",
},
"non-ignored value": {
input: `export OPTION_B='\n'`,
want: `export OPTION_B='\n'`,
@@ -492,7 +452,7 @@ func TestLinesToIgnore(t *testing.T) {
func TestErrorReadDirectory(t *testing.T) {
envFileName := "fixtures/"
envMap, err := Read(envFileName)
envMap, _, err := Read(envFileName)
if err == nil {
t.Errorf("Expected error, got %v", envMap)
@@ -501,7 +461,7 @@ func TestErrorReadDirectory(t *testing.T) {
func TestErrorParsing(t *testing.T) {
envFileName := "fixtures/invalid1.env"
envMap, err := Read(envFileName)
envMap, _, err := Read(envFileName)
if err == nil {
t.Errorf("Expected error, got %v", envMap)
}
@@ -510,57 +470,30 @@ func TestErrorParsing(t *testing.T) {
func TestComments(t *testing.T) {
envFileName := "fixtures/comments.env"
expectedValues := map[string]string{
"qux": "thud",
"thud": "fred#qux",
"fred": "qux#baz",
"foo": "bar",
"bar": "foo#baz",
"baz": "foo",
"foo": "bar",
"bar": "foo#baz",
"baz": "foo",
}
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
}
func TestIsInt(t *testing.T) {
checkAndCompare := func(s string, expected bool) {
if isInt(s) != expected {
t.Fail()
}
}
// invalid values
checkAndCompare("", false)
checkAndCompare("+123", false)
checkAndCompare("+12a3", false)
checkAndCompare("12a3", false)
checkAndCompare("abc", false)
checkAndCompare("12 3", false)
checkAndCompare("-", false)
checkAndCompare(" ", false)
// valid values
checkAndCompare("-123", true)
checkAndCompare("123", true)
checkAndCompare("-922337203685477580868712", true)
checkAndCompare("922337203685477580837281", true)
}
func TestWrite(t *testing.T) {
writeAndCompare := func(env string, expected string) {
envMap, _ := Unmarshal(env)
envMap, _, _ := Unmarshal(env)
actual, _ := Marshal(envMap)
if expected != actual {
t.Errorf("Expected '%v' (%v) to write as '%v', got '%v' instead.", env, envMap, expected, actual)
}
}
//just test some single lines to show the general idea
//TestRoundtrip makes most of the good assertions
// just test some single lines to show the general idea
// TestRoundtrip makes most of the good assertions
//values are always double-quoted
// values are always double-quoted
writeAndCompare(`key=value`, `key="value"`)
//double-quotes are escaped
// double-quotes are escaped
writeAndCompare(`key=va"lu"e`, `key="va\"lu\"e"`)
//but single quotes are left alone
// but single quotes are left alone
writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`)
// newlines, backslashes, and some other special chars are escaped
writeAndCompare(`foo="\n\r\\r!"`, `foo="\n\r\\r\!"`)
@@ -568,18 +501,13 @@ func TestWrite(t *testing.T) {
writeAndCompare("foo=bar\nbaz=buzz", "baz=\"buzz\"\nfoo=\"bar\"")
// integers should not be quoted
writeAndCompare(`key="10"`, `key=10`)
// leading + is not numeric — must be quoted to preserve the sign
writeAndCompare(`key=+123`, `key="+123"`)
// leading zeros must be preserved (not collapsed to a smaller int)
writeAndCompare(`key=007`, `key=007`)
}
func TestRoundtrip(t *testing.T) {
fixtures := []string{"equals.env", "exported.env", "plain.env", "quoted.env"}
for _, fixture := range fixtures {
fixtureFilename := fmt.Sprintf("fixtures/%s", fixture)
env, err := readFile(fixtureFilename)
env, _, err := readFile(fixtureFilename)
if err != nil {
t.Errorf("Expected '%s' to read without error (%v)", fixtureFilename, err)
}
@@ -587,7 +515,7 @@ func TestRoundtrip(t *testing.T) {
if err != nil {
t.Errorf("Expected '%s' to Marshal (%v)", fixtureFilename, err)
}
roundtripped, err := Unmarshal(rep)
roundtripped, _, err := Unmarshal(rep)
if err != nil {
t.Errorf("Expected '%s' to Mashal and Unmarshal (%v)", fixtureFilename, err)
}
@@ -633,7 +561,7 @@ func TestTrailingNewlines(t *testing.T) {
for n, c := range cases {
t.Run(n, func(t *testing.T) {
result, err := Unmarshal(c.input)
result, _, err := Unmarshal(c.input)
if err != nil {
t.Errorf("Input: %q Unexpected error:\t%q", c.input, err)
}
@@ -694,7 +622,7 @@ func TestWhitespace(t *testing.T) {
for n, c := range cases {
t.Run(n, func(t *testing.T) {
result, err := Unmarshal(c.input)
result, _, err := Unmarshal(c.input)
if err != nil {
t.Errorf("Input: %q Unexpected error:\t%q", c.input, err)
}
@@ -705,22 +633,76 @@ func TestWhitespace(t *testing.T) {
}
}
func TestParserErrors(t *testing.T) {
func TestModfiers(t *testing.T) {
cases := map[string]struct {
input string
err error
input string
key string
value string
modifiers map[string]string
}{
"UnterminatedQuote": {
input: "foo=\"bar",
err: ErrUnterminatedQuote,
"No Modifier": {
input: "A=a",
key: "A",
value: "a",
},
"With comment": {
input: "A=a # my comment",
key: "A",
value: "a",
},
"With single modifier": {
input: "A=a # foo=bar",
key: "A",
value: "a",
modifiers: map[string]string{
"foo": "bar",
},
},
"With multiple modifiers": {
input: "A=a # foo=bar length=10",
key: "A",
value: "a",
modifiers: map[string]string{
"foo": "bar",
"length": "10",
},
},
"With quoted var": {
input: "A='a' # foo=bar",
key: "A",
value: "a",
modifiers: map[string]string{
"foo": "bar",
},
},
"With quoted var 2": {
input: "A='a' # foo=bar\nB=b",
key: "A",
value: "a",
modifiers: map[string]string{
"foo": "bar",
},
},
}
for n, c := range cases {
t.Run(n, func(t *testing.T) {
v, err := Unmarshal(c.input)
if !errors.Is(err, c.err) {
t.Errorf("Input: %q Expected:\t %q\nGot:\t %q Val: %v", c.input, c.err, err, v)
values, modifiers, err := Unmarshal(c.input)
if err != nil {
t.Errorf("Input: %q Unexpected error:\t%q", c.input, err)
}
if values[c.key] != c.value {
t.Errorf("Input %q Expected:\t %q/%q\nGot:\t %q", c.input, c.key, c.value, values)
}
if modifiers[c.key] == nil && c.modifiers != nil {
t.Errorf("Input %q Expected modifiers\n Got: none", c.input)
} else {
for k, v := range c.modifiers {
if modifiers[c.key][k] != v {
t.Errorf("Input %q Expected modifier %s=%s\n Got: %s=%s", c.input, k, v, k, modifiers[c.key][k])
}
}
}
})
}
+68 -66
View File
@@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"os"
"regexp"
"strings"
"unicode"
@@ -18,14 +17,8 @@ const (
exportPrefix = "export"
)
var (
ErrZeroLengthString = errors.New("zero length string")
ErrUnexpectedChar = errors.New("unexpected character")
ErrUnterminatedQuote = errors.New("unterminated quoted value")
)
func parseBytes(src []byte, out map[string]string) error {
src = bytes.ReplaceAll(src, []byte("\r\n"), []byte("\n"))
func parseBytes(src []byte, vars map[string]string, modifiers map[string]map[string]string) error {
src = bytes.Replace(src, []byte("\r\n"), []byte("\n"), -1)
cutset := src
for {
cutset = getStatementStart(cutset)
@@ -39,12 +32,13 @@ func parseBytes(src []byte, out map[string]string) error {
return err
}
value, left, err := extractVarValue(left, out)
value, mods, left, err := extractVarValue(left, vars)
if err != nil {
return err
}
out[key] = value
vars[key] = value
modifiers[key] = mods
cutset = left
}
@@ -55,25 +49,23 @@ func parseBytes(src []byte, out map[string]string) error {
//
// It skips any comment line or non-whitespace character.
func getStatementStart(src []byte) []byte {
for {
pos := indexOfNonSpaceChar(src)
if pos == -1 {
return nil
}
src = src[pos:]
if src[0] != charComment {
return src
}
// skip comment section
pos = bytes.IndexFunc(src, isCharFunc('\n'))
if pos == -1 {
return nil
}
src = src[pos:]
pos := indexOfNonSpaceChar(src)
if pos == -1 {
return nil
}
src = src[pos:]
if src[0] != charComment {
return src
}
// skip comment section
pos = bytes.IndexFunc(src, isCharFunc('\n'))
if pos == -1 {
return nil
}
return getStatementStart(src[pos:])
}
// locateKeyName locates and parses key name and returns rest of slice
@@ -104,20 +96,19 @@ loop:
break loop
case '_':
default:
// variable name should match [A-Za-z0-9_.-]
if unicode.IsLetter(rchar) || unicode.IsNumber(rchar) || rchar == '.' || rchar == '-' {
// variable name should match [A-Za-z0-9_.]
if unicode.IsLetter(rchar) || unicode.IsNumber(rchar) || rchar == '.' {
continue
}
return "", nil, fmt.Errorf(
`%w %q in variable name near %q`,
ErrUnexpectedChar,
`unexpected character %q in variable name near %q`,
string(char), string(src))
}
}
if len(src) == 0 {
return "", nil, ErrZeroLengthString
return "", nil, errors.New("zero length string")
}
// trim whitespace
@@ -127,34 +118,34 @@ loop:
}
// extractVarValue extracts variable value and returns rest of slice
func extractVarValue(src []byte, vars map[string]string) (value string, rest []byte, err error) {
func extractVarValue(src []byte, vars map[string]string) (value string, modifiers map[string]string, rest []byte, err error) {
quote, hasPrefix := hasQuotePrefix(src)
if !hasPrefix {
// unquoted value - read until end of line
endOfLine := bytes.IndexFunc(src, isLineEnd)
// unquoted value - read until end of line
endOfLine := bytes.IndexFunc(src, isLineEnd)
// Hit EOF without a trailing newline
if endOfLine == -1 {
endOfLine = len(src)
// Hit EOF without a trailing newline
if endOfLine == -1 {
endOfLine = len(src)
if endOfLine == 0 {
return "", nil, nil
}
if endOfLine == 0 {
return "", nil, nil, nil
}
}
if !hasPrefix {
// Convert line to rune away to do accurate countback of runes
line := []rune(string(src[0:endOfLine]))
// Assume end of line is end of var
endOfVar := len(line)
if endOfVar == 0 {
return "", src[endOfLine:], nil
return "", nil, src[endOfLine:], nil
}
comment := ""
// Work backwards to check if the line ends in whitespace then
// a comment, ie: foo=bar # baz # other
for i := 0; i < endOfVar; i++ {
if line[i] == charComment && i < endOfVar {
// a comment (ie asdasd # some comment)
for i := endOfVar - 1; i >= 0; i-- {
if line[i] == charComment && i > 0 {
comment = string(line[i+1:])
if isSpace(line[i-1]) {
endOfVar = i
break
@@ -164,7 +155,7 @@ func extractVarValue(src []byte, vars map[string]string) (value string, rest []b
trimmed := strings.TrimFunc(string(line[0:endOfVar]), isSpace)
return expandVariables(trimmed, vars), src[endOfLine:], nil
return expandVariables(trimmed, vars), extractModifiers(comment), src[endOfLine:], nil
}
// lookup quoted string terminator
@@ -173,12 +164,8 @@ func extractVarValue(src []byte, vars map[string]string) (value string, rest []b
continue
}
// skip escaped quote symbol; a quote is escaped only when preceded by an odd number of backslashes
backslashes := 0
for j := i - 1; j >= 0 && src[j] == '\\'; j-- {
backslashes++
}
if backslashes%2 == 1 {
// skip escaped quote symbol (\" or \', depends on quote)
if prevChar := src[i-1]; prevChar == '\\' {
continue
}
@@ -191,7 +178,11 @@ func extractVarValue(src []byte, vars map[string]string) (value string, rest []b
value = expandVariables(expandEscapes(value), vars)
}
return value, src[i+1:], nil
var mods map[string]string
if endOfLine > i {
mods = extractModifiers(string(src[i+1 : endOfLine]))
}
return value, mods, src[i+1:], nil
}
// return formatted error if quoted string is not terminated
@@ -200,7 +191,24 @@ func extractVarValue(src []byte, vars map[string]string) (value string, rest []b
valEndIndex = len(src)
}
return "", nil, fmt.Errorf("%w %s", ErrUnterminatedQuote, src[:valEndIndex])
return "", nil, nil, fmt.Errorf("unterminated quoted value %s", src[:valEndIndex])
}
func extractModifiers(comment string) map[string]string {
if comment == "" {
return nil
}
comment = strings.TrimSpace(comment)
kvpairs := strings.Split(comment, " ")
mods := make(map[string]string)
for _, kv := range kvpairs {
kvsplit := strings.Split(kv, "=")
if len(kvsplit) != 2 {
continue
}
mods[kvsplit[0]] = kvsplit[1]
}
return mods
}
func expandEscapes(str string) string {
@@ -225,7 +233,7 @@ func indexOfNonSpaceChar(src []byte) int {
}
// hasQuotePrefix reports whether charset starts with single or double quote and returns quote character
func hasQuotePrefix(src []byte) (prefix byte, isQuoted bool) {
func hasQuotePrefix(src []byte) (prefix byte, isQuored bool) {
if len(src) == 0 {
return 0, false
}
@@ -278,12 +286,6 @@ func expandVariables(v string, m map[string]string) string {
if submatch[1] == "\\" || submatch[2] == "(" {
return submatch[0][1:]
} 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 s