Skip to content

Commit

Permalink
make presence of register to trigger multiline script
Browse files Browse the repository at this point in the history
this is similar to how it handled a single-line script with export
  • Loading branch information
umputun committed Aug 9, 2023
1 parent 3757e97 commit a14a57b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ func (cmd *Cmd) GetScript() (command string, rdr io.Reader) {
}

elems := strings.Split(cmd.Script, "\n")
// export should be treated as multiline for env vars to be set
if len(elems) > 1 || strings.Contains(cmd.Script, "export") {
// If there are multiple lines, we need to use a script file.
// Export presence should be treated as multiline for env vars to be set. The same thing for register variables.
if len(elems) > 1 || strings.Contains(cmd.Script, "export") || len(cmd.Register) > 0 {
log.Printf("[DEBUG] command %q is multiline, using script file", cmd.Name)
return "", cmd.scriptFile(cmd.Script, cmd.Register)
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/config/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ echo 'Goodbye, World!'`,
"echo setvar GREETING=${GREETING}",
},
},
{
name: "single line with register",
cmd: &Cmd{
Script: `echo 'Hello, World!'
echo 'Goodbye, World!'`,
Register: []string{"foo"},
},
expectedScript: "",
expectedContents: []string{
"#!/bin/sh",
"set -e",
"echo 'Hello, World!'",
"echo 'Goodbye, World!'",
"echo setvar foo=${foo}",
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit a14a57b

Please sign in to comment.