Skip to content

Commit

Permalink
Merge pull request #138 from 1vn/master
Browse files Browse the repository at this point in the history
Parse CLI envs for Network
  • Loading branch information
VojtechVitek authored Apr 24, 2018
2 parents 096e543 + a9fce3a commit 861527f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cmd/sup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ func parseArgs(conf *sup.Supfile) (*sup.Network, []*sup.Command, error) {
return nil, nil, ErrUnknownNetwork
}

// Parse CLI --env flag env vars, override values defined in Network env.
for _, env := range envVars {
if len(env) == 0 {
continue
}
i := strings.Index(env, "=")
if i < 0 {
if len(env) > 0 {
network.Env.Set(env, "")
}
continue
}
network.Env.Set(env[:i], env[i+1:])
}

hosts, err := network.ParseInventory()
if err != nil {
return nil, nil, err
Expand Down
10 changes: 10 additions & 0 deletions supfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ func (e EnvVar) AsExport() string {
// but maintains order, enabling late variables to reference early variables.
type EnvList []*EnvVar

func (e EnvList) Slice() []string {
envs := make([]string, len(e))
for i, env := range e {
envs[i] = env.String()
}
return envs
}

func (e *EnvList) UnmarshalYAML(unmarshal func(interface{}) error) error {
items := []yaml.MapItem{}

Expand Down Expand Up @@ -327,6 +335,8 @@ func (n Network) ParseInventory() ([]string, error) {
}

cmd := exec.Command("/bin/sh", "-c", n.Inventory)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, n.Env.Slice()...)
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
Expand Down

0 comments on commit 861527f

Please sign in to comment.