forked from Praqma/helmsman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
62 lines (50 loc) · 1.35 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"bytes"
"fmt"
"log"
"os/exec"
"strings"
"syscall"
)
// command type representing all executable commands Helmsman needs
// to execute in order to inspect the environment/ releases/ charts etc.
type command struct {
Cmd string
Args []string
Description string
}
//printDescription prints the description of a command
func (c command) printDescription() {
fmt.Println(c.Description)
}
// printFullCommand prints the executable command.
func (c command) printFullCommand() {
fmt.Println(c.Description, " by running : \"", c.Cmd, " ", c.Args, " \"")
}
// exec executes the executable command and returns the exit code and execution result
func (c command) exec(debug bool, verbose bool) (int, string) {
if debug {
log.Println("INFO: " + c.Description)
}
if verbose {
log.Println("VERBOSE: " + strings.Join(c.Args[1:], " "))
}
cmd := exec.Command(c.Cmd, c.Args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {
logError("ERROR: cmd.Start: " + err.Error())
}
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus(), stderr.String()
}
} else {
logError("ERROR: cmd.Wait: " + err.Error())
}
}
return 0, stdout.String()
}