Skip to content
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

Add run args and accept barcelona yml vars #23

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ type ReviewAppResponse struct {
ReviewApps []*ReviewApp `json:"review_apps,omitempty"`
}

type RunEnv struct {
Vars map[string]string `yaml:"vars" json:"vars"`
}

type Heritage struct {
Name string `yaml:"name" json:"name"`
ImageName string `yaml:"image_name" json:"image_name"`
Expand All @@ -146,6 +150,7 @@ type Heritage struct {
EnvVars map[string]string `json:"env_vars,omitempty"`
Environment []*EnvironmentPair `yaml:"environment" json:"environment"`
Token string `json:"token,omitempty"`
RunEnv *RunEnv `yaml:"run_env,omitempty" json:"run_env,omitempty"`
}

func (h *Heritage) FillinDefaults() {
Expand Down
36 changes: 35 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"errors"
"strings"
"time"

Expand Down Expand Up @@ -37,20 +39,38 @@ var RunCommand = cli.Command{
Name: "detach, D",
Usage: "Detach mode",
},
cli.StringSliceFlag{
Name: "envvar, E",
Usage: "Environment variable to pass to task",
},
},
Action: func(c *cli.Context) error {
envName := c.String("environment")
heritageName := c.String("heritage-name")
detach := c.Bool("detach")
envVars := c.StringSlice("envvar")
envVarMap := make(map[string]string)
if len(envName) > 0 && len(heritageName) > 0 {
return cli.NewExitError("environment and heritage-name are exclusive", 1)
}
if len(envName) > 0 {
env, err := LoadEnvironment(c.String("environment"))
env, err := LoadEnvironment(envName)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
heritageName = env.Name
for k, v := range env.RunEnv.Vars {
envVarMap[k] = v
}
}
if len(envVars) > 0 {
varmap, err := checkEnvVars(envVars)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
for k, v := range varmap {
envVarMap[k] = v
}
}
if len(c.Args()) == 0 {
return cli.NewExitError("Command is required", 1)
Expand All @@ -59,6 +79,7 @@ var RunCommand = cli.Command{
params := map[string]interface{}{
"interactive": !detach,
"command": command,
"env_vars": envVarMap,
}
memory := c.Int("memory")
if memory > 0 {
Expand Down Expand Up @@ -138,3 +159,16 @@ var RunCommand = cli.Command{
return nil
},
}

func checkEnvVars(envvarSlice []string) (map[string]string, error) {
var result = make(map[string]string)

re := regexp.MustCompile(`^([A-Z_]+)=(.*)$`)
for _, envvar := range envvarSlice {
if !re.Match([]byte(envvar)) {
return nil, errors.New(fmt.Sprintf("Env Variable %s is not valid. Name must have PASCAL_CASE=", envvar))
}
result[re.FindStringSubmatch(envvar)[1]] = re.FindStringSubmatch(envvar)[2]
}
return result, nil
}
34 changes: 34 additions & 0 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"testing"
)

func TestCheckEnvVars(t *testing.T) {
result, err := checkEnvVars([]string {"ABC=def", "GHI=jkl"})

if err != nil {
t.Errorf("Expected there to be no error but got: %s", err)
}

if result["ABC"] != "def" {
t.Errorf("Expected ABC to be set to def but got: %s", result)
}

if result["GHI"] != "jkl" {
t.Errorf("Expected ABC to be set to def but got: %s", result)
}
}

func TestCheckEnvVarsError(t *testing.T) {
result, err := checkEnvVars([]string {"ABCd=def", "GHI=jkl"})

if err == nil {
t.Errorf("Expected to be an error but was nil")
}

if result != nil {
t.Errorf("Expected result to be nil, but was %s", result)
}

}