-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initally copy-over from kuddle-buddy
- Loading branch information
1 parent
1c742db
commit 480390b
Showing
5 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |