56 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
package helmexec
 | 
						|
 | 
						|
import (
 | 
						|
	"os"
 | 
						|
	"os/exec"
 | 
						|
	"strings"
 | 
						|
)
 | 
						|
 | 
						|
const (
 | 
						|
	tmpPrefix = "helmfile-"
 | 
						|
	tmpSuffix = "-exec"
 | 
						|
)
 | 
						|
 | 
						|
// Runner interface for shell commands
 | 
						|
type Runner interface {
 | 
						|
	Execute(cmd string, args []string, env map[string]string) ([]byte, error)
 | 
						|
}
 | 
						|
 | 
						|
// ShellRunner implemention for shell commands
 | 
						|
type ShellRunner struct {
 | 
						|
	Dir string
 | 
						|
}
 | 
						|
 | 
						|
// Execute a shell command
 | 
						|
func (shell ShellRunner) Execute(cmd string, args []string, env map[string]string) ([]byte, error) {
 | 
						|
	preparedCmd := exec.Command(cmd, args...)
 | 
						|
	preparedCmd.Dir = shell.Dir
 | 
						|
	preparedCmd.Env = mergeEnv(os.Environ(), env)
 | 
						|
	return preparedCmd.CombinedOutput()
 | 
						|
}
 | 
						|
 | 
						|
func mergeEnv(orig []string, new map[string]string) []string {
 | 
						|
	wanted := env2map(orig)
 | 
						|
	for k, v := range new {
 | 
						|
		wanted[k] = v
 | 
						|
	}
 | 
						|
	return map2env(wanted)
 | 
						|
}
 | 
						|
 | 
						|
func map2env(wanted map[string]string) []string {
 | 
						|
	result := []string{}
 | 
						|
	for k, v := range wanted {
 | 
						|
		result = append(result, k+"="+v)
 | 
						|
	}
 | 
						|
	return result
 | 
						|
}
 | 
						|
 | 
						|
func env2map(env []string) map[string]string {
 | 
						|
	wanted := map[string]string{}
 | 
						|
	for _, cur := range env {
 | 
						|
		pair := strings.SplitN(cur, "=", 2)
 | 
						|
		wanted[pair[0]] = pair[1]
 | 
						|
	}
 | 
						|
	return wanted
 | 
						|
}
 |