mirror of
https://github.com/hashicorp/packer.git
synced 2026-05-28 04:35:38 -04:00
check for shebang after whole script is interpolated
This commit is contained in:
parent
cb110b263b
commit
8e75537601
1 changed files with 23 additions and 13 deletions
|
|
@ -9,6 +9,7 @@ package shell
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
|
@ -207,29 +208,38 @@ func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packe
|
|||
scripts = append(scripts, tf.Name())
|
||||
|
||||
// Write our contents to it
|
||||
writer := bufio.NewWriter(tf)
|
||||
var shebangWritten bool
|
||||
fileWriter := bufio.NewWriter(tf)
|
||||
commandBuffer := &bytes.Buffer{}
|
||||
for _, command := range p.config.Inline {
|
||||
p.config.ctx.Data = generatedData
|
||||
command, err := interpolate.Render(command, &p.config.ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error interpolating Inline: %s", err)
|
||||
}
|
||||
if !shebangWritten {
|
||||
// If the user has defined an inline shebang, use that.
|
||||
// Or If command does not start with a shebang, use the default shebang.
|
||||
// else command already has a shebang, so do not write it.
|
||||
if p.config.inlineShebangDefined || !strings.HasPrefix(command, "#!") {
|
||||
_, _ = writer.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang))
|
||||
}
|
||||
shebangWritten = true
|
||||
}
|
||||
if _, err := writer.WriteString(command + "\n"); err != nil {
|
||||
if _, err := commandBuffer.WriteString(command + "\n"); err != nil {
|
||||
return fmt.Errorf("Error preparing shell script: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := writer.Flush(); err != nil {
|
||||
firstLine, err := commandBuffer.ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
return fmt.Errorf("Error preparing shell script: %s", err)
|
||||
}
|
||||
// If the user has defined an inline shebang, use that.
|
||||
// Or If command does not start with a shebang, use the default shebang.
|
||||
// else command already has a shebang, so do not write it.
|
||||
if p.config.inlineShebangDefined || !strings.HasPrefix(firstLine, "#!") {
|
||||
if _, err := fileWriter.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang)); err != nil {
|
||||
return fmt.Errorf("Error preparing shell script: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Write the collected commands to the file buffer
|
||||
if _, err := fileWriter.Write(commandBuffer.Bytes()); err != nil {
|
||||
return fmt.Errorf("Error preparing shell script: %s", err)
|
||||
}
|
||||
|
||||
if err := fileWriter.Flush(); err != nil {
|
||||
return fmt.Errorf("Error preparing shell script: %s", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue