Skip to content

Commit

Permalink
feat: adds support for providing an environment file
Browse files Browse the repository at this point in the history
Closed #6
  • Loading branch information
aubm committed Oct 19, 2016
1 parent 17933f0 commit 397a582
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ The collection is a JSON file generated from the Postman UI. You can get more in

Use the `-collection=/path/to/collection.json` option to provide the collection to Postmanerator.

### Provide an environment file

The environment file is a JSON file generated from the Postman UI. You can get more information about Postman environments from the [official documentation](https://www.getpostman.com/docs/environments).

Use the `-environment=/path/to/environment.json` option to provide the environment to Postmanerator.

### Provide a theme

By default, Postmanerator will use its `default` theme, but you can change it by using the `-theme=theme_name` option.
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
type Configuration struct {
Out io.Writer
CollectionFile string
EnvironmentFile string
UsedTheme string
OutputFile string
Watch bool
Expand All @@ -41,6 +42,7 @@ var (
func init() {
config.Out = os.Stdout
flag.StringVar(&config.CollectionFile, "collection", "", "the postman exported collection JSON file")
flag.StringVar(&config.EnvironmentFile, "environment", "", "the postman exported environment JSON file")
flag.StringVar(&config.UsedTheme, "theme", "default", "the theme to use")
flag.StringVar(&config.OutputFile, "output", "", "the output file, default is stdout")
flag.BoolVar(&config.Watch, "watch", false, "automatically regenerate the output when the theme changes")
Expand Down Expand Up @@ -115,6 +117,14 @@ func defaultCommand(config Configuration) error {
return errors.New("You must provide a collection using the -collection flag")
}

var env map[string]string
if config.EnvironmentFile != "" {
env, err = postman.EnvironmentFromFile(config.EnvironmentFile)
if err != nil {
return fmt.Errorf("Failed to open environment file: %v", err)
}
}

if config.OutputFile != "" {
out, err = os.Create(config.OutputFile)
if err != nil {
Expand All @@ -126,6 +136,7 @@ func defaultCommand(config Configuration) error {
col, err := postman.CollectionFromFile(config.CollectionFile, postman.CollectionOptions{
IgnoredRequestHeaders: postman.HeadersList(config.IgnoredRequestHeaders.values),
IgnoredResponseHeaders: postman.HeadersList(config.IgnoredResponseHeaders.values),
EnvironmentVariables: env,
})
if err != nil {
return fmt.Errorf("Failed to parse collection file: %v", err)
Expand Down
9 changes: 9 additions & 0 deletions postman/collection.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package postman

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
)
Expand Down Expand Up @@ -29,6 +31,12 @@ func CollectionFromFile(file string, options CollectionOptions) (*Collection, er
return nil, err
}

if options.EnvironmentVariables != nil {
for k, v := range options.EnvironmentVariables {
buf = bytes.Replace(buf, []byte(fmt.Sprintf("{{%v}}", k)), []byte(v), -1)
}
}

err = json.Unmarshal(buf, col)
if err != nil {
return nil, err
Expand Down Expand Up @@ -70,6 +78,7 @@ func CollectionFromFile(file string, options CollectionOptions) (*Collection, er
type CollectionOptions struct {
IgnoredRequestHeaders HeadersList
IgnoredResponseHeaders HeadersList
EnvironmentVariables map[string]string
}

type HeadersList []string
Expand Down
35 changes: 35 additions & 0 deletions postman/environment.go
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
}
47 changes: 47 additions & 0 deletions postman/environment_test.go
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()
}

0 comments on commit 397a582

Please sign in to comment.