4
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2025-07-13 13:46:28 +00:00

Update dependencies (#1841)

This commit is contained in:
Wim
2022-06-11 23:07:42 +02:00
committed by GitHub
parent 3819062574
commit 8751fb4bb1
188 changed files with 5608 additions and 1334 deletions

View File

@ -1,10 +0,0 @@
language: go
go:
- 1.x
os:
- linux
- osx
script:
- go test -test.v -coverprofile=coverage.out -covermode=count
after_success:
- bash <(curl -s https://codecov.io/bash)

View File

@ -1,5 +1,16 @@
# Changelog
## [1.3.0] - 2022-05-23
### Added
- Support = within double-quoted strings
- Add support for multiline values
### Changed
- `OverLoad` prefer environment variables over local variables
## [1.2.0] - 2019-08-03
### Added

View File

@ -1,12 +1,11 @@
# gotenv
[![Build Status](https://travis-ci.org/subosito/gotenv.svg?branch=master)](https://travis-ci.org/subosito/gotenv)
[![Build status](https://ci.appveyor.com/api/projects/status/wb2e075xkfl0m0v2/branch/master?svg=true)](https://ci.appveyor.com/project/subosito/gotenv/branch/master)
[![Build Status](https://github.com/subosito/gotenv/workflows/Go%20workflow/badge.svg)](https://github.com/subosito/gotenv/actions)
[![Coverage Status](https://badgen.net/codecov/c/github/subosito/gotenv)](https://codecov.io/gh/subosito/gotenv)
[![Go Report Card](https://goreportcard.com/badge/github.com/subosito/gotenv)](https://goreportcard.com/report/github.com/subosito/gotenv)
[![GoDoc](https://godoc.org/github.com/subosito/gotenv?status.svg)](https://godoc.org/github.com/subosito/gotenv)
Load environment variables dynamically in Go.
Load environment variables from `.env` or `io.Reader` in Go.
## Usage
@ -120,7 +119,7 @@ Just in case you want to parse environment variables from any `io.Reader`, goten
pairs := gotenv.Parse(strings.NewReader("FOO=test\nBAR=$FOO"))
// gotenv.Env{"FOO": "test", "BAR": "test"}
err, pairs = gotenv.StrictParse(strings.NewReader(`FOO="bar"`))
pairs, err := gotenv.StrictParse(strings.NewReader(`FOO="bar"`))
// gotenv.Env{"FOO": "bar"}
```

View File

@ -1,9 +0,0 @@
build: off
clone_folder: c:\gopath\src\github.com\subosito\gotenv
environment:
GOPATH: c:\gopath
stack: go 1.10
before_test:
- go get -t
test_script:
- go test -v -cover -race

View File

@ -16,6 +16,9 @@ const (
// Pattern for detecting valid variable within a value
variablePattern = `(\\)?(\$)(\{?([A-Z0-9_]+)?\}?)`
// Byte order mark character
bom = "\xef\xbb\xbf"
)
// Env holds key/value pair of valid environment variable
@ -84,7 +87,7 @@ func loadenv(override bool, filenames ...string) error {
// parse and set :)
func parset(r io.Reader, override bool) error {
env, err := StrictParse(r)
env, err := strictParse(r, override)
if err != nil {
return err
}
@ -110,7 +113,7 @@ func setenv(key, val string, override bool) {
// It expands the value of a variable from the environment variable but does not set the value to the environment itself.
// This function is skipping any invalid lines and only processing the valid one.
func Parse(r io.Reader) Env {
env, _ := StrictParse(r)
env, _ := strictParse(r, false)
return env
}
@ -118,22 +121,59 @@ func Parse(r io.Reader) Env {
// It expands the value of a variable from the environment variable but does not set the value to the environment itself.
// This function is returning an error if there are any invalid lines.
func StrictParse(r io.Reader) (Env, error) {
return strictParse(r, false)
}
func strictParse(r io.Reader, override bool) (Env, error) {
env := make(Env)
scanner := bufio.NewScanner(r)
i := 1
bom := string([]byte{239, 187, 191})
firstLine := true
for scanner.Scan() {
line := scanner.Text()
line := strings.TrimSpace(scanner.Text())
if i == 1 {
if firstLine {
line = strings.TrimPrefix(line, bom)
firstLine = false
}
i++
if line == "" || line[0] == '#' {
continue
}
err := parseLine(line, env)
quote := ""
idx := strings.Index(line, "=")
if idx == -1 {
idx = strings.Index(line, ":")
}
if idx > 0 && idx < len(line)-1 {
val := strings.TrimSpace(line[idx+1:])
if val[0] == '"' || val[0] == '\'' {
quote = val[:1]
idx = strings.LastIndex(strings.TrimSpace(val[1:]), quote)
if idx >= 0 && val[idx] != '\\' {
quote = ""
}
}
}
for quote != "" && scanner.Scan() {
l := scanner.Text()
line += "\n" + l
idx := strings.LastIndex(l, quote)
if idx > 0 && l[idx-1] == '\\' {
continue
}
if idx >= 0 {
quote = ""
}
}
if quote != "" {
return env, fmt.Errorf("missing quotes")
}
err := parseLine(line, env, override)
if err != nil {
return env, err
}
@ -142,9 +182,14 @@ func StrictParse(r io.Reader) (Env, error) {
return env, nil
}
func parseLine(s string, env Env) error {
rl := regexp.MustCompile(linePattern)
rm := rl.FindStringSubmatch(s)
var (
lineRgx = regexp.MustCompile(linePattern)
unescapeRgx = regexp.MustCompile(`\\([^$])`)
varRgx = regexp.MustCompile(variablePattern)
)
func parseLine(s string, env Env, override bool) error {
rm := lineRgx.FindStringSubmatch(s)
if len(rm) == 0 {
return checkFormat(s, env)
@ -153,35 +198,36 @@ func parseLine(s string, env Env) error {
key := rm[1]
val := rm[2]
// trim whitespace
val = strings.TrimSpace(val)
// determine if string has quote prefix
hdq := strings.HasPrefix(val, `"`)
// determine if string has single quote prefix
hsq := strings.HasPrefix(val, `'`)
// trim whitespace
val = strings.Trim(val, " ")
// remove quotes '' or ""
rq := regexp.MustCompile(`\A(['"])(.*)(['"])\z`)
val = rq.ReplaceAllString(val, "$2")
if l := len(val); (hsq || hdq) && l >= 2 {
val = val[1 : l-1]
}
if hdq {
val = strings.Replace(val, `\n`, "\n", -1)
val = strings.Replace(val, `\r`, "\r", -1)
val = strings.ReplaceAll(val, `\n`, "\n")
val = strings.ReplaceAll(val, `\r`, "\r")
// Unescape all characters except $ so variables can be escaped properly
re := regexp.MustCompile(`\\([^$])`)
val = re.ReplaceAllString(val, "$1")
val = unescapeRgx.ReplaceAllString(val, "$1")
}
rv := regexp.MustCompile(variablePattern)
fv := func(s string) string {
return varReplacement(s, hsq, env)
return varReplacement(s, hsq, env, override)
}
val = rv.ReplaceAllStringFunc(val, fv)
val = parseVal(val, env)
if !hsq {
val = varRgx.ReplaceAllStringFunc(val, fv)
val = parseVal(val, env, hdq, override)
}
env[key] = val
return nil
@ -201,7 +247,9 @@ func parseExport(st string, env Env) error {
return nil
}
func varReplacement(s string, hsq bool, env Env) string {
var varNameRgx = regexp.MustCompile(`(\$)(\{?([A-Z0-9_]+)\}?)`)
func varReplacement(s string, hsq bool, env Env, override bool) string {
if strings.HasPrefix(s, "\\") {
return strings.TrimPrefix(s, "\\")
}
@ -210,9 +258,7 @@ func varReplacement(s string, hsq bool, env Env) string {
return s
}
sn := `(\$)(\{?([A-Z0-9_]+)\}?)`
rn := regexp.MustCompile(sn)
mn := rn.FindStringSubmatch(s)
mn := varNameRgx.FindStringSubmatch(s)
if len(mn) == 0 {
return s
@ -220,6 +266,10 @@ func varReplacement(s string, hsq bool, env Env) string {
v := mn[3]
if replace, ok := os.LookupEnv(v); ok && !override {
return replace
}
replace, ok := env[v]
if !ok {
replace = os.Getenv(v)
@ -242,21 +292,14 @@ func checkFormat(s string, env Env) error {
return fmt.Errorf("line `%s` doesn't match format", s)
}
func parseVal(val string, env Env) string {
if strings.Contains(val, "=") {
if !(val == "\n" || val == "\r") {
kv := strings.Split(val, "\n")
func parseVal(val string, env Env, ignoreNewlines bool, override bool) string {
if strings.Contains(val, "=") && !ignoreNewlines {
kv := strings.Split(val, "\r")
if len(kv) == 1 {
kv = strings.Split(val, "\r")
}
if len(kv) > 1 {
val = kv[0]
for i := 1; i < len(kv); i++ {
parseLine(kv[i], env)
}
if len(kv) > 1 {
val = kv[0]
for _, l := range kv[1:] {
_ = parseLine(l, env, override)
}
}
}