forked from oliverisaac/koi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (51 loc) · 1.41 KB
/
main.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 (
"fmt"
"log"
"os"
"os/exec"
"github.com/oliverisaac/koi/koi"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var version string = "unset-version"
var commit string = "unset-commit"
func main() {
var exitCode int
var err error
logLevel := defaultEnv("KOI_LOG_LEVEL", "INFO")
if ll, err := logrus.ParseLevel(logLevel); err == nil {
logrus.SetLevel(ll)
}
exe := defaultEnv("KOI_KUBECTL_EXE", "kubectl")
koiArgs := koi.ApplyTweaksToArgs(os.Args[1:])
requestedKoiCommand := koi.GetCommand(koiArgs)
if requestedKoiCommand == "events" {
exitCode, err = koi.EventsCommand(exe, koiArgs)
} else if requestedKoiCommand == "version" {
fmt.Printf("Koi version: %s (%s)\n", version, commit)
exitCode, err = runAttachedCommand(exe, koiArgs...)
} else {
exitCode, err = runAttachedCommand(exe, koiArgs...)
}
if err != nil {
log.Fatal(errors.Wrap(err, "Failed to run the command"))
os.Exit(1)
}
os.Exit(exitCode)
}
func defaultEnv(env string, defaultVal string) string {
if val, ok := os.LookupEnv(env); ok {
return val
}
return defaultVal
}
func runAttachedCommand(command string, args ...string) (exitCode int, runErr error) {
cmd := exec.Command(command, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
runErr = cmd.Run()
exitCode = cmd.ProcessState.ExitCode()
return exitCode, errors.Wrapf(runErr, "Failed to run command %q %q", command, args)
}