update github.com/compose-spec/compose-go to v1.3.0

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2022-08-01 14:12:33 +02:00
parent 7b25e2cffc
commit 52fd555bdd
145 changed files with 10391 additions and 2854 deletions

View File

@ -17,13 +17,14 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
"github.com/compose-spec/compose-go/template"
)
const doubleQuoteSpecialChars = "\\\n\r\"!$`"
@ -42,7 +43,7 @@ func Parse(r io.Reader) (map[string]string, error) {
// ParseWithLookup reads an env file from io.Reader, returning a map of keys and values.
func ParseWithLookup(r io.Reader, lookupFn LookupFn) (map[string]string, error) {
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
@ -61,7 +62,7 @@ func ParseWithLookup(r io.Reader, lookupFn LookupFn) (map[string]string, error)
// godotenv.Load("fileone", "filetwo")
//
// It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults
func Load(filenames ...string) (err error) {
func Load(filenames ...string) error {
return load(false, filenames...)
}
@ -76,52 +77,55 @@ func Load(filenames ...string) (err error) {
// godotenv.Overload("fileone", "filetwo")
//
// It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefilly set all vars.
func Overload(filenames ...string) (err error) {
func Overload(filenames ...string) error {
return load(true, filenames...)
}
func load(overload bool, filenames ...string) (err error) {
func load(overload bool, filenames ...string) error {
filenames = filenamesOrDefault(filenames)
for _, filename := range filenames {
err = loadFile(filename, overload)
err := loadFile(filename, overload)
if err != nil {
return // return early on a spazout
return err
}
}
return
return nil
}
var startsWithDigitRegex = regexp.MustCompile(`^\s*\d.*`) // Keys starting with numbers are ignored
// ReadWithLookup gets all env vars from the files and/or lookup function and return values as
// a map rather than automatically writing values into env
func ReadWithLookup(lookupFn LookupFn, filenames ...string) (envMap map[string]string, err error) {
func ReadWithLookup(lookupFn LookupFn, filenames ...string) (map[string]string, error) {
filenames = filenamesOrDefault(filenames)
envMap = make(map[string]string)
envMap := make(map[string]string)
for _, filename := range filenames {
individualEnvMap, individualErr := readFile(filename, lookupFn)
if individualErr != nil {
err = individualErr
return // return early on a spazout
return envMap, individualErr
}
for key, value := range individualEnvMap {
if startsWithDigitRegex.MatchString(key) {
continue
}
envMap[key] = value
}
}
return
return envMap, nil
}
// 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) (map[string]string, error) {
return ReadWithLookup(nil, filenames...)
}
// 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) (map[string]string, error) {
return UnmarshalBytes([]byte(str))
}
@ -182,7 +186,7 @@ func Marshal(envMap map[string]string) (string, error) {
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)))
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v))) // nolint // Cannot use %q here
}
}
sort.Strings(lines)
@ -218,10 +222,10 @@ func loadFile(filename string, overload bool) error {
return nil
}
func readFile(filename string, lookupFn LookupFn) (envMap map[string]string, err error) {
func readFile(filename string, lookupFn LookupFn) (map[string]string, error) {
file, err := os.Open(filename)
if err != nil {
return
return nil, err
}
defer file.Close()
@ -230,28 +234,25 @@ func readFile(filename string, lookupFn LookupFn) (envMap map[string]string, err
var exportRegex = regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)
func parseLine(line string, envMap map[string]string) (key string, value string, err error) {
func parseLine(line string, envMap map[string]string) (string, string, error) {
return parseLineWithLookup(line, envMap, nil)
}
func parseLineWithLookup(line string, envMap map[string]string, lookupFn LookupFn) (key string, value string, err error) {
if len(line) == 0 {
err = errors.New("zero length string")
return
func parseLineWithLookup(line string, envMap map[string]string, lookupFn LookupFn) (string, string, error) {
if line == "" {
return "", "", errors.New("zero length string")
}
// ditch the comments (but keep quoted hashes)
if strings.Contains(line, "#") {
if strings.HasPrefix(strings.TrimSpace(line), "#") || strings.Contains(line, " #") {
segmentsBetweenHashes := strings.Split(line, "#")
quotesAreOpen := false
var segmentsToKeep []string
for _, segment := range segmentsBetweenHashes {
if strings.Count(segment, "\"") == 1 || strings.Count(segment, "'") == 1 {
if quotesAreOpen {
quotesAreOpen = false
segmentsToKeep = append(segmentsToKeep, segment)
} else {
quotesAreOpen = true
}
quotesAreOpen = !quotesAreOpen
}
if len(segmentsToKeep) == 0 || quotesAreOpen {
@ -271,14 +272,14 @@ func parseLineWithLookup(line string, envMap map[string]string, lookupFn LookupF
}
if len(splitString) != 2 {
err = errors.New("can't separate key from value")
return
return "", "", errors.New("can't separate key from value")
}
key = exportRegex.ReplaceAllString(splitString[0], "$1")
key := exportRegex.ReplaceAllString(splitString[0], "$1")
// Parse the value
value = parseValue(splitString[1], envMap, lookupFn)
return
value := parseValue(splitString[1], envMap, lookupFn)
return key, value, nil
}
var (
@ -322,42 +323,24 @@ func parseValue(value string, envMap map[string]string, lookupFn LookupFn) strin
}
if singleQuotes == nil {
value = expandVariables(value, envMap, lookupFn)
value, _ = expandVariables(value, envMap, lookupFn)
}
}
return value
}
var expandVarRegex = regexp.MustCompile(`(\\)?(\$)(\()?\{?([A-Z0-9_]+)?\}?`)
func expandVariables(v string, envMap map[string]string, lookupFn LookupFn) string {
return expandVarRegex.ReplaceAllStringFunc(v, func(s string) string {
submatch := expandVarRegex.FindStringSubmatch(s)
if submatch == nil {
return s
func expandVariables(value string, envMap map[string]string, lookupFn LookupFn) (string, error) {
retVal, err := template.Substitute(value, func(k string) (string, bool) {
if v, ok := envMap[k]; ok {
return v, ok
}
if submatch[1] == "\\" || submatch[2] == "(" {
return submatch[0][1:]
} else if submatch[4] != "" {
// first check if we have defined this already earlier
if envMap[submatch[4]] != "" {
return envMap[submatch[4]]
}
if lookupFn == nil {
return ""
}
// if we have not defined it, check the lookup function provided
// by the user
s2, ok := lookupFn(submatch[4])
if ok {
return s2
}
return ""
}
return s
return lookupFn(k)
})
if err != nil {
return value, err
}
return retVal, nil
}
func doubleQuoteEscape(line string) string {
@ -369,7 +352,7 @@ func doubleQuoteEscape(line string) string {
if c == '\r' {
toReplace = `\r`
}
line = strings.Replace(line, string(c), toReplace, -1)
line = strings.ReplaceAll(line, string(c), toReplace)
}
return line
}

View File

@ -37,7 +37,6 @@ func parseBytes(src []byte, out map[string]string, lookupFn LookupFn) error {
}
if inherited {
value, ok := lookupFn(key)
if ok {
out[key] = value
@ -50,9 +49,6 @@ func parseBytes(src []byte, out map[string]string, lookupFn LookupFn) error {
if err != nil {
return err
}
if lookUpValue, ok := lookupFn(key); ok {
value = lookUpValue
}
out[key] = value
cutset = left
@ -85,7 +81,9 @@ func getStatementStart(src []byte) []byte {
}
// locateKeyName locates and parses key name and returns rest of slice
func locateKeyName(src []byte) (key string, cutset []byte, inherited bool, err error) {
func locateKeyName(src []byte) (string, []byte, bool, error) {
var key string
var inherited bool
// trim "export" and space at beginning
src = bytes.TrimLeftFunc(bytes.TrimPrefix(src, []byte(exportPrefix)), isSpace)
@ -105,9 +103,9 @@ loop:
offset = i + 1
inherited = char == '\n'
break loop
case '_':
case '_', '.':
default:
// variable name should match [A-Za-z0-9_]
// variable name should match [A-Za-z0-9_.]
if unicode.IsLetter(rchar) || unicode.IsNumber(rchar) {
continue
}
@ -124,12 +122,12 @@ loop:
// trim whitespace
key = strings.TrimRightFunc(key, unicode.IsSpace)
cutset = bytes.TrimLeftFunc(src[offset:], isSpace)
cutset := bytes.TrimLeftFunc(src[offset:], isSpace)
return key, cutset, inherited, nil
}
// extractVarValue extracts variable value and returns rest of slice
func extractVarValue(src []byte, envMap map[string]string, lookupFn LookupFn) (value string, rest []byte, err error) {
func extractVarValue(src []byte, envMap map[string]string, lookupFn LookupFn) (string, []byte, error) {
quote, isQuoted := hasQuotePrefix(src)
if !isQuoted {
// unquoted value - read until new line
@ -138,13 +136,17 @@ func extractVarValue(src []byte, envMap map[string]string, lookupFn LookupFn) (v
if end < 0 {
value := strings.Split(string(src), "#")[0] // Remove inline comments on unquoted lines
value = strings.TrimRightFunc(value, unicode.IsSpace)
return expandVariables(value, envMap, lookupFn), nil, nil
retVal, err := expandVariables(value, envMap, lookupFn)
return retVal, nil, err
}
value := strings.Split(string(src[0:end]), "#")[0]
value = strings.TrimRightFunc(value, unicode.IsSpace)
rest = src[end:]
return expandVariables(value, envMap, lookupFn), rest, nil
retVal, err := expandVariables(value, envMap, lookupFn)
return retVal, rest, err
}
// lookup quoted string terminator
@ -160,11 +162,16 @@ func extractVarValue(src []byte, envMap map[string]string, lookupFn LookupFn) (v
// trim quotes
trimFunc := isCharFunc(rune(quote))
value = string(bytes.TrimLeftFunc(bytes.TrimRightFunc(src[0:i], trimFunc), trimFunc))
value := string(bytes.TrimLeftFunc(bytes.TrimRightFunc(src[0:i], trimFunc), trimFunc))
if quote == prefixDoubleQuote {
// unescape newlines for double quote (this is compat feature)
// and expand environment variables
value = expandVariables(expandEscapes(value), envMap, lookupFn)
retVal, err := expandVariables(expandEscapes(value), envMap, lookupFn)
if err != nil {
return "", nil, err
}
value = retVal
}
return value, src[i+1:], nil
@ -187,6 +194,8 @@ func expandEscapes(str string) string {
return "\n"
case "r":
return "\r"
case "$":
return "$$"
default:
return match
}
@ -201,14 +210,14 @@ func indexOfNonSpaceChar(src []byte) int {
}
// hasQuotePrefix reports whether charset starts with single or double quote and returns quote character
func hasQuotePrefix(src []byte) (quote byte, isQuoted bool) {
func hasQuotePrefix(src []byte) (byte, bool) {
if len(src) == 0 {
return 0, false
}
switch prefix := src[0]; prefix {
switch quote := src[0]; quote {
case prefixDoubleQuote, prefixSingleQuote:
return prefix, true
return quote, true // isQuoted
default:
return 0, false
}