[config] support for old export-style config files
This commit is contained in:
@@ -13,11 +13,16 @@ func TestExportfileRoundtrip(t *testing.T) {
|
|||||||
Expect(env.Map()).To(Equal(pairs("Baz", "BOFF", "Foo", "Bar", "HI", "ho")))
|
Expect(env.Map()).To(Equal(pairs("Baz", "BOFF", "Foo", "Bar", "HI", "ho")))
|
||||||
Expect(env.String()).To(Equal("Baz='BOFF'\nFoo='Bar'\nHI='ho'"))
|
Expect(env.String()).To(Equal("Baz='BOFF'\nFoo='Bar'\nHI='ho'"))
|
||||||
|
|
||||||
env, err = NewFromString("\n export HI='h\\\no\\'\n")
|
env, err = NewFromString("\n export HI='h\\\no\\' \n")
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(env.Map()).To(Equal(pairs("HI", "h\\\no\\")))
|
Expect(env.Map()).To(Equal(pairs("HI", "h\\\no\\")))
|
||||||
Expect(env.String()).To(Equal("HI='h\\\no\\'"))
|
Expect(env.String()).To(Equal("HI='h\\\no\\'"))
|
||||||
|
|
||||||
|
env, err = NewFromString("\n export HI=ho\n")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(env.Map()).To(Equal(pairs("HI", "ho")))
|
||||||
|
Expect(env.String()).To(Equal("HI='ho'"))
|
||||||
|
|
||||||
env, err = NewFromString("HI='ho'\nFOO=''\\''\nBAR='\\'''\\'''")
|
env, err = NewFromString("HI='ho'\nFOO=''\\''\nBAR='\\'''\\'''")
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(env.Map()).To(Equal(pairs("HI", "ho", "FOO", "'\nBAR=''")))
|
Expect(env.Map()).To(Equal(pairs("HI", "ho", "FOO", "'\nBAR=''")))
|
||||||
@@ -31,10 +36,8 @@ func TestExportfileRoundtrip(t *testing.T) {
|
|||||||
|
|
||||||
func TestExportfileErrors(t *testing.T) {
|
func TestExportfileErrors(t *testing.T) {
|
||||||
RegisterTestingT(t)
|
RegisterTestingT(t)
|
||||||
_, err := NewFromString("FOO=bar") //values must be quoted
|
|
||||||
Expect(err).To(HaveOccurred())
|
|
||||||
|
|
||||||
_, err = NewFromString("FOO='bar\\''") //single quotes are not escaped this way
|
_, err := NewFromString("FOO='bar\\''") //single quotes are not escaped this way
|
||||||
Expect(err).To(HaveOccurred())
|
Expect(err).To(HaveOccurred())
|
||||||
|
|
||||||
_, err = NewFromString("F\nOO='bar'") //keys cannot have embedded newlines
|
_, err = NewFromString("F\nOO='bar'") //keys cannot have embedded newlines
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseEnv(name string, filename string) (*Env, error) {
|
func parseEnv(name string, filename string) (*Env, error) {
|
||||||
@@ -24,7 +25,7 @@ func parseEnv(name string, filename string) (*Env, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseEnvFromReader(name string, filename string, reader io.Reader) (*Env, error) {
|
func parseEnvFromReader(name string, filename string, reader io.Reader) (*Env, error) {
|
||||||
//The file format looks like: `KEY='VALUE'`. Eait pair is terminated with a newline.
|
//The file format looks like: `KEY='VALUE'`. Each pair is terminated with a newline.
|
||||||
//All characters are valid for VALUE without escaping but one: the single quote.
|
//All characters are valid for VALUE without escaping but one: the single quote.
|
||||||
//A single quote literal is represented by ending the quoting
|
//A single quote literal is represented by ending the quoting
|
||||||
//emitting a \' and resuming the quoting. So for VALUE to be "don't care":
|
//emitting a \' and resuming the quoting. So for VALUE to be "don't care":
|
||||||
@@ -40,6 +41,7 @@ func parseEnvFromReader(name string, filename string, reader io.Reader) (*Env, e
|
|||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
var state = StateKey
|
var state = StateKey
|
||||||
var quoted = false
|
var quoted = false
|
||||||
|
var exportStyle = false
|
||||||
var escaped = false
|
var escaped = false
|
||||||
var key = ""
|
var key = ""
|
||||||
for char, _, err := buffered.ReadRune(); err == nil; char, _, err = buffered.ReadRune() {
|
for char, _, err := buffered.ReadRune(); err == nil; char, _, err = buffered.ReadRune() {
|
||||||
@@ -48,6 +50,7 @@ func parseEnvFromReader(name string, filename string, reader io.Reader) (*Env, e
|
|||||||
switch char {
|
switch char {
|
||||||
case ' ':
|
case ' ':
|
||||||
if buffer.String() == "export" {
|
if buffer.String() == "export" {
|
||||||
|
exportStyle = true
|
||||||
buffer.Truncate(0) //so we can read exportfiles as well as envfiles
|
buffer.Truncate(0) //so we can read exportfiles as well as envfiles
|
||||||
} else if buffer.Len() == 0 {
|
} else if buffer.Len() == 0 {
|
||||||
continue //leading spaced are allowed but not encouraged
|
continue //leading spaced are allowed but not encouraged
|
||||||
@@ -79,11 +82,16 @@ func parseEnvFromReader(name string, filename string, reader io.Reader) (*Env, e
|
|||||||
buffer.WriteRune(char)
|
buffer.WriteRune(char)
|
||||||
} else {
|
} else {
|
||||||
state = StateKey
|
state = StateKey
|
||||||
env.Set(key, buffer.String())
|
value := buffer.String()
|
||||||
|
if exportStyle {
|
||||||
|
value = strings.TrimSpace(value)
|
||||||
|
}
|
||||||
|
env.Set(key, value)
|
||||||
buffer.Truncate(0)
|
buffer.Truncate(0)
|
||||||
key = ""
|
key = ""
|
||||||
escaped = false
|
escaped = false
|
||||||
quoted = false
|
quoted = false
|
||||||
|
exportStyle = false
|
||||||
}
|
}
|
||||||
case '\\':
|
case '\\':
|
||||||
if quoted {
|
if quoted {
|
||||||
@@ -93,8 +101,8 @@ func parseEnvFromReader(name string, filename string, reader io.Reader) (*Env, e
|
|||||||
escaped = true
|
escaped = true
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if !quoted {
|
if !quoted && !exportStyle {
|
||||||
return nil, errors.New("unquoted or unbalanced value for " + key)
|
return nil, errors.New("trailing content for value of " + key)
|
||||||
}
|
}
|
||||||
if escaped {
|
if escaped {
|
||||||
return nil, errors.New("invalid escape for " + key)
|
return nil, errors.New("invalid escape for " + key)
|
||||||
|
|||||||
Reference in New Issue
Block a user