Skip to content

Commit

Permalink
expose env vars through exec mixin and add docs
Browse files Browse the repository at this point in the history
Signed-off-by: David Justice <[email protected]>
  • Loading branch information
devigned committed Mar 3, 2022
1 parent 7afc273 commit 29136a7
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/content/mixins/exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ exec:
- flag-value2
suffix-arguments: # These arguments are specified after any flags are passed
- suffix-arg1
envs: # Environment variables to be added to the command execution environment
FOO_KEY: foo-value
suppress-output: false # Do not print the command output to the console
ignoreError: # Conditions when execution should continue even if the command fails
all: true # Ignore all errors
Expand Down
30 changes: 19 additions & 11 deletions pkg/exec/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,27 @@ func (a *Actions) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}

var _ builder.HasOrderedArguments = Step{}
var _ builder.ExecutableStep = Step{}
var _ builder.StepWithOutputs = Step{}
var (
_ builder.HasOrderedArguments = Step{}
_ builder.ExecutableStep = Step{}
_ builder.StepWithOutputs = Step{}
_ builder.HasEnvironmentVars = Step{}
)

type Step struct {
Instruction `yaml:"exec"`
}

type Instruction struct {
Description string `yaml:"description"`
Command string `yaml:"command"`
WorkingDir string `yaml:"dir,omitempty"`
Arguments []string `yaml:"arguments,omitempty"`
SuffixArguments []string `yaml:"suffix-arguments,omitempty"`
Flags builder.Flags `yaml:"flags,omitempty"`
Outputs []Output `yaml:"outputs,omitempty"`
SuppressOutput bool `yaml:"suppress-output,omitempty"`
Description string `yaml:"description"`
Command string `yaml:"command"`
WorkingDir string `yaml:"dir,omitempty"`
Arguments []string `yaml:"arguments,omitempty"`
SuffixArguments []string `yaml:"suffix-arguments,omitempty"`
Flags builder.Flags `yaml:"flags,omitempty"`
EnvironmentVars map[string]string `yaml:"envs,omitempty"`
Outputs []Output `yaml:"outputs,omitempty"`
SuppressOutput bool `yaml:"suppress-output,omitempty"`

// Allow the user to ignore some errors
builder.IgnoreErrorHandler `yaml:"ignoreError,omitempty"`
Expand All @@ -126,6 +130,10 @@ func (s Step) GetFlags() builder.Flags {
return s.Flags
}

func (s Step) GetEnvironmentVars() map[string]string {
return s.EnvironmentVars
}

func (s Step) SuppressesOutput() bool {
return s.SuppressOutput
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (

"get.porter.sh/porter/pkg/test"

yaml "get.porter.sh/porter/pkg/yaml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

yaml "get.porter.sh/porter/pkg/yaml"
)

func TestAction_UnmarshalYAML(t *testing.T) {
Expand All @@ -34,6 +35,7 @@ func TestAction_UnmarshalYAML(t *testing.T) {
assert.Equal(t, builder.NewFlag("machine-type", "f1-micro"), step.Flags[0])
assert.Equal(t, builder.NewFlag("project", "porterci"), step.Flags[1])
assert.Equal(t, builder.NewFlag("zone", "us-central1-a"), step.Flags[2])
assert.Equal(t, step.EnvironmentVars, map[string]string{"SECRET": "super-secret"})
require.Len(t, step.Outputs, 1)
assert.Equal(t, Output{Name: "vms", JsonPath: "$[*].id"}, step.Outputs[0])
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/exec/schema/exec.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
"type": "string"
}
},
"envs": {
"description": "Environment variables to add to the env of the command",
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"suffix-arguments": {
"description": "Positional arguments to pass to the command after any flags",
"type": "array",
Expand Down
2 changes: 2 additions & 0 deletions pkg/exec/testdata/install-input.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ install:
project: porterci
zone: us-central1-a
machine-type: f1-micro
envs:
SECRET: super-secret
ignoreError:
output:
contains: ["already exists"]
Expand Down
10 changes: 8 additions & 2 deletions pkg/pkgmgmt/client/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import (
"net/http"
"net/http/httptest"
"path"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"get.porter.sh/porter/pkg"
"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/pkgmgmt"
"get.porter.sh/porter/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFileSystem_InstallFromUrl(t *testing.T) {
Expand Down Expand Up @@ -52,6 +54,10 @@ func TestFileSystem_InstallFromUrl(t *testing.T) {
}

func TestFileSystem_InstallFromFeedUrl(t *testing.T) {
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
t.Skip("skipping because there is no release for helm for darwin/arm64")
}

var testURL = ""
feed, err := ioutil.ReadFile("../feed/testdata/atom.xml")
require.NoError(t, err)
Expand Down
7 changes: 7 additions & 0 deletions pkg/porter/testdata/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,13 @@
"description": "The directory in which to execute the command",
"type": "string"
},
"envs": {
"additionalProperties": {
"type": "string"
},
"description": "Environment variables to add to the env of the command",
"type": "object"
},
"flags": {
"additionalProperties": {
"type": "string"
Expand Down

0 comments on commit 29136a7

Please sign in to comment.