templatefile/pkg/utils/main.go

43 lines
700 B
Go
Raw Permalink Normal View History

package utils
import (
"bytes"
2022-07-01 15:44:07 +02:00
"fmt"
"os/exec"
"github.com/hashicorp/hcl/v2"
)
2022-07-01 15:44:07 +02:00
var DryRun = false
func CheckErr(e error) {
if e != nil {
panic(e)
}
}
func CheckDiags(diag hcl.Diagnostics) {
if diag.HasErrors() {
panic(diag.Error())
}
}
// Execute a system command ...
func RunSystemCommand(name string, arg ...string) ([]byte, []byte, error) {
2022-07-01 15:44:07 +02:00
if DryRun {
stdOut := []byte(fmt.Sprintf("CMD %s\n", name))
stdErr := []byte("STDERR\n")
return stdOut, stdErr, nil
} else {
var stdOut bytes.Buffer
var stdErr bytes.Buffer
cmd := exec.Command(name, arg...)
cmd.Stderr = &stdErr
cmd.Stdout = &stdOut
err := cmd.Run()
return stdOut.Bytes(), stdErr.Bytes(), err
}
}