Skip to content

Commit

Permalink
feat: let user pass CliArgs as go template to scripts and commands
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomacf committed Jan 7, 2022
1 parent 385f8ce commit 951482f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
15 changes: 15 additions & 0 deletions cmd/act/actfile/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package actfile

import (
"regexp"
"strings"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -232,6 +233,20 @@ func (cmd *Cmd) UnmarshalYAML(value *yaml.Node) error {
cmd.Args = append(cmd.Args, actArgs...)
}

// We let user pass command args together with script.
if cmdObj.Script != "" {
// Trim whitespaces from template strings
var re = regexp.MustCompile(`{{ *([^ ]+) *}}`)
scriptLine := re.ReplaceAllString(cmdObj.Script, "{{$1}}")

args := strings.Split(scriptLine, " ")
scriptPath := args[0]
scriptArgs := args[1:]

cmd.Script = scriptPath
cmd.Args = append(cmd.Args, scriptArgs...)
}

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/act/run/act.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ func (ctx *ActRunCtx) MergeVars() map[string]string {
}
}

// Add the set of all command line arguments as a single var
vars["CliArgs"] = strings.Join(ctx.Args, " ")

return vars
}

Expand Down
10 changes: 8 additions & 2 deletions cmd/act/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,18 @@ func CmdExec(cmd *actfile.Cmd, ctx *ActRunCtx, wg *sync.WaitGroup) {
if cmd.Script != "" {
cmdLine = utils.CompileTemplate(cmd.Script, vars)

shArgs = append([]string{cmdLine}, ctx.Args...)
var cmdArgs []string

for _, arg := range cmd.Args {
compiledArg := utils.CompileTemplate(arg, vars)
cmdArgs = append(cmdArgs, compiledArg)
}

shArgs = append([]string{cmdLine}, cmdArgs...)
} else {
cmdLine = utils.CompileTemplate(cmd.Cmd, vars)

shArgs = []string{"-c", cmdLine, "--"}
shArgs = append(shArgs, ctx.Args...)
}

// Set shell to use in the right precedence order.
Expand Down

0 comments on commit 951482f

Please # to comment.