/
oc.go
59 lines (52 loc) · 1.15 KB
/
oc.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
package utils
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
)
type OCInterface interface {
Run(arg ...string) error
RunWithOutputFile(outputFile string, arg ...string) error
}
type OC struct {
Kubeconfig string
executable string
}
func NewOC(kubeconfig string) *OC {
return &OC{Kubeconfig: kubeconfig, executable: "oc"}
}
// Run oc with the given args. Outputs are written to stdout
func (r *OC) Run(arg ...string) error {
return r.run(os.Stdout, arg...)
}
// Run oc with the given args, outputs are written to the given outputFile
func (r *OC) RunWithOutputFile(outputFile string, arg ...string) error {
if outputFile == "" {
return errors.New("output file is empty")
}
outfile, err := os.Create(outputFile)
if err != nil {
return err
}
defer outfile.Close()
return r.run(outfile, arg...)
}
func (r *OC) run(out io.Writer, arg ...string) error {
oc, err := exec.LookPath(r.executable)
if err != nil {
return nil
}
envs := []string{fmt.Sprintf("KUBECONFIG=%s", r.Kubeconfig)}
args := []string{oc}
args = append(args, arg...)
c := &exec.Cmd{
Path: oc,
Args: args,
Stdout: out,
Stderr: os.Stderr,
Env: envs,
}
return c.Run()
}