-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from carlpett/env-and-ini-format-support
Add support for env and ini files
- Loading branch information
Showing
9 changed files
with
137 additions
and
2 deletions.
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
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
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
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,29 @@ | ||
package dotenv | ||
|
||
import ( | ||
"fmt" | ||
"bytes" | ||
"strings" | ||
) | ||
|
||
func Unmarshal(in []byte, out *map[string]interface{}) error { | ||
if *out == nil { | ||
*out = make(map[string]interface{}) | ||
} | ||
for _, line := range bytes.Split(in, []byte("\n")) { | ||
if len(line) == 0 { | ||
continue | ||
} | ||
if line[0] == '#' { | ||
continue | ||
} else { | ||
pos := bytes.Index(line, []byte("=")) | ||
if pos == -1 { | ||
return fmt.Errorf("invalid dotenv input line: %s", line) | ||
} | ||
(*out)[string(line[:pos])] = strings.Replace(string(line[pos+1:]), "\\n", "\n", -1) | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,22 @@ | ||
package dotenv | ||
|
||
import ( | ||
"testing" | ||
"reflect" | ||
) | ||
|
||
func TestUnmarshal(t *testing.T) { | ||
input := []byte(`# Comment! | ||
password=P@ssw0rd`) | ||
expectedOutput := map[string]interface{}{ | ||
"password":"P@ssw0rd", | ||
} | ||
var data map[string]interface{} | ||
err := Unmarshal(input, &data) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !reflect.DeepEqual(expectedOutput, data) { | ||
t.Errorf("Unexpected output, expected %v, got %v", expectedOutput, data) | ||
} | ||
} |
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,34 @@ | ||
package ini | ||
|
||
import ( | ||
"gopkg.in/ini.v1" | ||
) | ||
|
||
func Unmarshal(in []byte, out *map[string]interface{}) error { | ||
f, err := ini.Load(in) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if *out == nil { | ||
*out = make(map[string]interface{}) | ||
} | ||
|
||
for _, s := range f.Sections() { | ||
var m map[string]interface{} | ||
// The root section key-value pairs should go directly on the root of the | ||
// map, not under a default subkey | ||
if s.Name() == ini.DefaultSection { | ||
m = *out | ||
} else { | ||
m = make(map[string]interface{}) | ||
(*out)[s.Name()] = m | ||
} | ||
|
||
for _, k := range s.Keys() { | ||
m[k.Name()] = k.Value() | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,27 @@ | ||
package ini | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestUnmarshal(t *testing.T) { | ||
input := []byte(`; Comment! | ||
rootKey = foo | ||
[Some section] | ||
example_key = example_value`) | ||
expectedOutput := map[string]interface{}{ | ||
"rootKey": "foo", | ||
"Some section": map[string]interface{}{ | ||
"example_key": "example_value", | ||
}, | ||
} | ||
var data map[string]interface{} | ||
err := Unmarshal(input, &data) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !reflect.DeepEqual(expectedOutput, data) { | ||
t.Errorf("Unexpected output, expected %v, got %v", expectedOutput, data) | ||
} | ||
} |
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
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