-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds support for providing an environment file
Closed #6
- Loading branch information
Showing
5 changed files
with
108 additions
and
0 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,35 @@ | ||
package postman | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
) | ||
|
||
type environmentExport struct { | ||
Values []struct { | ||
Key string `json:"key"` | ||
Value string `json:"value"` | ||
} `json:"values"` | ||
} | ||
|
||
func EnvironmentFromFile(file string) (map[string]string, error) { | ||
f, err := os.Open(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
envExport := new(environmentExport) | ||
|
||
err = json.NewDecoder(f).Decode(envExport) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
env := map[string]string{} | ||
for _, v := range envExport.Values { | ||
env[v.Key] = v.Value | ||
} | ||
|
||
return env, 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,47 @@ | ||
package postman | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestEnvironmentFromFile(t *testing.T) { | ||
filename := createTmpEnvironmentFile() | ||
|
||
env, err := EnvironmentFromFile(filename) | ||
|
||
expectedEnv := map[string]string{"domain": "localhost:8080"} | ||
if ok := reflect.DeepEqual(env, expectedEnv); ok == false { | ||
t.Errorf("Expected %v, got %v, err is %v", expectedEnv, env, err) | ||
} | ||
} | ||
|
||
func createTmpEnvironmentFile() string { | ||
f, err := ioutil.TempFile(os.TempDir(), "postman_env_file") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
|
||
fmt.Fprint(f, `{ | ||
"id": "316cfffe-80bc-ff35-4a30-46d6085d1973", | ||
"name": "Books API - Local", | ||
"values": [ | ||
{ | ||
"key": "domain", | ||
"value": "localhost:8080", | ||
"type": "text", | ||
"enabled": true | ||
} | ||
], | ||
"timestamp": 1476905519649, | ||
"_postman_variable_scope": "environment", | ||
"_postman_exported_at": "2016-10-19T19:33:45.473Z", | ||
"_postman_exported_using": "Postman/4.7.2" | ||
}`) | ||
|
||
return f.Name() | ||
} |