github.com/openshift/installer@v1.4.17/pkg/asset/agent/oc.go (about) 1 package agent 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "os" 8 "os/exec" 9 "strings" 10 ) 11 12 // ExecuteOC will execute an oc command. 13 func ExecuteOC(pullSecret string, command []string) (string, error) { 14 // create registry config 15 ps, err := os.CreateTemp("", "registry-config") 16 if err != nil { 17 return "", err 18 } 19 defer func() { 20 ps.Close() 21 os.Remove(ps.Name()) 22 }() 23 _, err = ps.Write([]byte(pullSecret)) 24 if err != nil { 25 return "", err 26 } 27 // flush the buffer to ensure the file can be read 28 ps.Close() 29 registryConfig := "--registry-config=" + ps.Name() 30 command = append(command, registryConfig) 31 var stdoutBytes, stderrBytes bytes.Buffer 32 cmd := exec.Command(command[0], command[1:]...) // #nosec G204 33 cmd.Stdout = &stdoutBytes 34 cmd.Stderr = &stderrBytes 35 36 err = cmd.Run() 37 if err == nil { 38 return strings.TrimSpace(stdoutBytes.String()), nil 39 } 40 41 var exitErr *exec.ExitError 42 if errors.As(err, &exitErr) { 43 err = fmt.Errorf("command '%s' exited with non-zero exit code %d: %s\n%s", command, exitErr.ExitCode(), stdoutBytes.String(), stderrBytes.String()) 44 } else { 45 err = fmt.Errorf("command '%s' failed: %w", command, err) 46 } 47 return "", err 48 }