diff --git a/go.mod b/go.mod index a4e0709..0207867 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/oliverisaac/koi go 1.17 + +require github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index e69de29..7c401c3 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/koi/tweakArgs.go b/koi/tweakArgs.go new file mode 100644 index 0000000..83ecc23 --- /dev/null +++ b/koi/tweakArgs.go @@ -0,0 +1,30 @@ +package koi + +import ( + "strings" +) + +// applyTweaksToArgs modifies the args sent in so they work wtih kubectl +// Goals: +// The -x flag should become --context +func ApplyTweaksToArgs(args []string) []string { + replacedFlags := map[string]bool{} + shortHandReplacements := map[string]string{ + "-x": "--context", + } + + for i, arg := range args { + if arg == "--" { + break + } + for shorthand, longform := range shortHandReplacements { + if strings.HasPrefix(arg, shorthand+"=") || arg == shorthand { + if _, ok := replacedFlags[shorthand]; !ok { + args[i] = strings.Replace(arg, shorthand, longform, 1) + replacedFlags[shorthand] = true + } + } + } + } + return args +} diff --git a/koi/tweakArgs_test.go b/koi/tweakArgs_test.go new file mode 100644 index 0000000..55e6f57 --- /dev/null +++ b/koi/tweakArgs_test.go @@ -0,0 +1,42 @@ +package koi + +import ( + "reflect" + "testing" +) + +func Test_ApplyTweaksToArgs(t *testing.T) { + tests := []struct { + name string + args []string + want []string + }{ + { + name: "-x flag should be changed to --context", + args: []string{"-x"}, + want: []string{"--context"}, + }, + { + name: "-x=bob flag should be changed to --context=bob", + args: []string{"-x=bob"}, + want: []string{"--context=bob"}, + }, + { + name: "-x should only be changed once", + args: []string{"-x", "bob", "-x", "example"}, + want: []string{"--context", "bob", "-x", "example"}, + }, + { + name: "-x should only be changed before a double-dash", + args: []string{"-n", "bob", "--", "bash", "-x", "example"}, + want: []string{"-n", "bob", "--", "bash", "-x", "example"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ApplyTweaksToArgs(tt.args); !reflect.DeepEqual(got, tt.want) { + t.Errorf("applyTweaksToArgs() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/main.go b/main.go index 52a7925..0b613c6 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,43 @@ package main import ( - "fmt" + "log" + "os" + "os/exec" + + "github.com/oliverisaac/koi/koi" + "github.com/pkg/errors" ) func main() { - fmt.Println("hello world") + var exitCode int + var err error + + exe := defaultEnv("KOI_KUBECTL_EXE", "kubectl") + koiArgs := koi.ApplyTweaksToArgs(os.Args[1:]) + + 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) }