Skip to content

Commit

Permalink
initally copy-over from kuddle-buddy
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverisaac committed Jan 31, 2022
1 parent 1c742db commit 480390b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/oliverisaac/koi

go 1.17

require github.com/pkg/errors v0.9.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
30 changes: 30 additions & 0 deletions koi/tweakArgs.go
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 42 additions & 0 deletions koi/tweakArgs_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
38 changes: 36 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 480390b

Please sign in to comment.