-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Read env variables from file #5
Open
ilya-korotya
wants to merge
9
commits into
antonmashko:master
Choose a base branch
from
ilya-korotya:feature/read-env-variables-from-file
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
509d5cb
read env variables from file
korotia-illia 56ab5bd
add unit tests
ilya-korotya 0d12cff
fix logic; add unit tests
ilya-korotya 7b1e337
fix trim function
ilya-korotya 089e8d2
fix logic env parse
ilya-korotya 24933c1
parse comment block
ilya-korotya 407d5b9
refactor code
ilya-korotya df1d81e
added few test fixtures
antonmashko 435d55c
refactor code
ilya-korotya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package envconf | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type ErrIncorrectValue struct { | ||
Value string | ||
Symbol string | ||
} | ||
|
||
func (e ErrIncorrectValue) Error() string { | ||
return fmt.Sprintf("%s contains invalid symbol %s\n", e.Value, e.Symbol) | ||
} | ||
|
||
var ( | ||
ErrInvalidPair = errors.New("invalid pair for env variable") | ||
ErrQuotesInQuotes = errors.New("quotes in quotes") | ||
ErrNotPairQuotes = errors.New("not pair quotes") | ||
) | ||
|
||
type EnvConfig struct { | ||
Envs map[string]string | ||
IsTrimSpace bool | ||
Quote string | ||
} | ||
|
||
func NewEnvConf() *EnvConfig { | ||
return &EnvConfig{Envs: map[string]string{}} | ||
} | ||
|
||
func (e *EnvConfig) TrimSpace(t bool) *EnvConfig { | ||
e.IsTrimSpace = t | ||
return e | ||
} | ||
|
||
func (e *EnvConfig) Parse(data io.Reader) error { | ||
lines, err := e.readLine(data) | ||
if err != nil { | ||
return err | ||
} | ||
err = e.parseEnvLines(lines) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (e *EnvConfig) parseEnvLines(lines []string) error { | ||
for _, line := range lines { | ||
if strings.Contains(line, "#") { | ||
line = strings.TrimSpace(line) | ||
line = trimComment(line) | ||
// continue if block comment | ||
if len(line) == 0 { | ||
continue | ||
} | ||
} | ||
line = strings.TrimLeft(line, "export ") | ||
i := strings.Index(line, "=") | ||
if i == -1 { | ||
return ErrInvalidPair | ||
} | ||
key := line[:i] | ||
value := line[i+1:] | ||
// trim space symbols near key and value | ||
if e.IsTrimSpace { | ||
key = strings.TrimSpace(key) | ||
value = strings.TrimSpace(value) | ||
} else if strings.HasSuffix(key, " ") || strings.HasPrefix(value, " ") { | ||
return ErrIncorrectValue{Value: key, Symbol: " "} | ||
} | ||
var err error | ||
key, err = e.trimQuotes(key) | ||
if err != nil { | ||
return err | ||
} | ||
if strings.Contains(key, " ") { | ||
return ErrInvalidPair | ||
} | ||
value, err = e.trimQuotes(value) | ||
if err != nil { | ||
return err | ||
} | ||
value = e.trimCharacterEscaping(value) | ||
e.Envs[key] = value | ||
} | ||
return nil | ||
} | ||
|
||
func (e *EnvConfig) readLine(data io.Reader) ([]string, error) { | ||
b := bufio.NewReader(data) | ||
lines := []string{} | ||
for { | ||
l, err := b.ReadString('\n') | ||
if err != nil { | ||
if err != io.EOF { | ||
return nil, err | ||
} | ||
if len(l) == 0 { | ||
return lines, nil | ||
} | ||
} | ||
lines = append(lines, l) | ||
} | ||
} | ||
|
||
func (e *EnvConfig) Set() error { | ||
for k, v := range e.Envs { | ||
if err := os.Setenv(k, v); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (e *EnvConfig) trimQuotes(s string) (string, error) { | ||
s = strings.TrimSpace(s) | ||
// string contains only space symbols | ||
if len(s) == 0 { | ||
return "", nil | ||
} | ||
// valide pair quotes | ||
if s[0] == '"' || s[0] == '\'' { | ||
e.Quote = string(s[0]) | ||
if s[0] != s[len(s)-1] { | ||
return "", ErrNotPairQuotes | ||
} | ||
s = s[1 : len(s)-1] | ||
} | ||
return s, nil | ||
} | ||
|
||
func (e *EnvConfig) trimCharacterEscaping(s string) string { | ||
if e.Quote == "'" { | ||
// remove escaping with ' | ||
return escaping(s, "'") | ||
} else if e.Quote == "\"" { | ||
// remove escaping with " | ||
return escaping(s, "\"") | ||
} | ||
return s | ||
} | ||
|
||
func escaping(s, q string) string { | ||
for i, j := 0, 1; j < len(s); i, j = i+1, j+1 { | ||
if string(s[i]) == `\` && string(s[j]) == q { | ||
s = s[:i] + s[i+1:] | ||
} | ||
} | ||
return s | ||
} | ||
|
||
func trimComment(s string) string { | ||
segmentsBetweenHashes := strings.Split(s, "#") | ||
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 | ||
} | ||
} | ||
if len(segmentsToKeep) == 0 || quotesAreOpen { | ||
segmentsToKeep = append(segmentsToKeep, segment) | ||
} | ||
} | ||
return strings.Join(segmentsToKeep, "#") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package envconf | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestEnvConfigStatusOK(t *testing.T) { | ||
type testCase struct { | ||
filePath string | ||
expectedValues map[string]string | ||
isTrimSpace bool | ||
} | ||
testcases := []testCase{ | ||
{ | ||
filePath: "./fixtures/basic.env", | ||
expectedValues: map[string]string{ | ||
"OPTION_A": "foo", | ||
"OPTION_B": "foo_bar", | ||
"OPTION_C": "3.14", | ||
"OPTION_D": "42", | ||
"_OPTION_E": "foo", | ||
"____OPTION_F": "bar", | ||
"OPTION_G": "1", | ||
"OPTION_H": "2", | ||
}, | ||
}, | ||
{ | ||
filePath: "./fixtures/exported.env", | ||
expectedValues: map[string]string{ | ||
"OPTION_A": "2", | ||
"OPTION_B": `\n`, | ||
}, | ||
}, | ||
{ | ||
filePath: "./fixtures/space.env", | ||
expectedValues: map[string]string{ | ||
"OPTION_A": "1", | ||
"OPTION_B": "2", | ||
"OPTION_C": "3", | ||
"OPTION_D": "4", | ||
"OPTION_E": "5", | ||
"OPTION_F": "", | ||
"OPTION_G": "", | ||
}, | ||
isTrimSpace: true, | ||
}, | ||
{ | ||
filePath: "./fixtures/quoted.env", | ||
expectedValues: map[string]string{ | ||
"OPTION_A": "1", | ||
"OPTION_B": "2", | ||
"OPTION_C": "", | ||
"OPTION_D": `\n`, | ||
"OPTION_E": "1", | ||
"OPTION_F": "2", | ||
"OPTION_G": "", | ||
"OPTION_H": `\n`, | ||
"OPTION_I": `foo 'bar'`, | ||
"OPTION_J": `foo"bar"`, | ||
"OPTION_K": `"foo`, | ||
"OPTION_L": `foo "bar"`, | ||
"OPTION_M": `foo \bar\`, | ||
"OPTION_N": `\\foo`, | ||
"OPTION_O": `foo \"bar\"`, | ||
"OPTION_P": "`foo bar`", | ||
}, | ||
}, | ||
{ | ||
filePath: "./fixtures/comment.env", | ||
expectedValues: map[string]string{ | ||
"OPTION_A": "1", | ||
"OPTION_B": "2", | ||
}, | ||
}, | ||
} | ||
for _, tc := range testcases { | ||
envsFile, err := os.Open(tc.filePath) | ||
if err != nil { | ||
t.Error("cannot open file via path:", tc.filePath) | ||
} | ||
envf := NewEnvConf().TrimSpace(tc.isTrimSpace) | ||
if err := envf.Parse(envsFile); err != nil { | ||
t.Errorf("cannot read file %s error: %s", tc.filePath, err) | ||
} | ||
// comparing result envs with expected | ||
if len(tc.expectedValues) != len(envf.Envs) { | ||
t.Errorf("he expected value is not equal to the value from the file: in the test case=%v != in the env file=%v", len(tc.expectedValues), len(envf.Envs)) | ||
} | ||
for k, v := range envf.Envs { | ||
values, ok := tc.expectedValues[k] | ||
if !ok { | ||
t.Error("expected values not contains key:", k) | ||
} | ||
if values != v { | ||
t.Errorf("the expected value is not equal to the value from the file: in the test case=%v !=in the file=%v. Test key: %s", values, v, k) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestEnvConfigParseIncorrectFileStatusError(t *testing.T) { | ||
type testCase struct { | ||
filePath string | ||
isTrimSpace bool | ||
} | ||
testcases := []testCase{ | ||
{ | ||
filePath: "./fixtures/space.env", | ||
}, | ||
} | ||
for _, tc := range testcases { | ||
envsFile, err := os.Open(tc.filePath) | ||
if err != nil { | ||
t.Error("cannot open file via path:", tc.filePath) | ||
} | ||
envf := NewEnvConf().TrimSpace(tc.isTrimSpace) | ||
err = envf.Parse(envsFile) | ||
err, ok := err.(ErrIncorrectValue) | ||
if !ok { | ||
t.Errorf("incorrect error type: %T", err) | ||
} | ||
} | ||
} | ||
|
||
func TestEnvConfigParseIncorrectKeyStatusError(t *testing.T) { | ||
type testCase struct { | ||
filePath string | ||
} | ||
testcases := []testCase{ | ||
{ | ||
filePath: "./fixtures/nagative_export.env", | ||
}, | ||
} | ||
for _, tc := range testcases { | ||
envsFile, err := os.Open(tc.filePath) | ||
if err != nil { | ||
t.Error("cannot open file via path:", tc.filePath) | ||
} | ||
envf := NewEnvConf() | ||
err = envf.Parse(envsFile) | ||
if err != ErrInvalidPair { | ||
t.Errorf("incorrect error type: %T", err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
OPTION_A=foo | ||
OPTION_B=foo_bar | ||
OPTION_C=3.14 | ||
OPTION_D=42 | ||
_OPTION_E=foo | ||
____OPTION_F=bar | ||
OPTION_G=1 | ||
OPTION_H=2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Block comment | ||
OPTION_A=1 | ||
OPTION_B=2 # InlineComment | ||
# OPTION_C=3 | ||
# OPTION_D=4 | ||
# OPTION_E=5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export OPTION_A=2 | ||
export OPTION_B='\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exp_ OPTION_A=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
OPTION_A='1' | ||
OPTION_B='2' | ||
OPTION_C='' | ||
OPTION_D='\n' | ||
OPTION_E="1" | ||
OPTION_F="2" | ||
OPTION_G="" | ||
OPTION_H="\n" | ||
OPTION_I="foo 'bar'" | ||
OPTION_J="foo\"bar\"" | ||
OPTION_K="\"foo" | ||
OPTION_L='foo "bar"' | ||
OPTION_M='foo \bar\' | ||
OPTION_N='\\foo' | ||
OPTION_O='foo \"bar\"' | ||
OPTION_P='`foo bar`' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
OPTION_A=1 | ||
OPTION_B=2 | ||
OPTION_C= 3 | ||
OPTION_D =4 | ||
OPTION_E = 5 | ||
OPTION_F = | ||
OPTION_G= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this changes is not related to this commit