Skip to content

Commit

Permalink
Merge pull request #6 from spideyz0r/new-features
Browse files Browse the repository at this point in the history
Add New features
  • Loading branch information
spideyz0r authored Jul 19, 2023
2 parents 6431372 + cd594f5 commit 8737494
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 371 deletions.
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,37 @@ $ kubesw set namespace kube-system
$ kubesw get namespace
kube-system
```
## Aliases / shortnames
You can also use the short form for each command:
```
namespace => namespaces ns
context => contexts ctx
list => ls l
get => current g
set => switch s
```
Some examples:
```kubesw get ns
kubesw set ctx "somecontext"
kubesw ls ns
kubesw switch ns "somenamespace"
```

## Auto-completion
The autocompletion script can be generated with the following:
```
kubesw completion bash
```

You can add the following line to your ~/.bashrc:
```
source <(`which kubesw` completion bash)
```

## TODO
- Fix the debug flag inside common
- Read extra/optional configurations like rc files and PS1 from the configuration file (maybe use viper)
- Add support to zsh
- Remove namespace string when listing namespaces
- Improve error checks and messages
- Add --global flag for updating the namespace or context globally
- Move history along with the new shell instance (if possible)
- Add shell autocompletion
- Allow short names, ctx for context, and ns for namespace
- Investigate the use of eval instead of spawning shells
11 changes: 10 additions & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"

common "github.com/spideyz0r/kubesw/pkg/common"

"github.com/spf13/cobra"
Expand All @@ -10,23 +11,29 @@ import (
var (
getCmd = &cobra.Command{
Use: "get",
Aliases: []string{"g", "current"},
Short: "Get current context or namespace",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Please specify a subcommand. Use --help for more details.")
},
}
namespaceGetCmd = &cobra.Command{
Use: "namespace",
Aliases: []string{"ns", "namespaces"},
Short: "get namespace",
Run: func(cmd *cobra.Command, args []string) {
debug, _ := cmd.Flags().GetBool("debug")
common.SetDebug(debug)
fmt.Printf("%s\n", common.GetCurrent("namespace"))

},
}
contextGetCmd = &cobra.Command{
Use: "context",
Aliases: []string{"ctx", "contexts"},
Short: "get a context",
Run: func(cmd *cobra.Command, args []string) {
debug, _ := cmd.Flags().GetBool("debug")
common.SetDebug(debug)
fmt.Printf("%s\n", common.GetCurrent("context"))
},
}
Expand All @@ -35,4 +42,6 @@ var (
func init() {
getCmd.AddCommand(namespaceGetCmd)
getCmd.AddCommand(contextGetCmd)
namespaceGetCmd.Flags().Bool("debug", false, "Enable debug mode")
contextGetCmd.Flags().Bool("debug", false, "Enable debug mode")
}
10 changes: 10 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"

common "github.com/spideyz0r/kubesw/pkg/common"

"github.com/spf13/cobra"
Expand All @@ -10,6 +11,7 @@ import (
var (
listCmd = &cobra.Command{
Use: "list",
Aliases: []string{"l", "ls"},
Short: "List all contexts or namespaces",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Please specify a subcommand. Use --help for more details.")
Expand All @@ -18,15 +20,21 @@ var (
}
namespaceListCmd = &cobra.Command{
Use: "namespace",
Aliases: []string{"ns", "namespaces"},
Short: "list namespace",
Run: func(cmd *cobra.Command, args []string) {
debug, _ := cmd.Flags().GetBool("debug")
common.SetDebug(debug)
common.ListNamespaces()
},
}
contextListCmd = &cobra.Command{
Use: "context",
Aliases: []string{"ctx", "contexts"},
Short: "list a context",
Run: func(cmd *cobra.Command, args []string) {
debug, _ := cmd.Flags().GetBool("debug")
common.SetDebug(debug)
common.ListContexts()
},
}
Expand All @@ -35,4 +43,6 @@ var (
func init() {
listCmd.AddCommand(namespaceListCmd)
listCmd.AddCommand(contextListCmd)
namespaceListCmd.Flags().Bool("debug", false, "Enable debug mode")
contextListCmd.Flags().Bool("debug", false, "Enable debug mode")
}
10 changes: 10 additions & 0 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"

common "github.com/spideyz0r/kubesw/pkg/common"

"github.com/spf13/cobra"
Expand All @@ -11,19 +12,23 @@ var (
debug = false
setCmd = &cobra.Command{
Use: "set",
Aliases: []string{"s", "switch"},
Short: "Set context or namespace",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Please specify a subcommand. Use --help for more details.")
},
}
namespaceSetCmd = &cobra.Command{
Use: "namespace",
Aliases: []string{"ns", "namespaces"},
Short: "set namespace",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("Please specify a single namespace")
return
}
debug, _ := cmd.Flags().GetBool("debug")
common.SetDebug(debug)
new_kube_config_path, kubeconfig_kubesw_dir := common.InitialSetup()
if debug {
fmt.Printf("KUBECONFIG: %s\n", new_kube_config_path)
Expand All @@ -35,12 +40,15 @@ var (
}
contextSetCmd = &cobra.Command{
Use: "context",
Aliases: []string{"ctx", "contexts"},
Short: "set a context",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("Please specify a single context")
return
}
debug, _ := cmd.Flags().GetBool("debug")
common.SetDebug(debug)
new_kube_config_path, kubeconfig_kubesw_dir := common.InitialSetup()
if debug {
fmt.Printf("KUBECONFIG: %s\n", new_kube_config_path)
Expand All @@ -54,4 +62,6 @@ var (
func init() {
setCmd.AddCommand(namespaceSetCmd)
setCmd.AddCommand(contextSetCmd)
namespaceSetCmd.Flags().Bool("debug", false, "Enable debug mode")
contextSetCmd.Flags().Bool("debug", false, "Enable debug mode")
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require (
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
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
Expand Down
Loading

0 comments on commit 8737494

Please sign in to comment.