Skip to content

Commit

Permalink
Fix broken indirect run args
Browse files Browse the repository at this point in the history
THis fixes the issue of broken indirect run args when there were two or more types of values in the object being passed as args.

Ref https://sweetops.slack.com/archives/CFFQ9GFB5/p1587009393226000
  • Loading branch information
mumoshu committed Apr 16, 2020
1 parent 9753895 commit a713c89
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/issues/cant-convert-go-str-to-bool/conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
order:
- foo
- bar
50 changes: 50 additions & 0 deletions examples/issues/cant-convert-go-str-to-bool/example.variant
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
job "deploy switch" {
option "dry-run" {
type = bool
}

option "item" {
type = string
}

option "tenant" {
type = string
}

exec {
command = "bash"
args = ["-c", "echo deploy switch tenant=${opt.tenant} item=${opt.item}"]
}
}

job "example" {
config "file" {
source file {
path = "${context.sourcedir}/conf.yaml"
}
}

option "dry-run" {
type = bool
default = false
}

parameter "tenant" {
type = string
default = "mytenant"
}

depends_on "deploy switch" {
items = conf.file.order
args = {
dry-run = opt.dry-run
item = item
tenant = param.tenant
}
}

exec {
command = "bash"
args = ["-c", "echo Done."]
}
}
22 changes: 22 additions & 0 deletions examples/issues/cant-convert-go-str-to-bool/example_test.variant
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
test "example" {
case "ok" {
out = trimspace(<<EOS
deploy switch tenant=mytenant item=foo
deploy switch tenant=mytenant item=bar
Done.
EOS
)
exitstatus = 0
}

run "example" {
}

assert "out" {
condition = run.res.stdout == case.out
}

assert "exitstatus" {
condition = run.res.exitstatus == case.exitstatus
}
}
5 changes: 5 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ func TestExamples(t *testing.T) {
args: []string{"variant", "run", "example", "echo foo", "echo bar", "-p", "myproj", "-t", "mytenant"},
wd: "./examples/issues/sweetops-CFFQ9GFB5-p1586798062189700",
},
{
subject: "examples/issues/cant-convert-go-str-to-bool",
args: []string{"variant", "test"},
wd: "./examples/issues/cant-convert-go-str-to-bool",
},
{
subject: "examples/exec",
args: []string{"variant", "test"},
Expand Down
11 changes: 9 additions & 2 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1247,12 +1247,19 @@ func (app *App) execMultiRun(l *EventLogger, jobCtx *hcl2.EvalContext, r *Depend
func exprToGoMap(ctx *hcl2.EvalContext, expr hcl2.Expression) (map[string]interface{}, error) {
args := map[string]interface{}{}

ctyArgs := map[string]cty.Value{}
// We need to explicitly specify that the type of values is DynamicPseudoType.
//
// Otherwise, for e.g. map[string]cty.Value{], DecodeExpression computes the lowest common type for all the values.
// That is, {"foo":true,"bar":"BAR"} would produce cty.Map(cty.String) = map[string]string,
// rather than cty.Map(DynamicPseudoType) = map[string]interface{}.
m := cty.MapValEmpty(cty.DynamicPseudoType)

if err := gohcl2.DecodeExpression(expr, ctx, &ctyArgs); err != nil {
if err := gohcl2.DecodeExpression(expr, ctx, &m); err != nil {
return nil, err
}

ctyArgs := m.AsValueMap()

for k, v := range ctyArgs {
var err error

Expand Down

0 comments on commit a713c89

Please sign in to comment.