Skip to content

Commit

Permalink
use cobra (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv authored Sep 24, 2023
1 parent 1863b89 commit a97d4f0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ kxd -l
This command will display the currently set kubeconfig file.

```bash
kxd -lc
kxd -x
```

This command will display the currently set Kubernetes Context.
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/manifoldco/promptui v0.9.0
github.com/spf13/cobra v1.7.0
k8s.io/client-go v0.28.2
)

Expand All @@ -15,6 +16,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObk
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04=
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -33,6 +34,8 @@ github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
Expand All @@ -49,6 +52,9 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
100 changes: 44 additions & 56 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/manifoldco/promptui"
"github.com/manifoldco/promptui/list"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
Expand All @@ -35,66 +36,53 @@ func main() {
log.Fatalf("Error getting user home directory: %v\n", err)
}

if len(os.Args) > 1 {
arg := strings.ToLower(os.Args[1])
switch arg {
case "-v", "--v", "version":
fmt.Println("kxd version:", version)
case "-c", "--c", "context":
err := runContextSwitcher(homeDir)
if err != nil {
log.Fatal(err)
var configFlag, contextFlag, listFlag, listContextFlag, versionFlag bool

rootCmd := &cobra.Command{
Use: "kxd",
Short: "kxd - switch between Kubeconfigs and contexts.",
Run: func(cmd *cobra.Command, args []string) {
if versionFlag {
fmt.Println("kxd version:", version)
} else if configFlag {
err := runConfigSwitcher(homeDir)
if err != nil {
log.Fatal(err)
}
} else if contextFlag {
err := runContextSwitcher(homeDir)
if err != nil {
log.Fatal(err)
}
} else if listFlag {
err := runListConfig(homeDir)
if err != nil {
log.Fatal(err)
}
} else if listContextFlag {
err := runListContext(homeDir)
if err != nil {
log.Fatal(err)
}
} else {
err := runConfigSwitcher(homeDir)
if err != nil {
log.Fatal(err)
}
}
case "-l", "--l", "list":
err := runListConfig(homeDir)
if err != nil {
log.Fatal(err)
}
case "-lc", "--lc", "list-context":
err := runListContext(homeDir)
if err != nil {
log.Fatal(err)
}
case "-h", "--h", "help":
err := displayHelp()
if err != nil {
log.Fatal(err)
}
case "-s", "--s", "switch":
err := runConfigSwitcher(homeDir)
if err != nil {
log.Fatal(err)
}
default:
err := runConfigSwitcher(homeDir)
if err != nil {
log.Fatal(err)
}
}
} else {
err := runConfigSwitcher(homeDir)
if err != nil {
log.Fatal(err)
}
},
}
}

func displayHelp() error {
var helpMessage strings.Builder
options := map[string]string{
" [-s] ": "Switch configs.",
" -c ": "Switch contexts.",
" -l ": "List current config.",
" -lc ": "List current context.",
" -h ": "Help. Displays this message.",
" -v ": "Displays version.",
}
helpMessage.WriteString("Usage: kxd [OPERATION]\n")
for option, description := range options {
helpMessage.WriteString(fmt.Sprintf(" %s: %s\n", option, description))
rootCmd.PersistentFlags().BoolVarP(&configFlag, "switch", "s", false, "Switch configs")
rootCmd.PersistentFlags().BoolVarP(&contextFlag, "context", "c", false, "Switch contexts")
rootCmd.PersistentFlags().BoolVarP(&listFlag, "list", "l", false, "List current config")
rootCmd.PersistentFlags().BoolVarP(&listContextFlag, "list-context", "x", false, "List current context")
rootCmd.PersistentFlags().BoolVarP(&versionFlag, "version", "v", false, "Displays version")

if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println(helpMessage.String())
return nil
}

func runListConfig(homeDir string) error {
Expand Down
22 changes: 11 additions & 11 deletions scripts/_kxd
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#!/usr/bin/env bash

run_context() {
_kxd_prompt context
_kxd_prompt --context
return
}

run_list_context() {
_kxd_prompt list-context
_kxd_prompt --list-context
return
}

run_list_config() {
_kxd_prompt list
_kxd_prompt --list
return
}

display_help() {
_kxd_prompt help
_kxd_prompt --help
return
}

display_version() {
_kxd_prompt version
_kxd_prompt --version
return
}

Expand All @@ -40,22 +40,22 @@ run_main() {
arg=$(echo "$1" | tr '[:upper:]' '[:lower:]')

case "$arg" in
-c|--c|context)
-c|--context)
run_context
;;
-l|--l|list)
-l|--list)
run_list_config
;;
-lc|--lc|list-context)
-x|--list-context)
run_list_context
;;
-h|--h|help)
-h|--help)
display_help
;;
-s|--s|switch)
-s|--switch)
run_main
;;
-v|--v|version)
-v|--version)
display_version
;;
*)
Expand Down

0 comments on commit a97d4f0

Please sign in to comment.