From d3988669ead0b195a158915d0d80aa926720a75e Mon Sep 17 00:00:00 2001 From: Anurag Sharma Date: Wed, 19 Mar 2025 18:48:59 +0530 Subject: [PATCH] provisioner: refactored the inline script creation --- provisioner/shell/provisioner.go | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index b16e69512..bc0ba997a 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -9,7 +9,6 @@ package shell import ( "bufio" - "bytes" "context" "errors" "fmt" @@ -209,39 +208,30 @@ func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packe // Set the path to the temporary file scripts = append(scripts, tf.Name()) - // Write our contents to it - fileWriter := bufio.NewWriter(tf) - commandBuffer := &bytes.Buffer{} + // Write all inline commands to this buffer + commandBuffer := strings.Builder{} 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 _, err := commandBuffer.WriteString(command + "\n"); err != nil { + if _, err := fmt.Fprintf(&commandBuffer, "%s\n", command); err != nil { return fmt.Errorf("Error preparing shell script: %s", err) } } - 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 { + if p.config.inlineShebangDefined || !strings.HasPrefix(commandBuffer.String(), "#!") { + if _, err := fmt.Fprintf(tf, "#!%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 { + // Write the collected commands to the file + if _, err := tf.WriteString(commandBuffer.String()); err != nil { return fmt.Errorf("Error preparing shell script: %s", err) }