Skip to content

Commit

Permalink
feat(cmd): add 'namespace' command for easy namespace switching (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
particledecay authored Dec 4, 2020
1 parent 9d7c653 commit aa1e15c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 0 additions & 1 deletion .conform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ policies:
- style
- test
scopes:
- "*"
- cmd
- pkg
- build
50 changes: 50 additions & 0 deletions cmd/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"errors"
"fmt"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

"github.com/particledecay/kconf/pkg/kubeconfig"
)

var namespaceCmd = &cobra.Command{
Use: "namespace",
Short: "Set preferred namespace",
Long: `Set the preferred namespace within the current context`,
Aliases: []string{"ns"},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("You must provide the name of a namespace within the current context")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
namespace := args[0]

config, err := kubeconfig.GetConfig()
if err != nil {
log.Fatal().Msgf("Could not read main config")
}

// fail if we have no current context
if config.CurrentContext == "" {
log.Fatal().Msgf("No current context detected. You must set one first with the `use` command.")
}

err = config.SetNamespace(config.CurrentContext, namespace)
if err != nil {
log.Fatal().Msgf("%v", err)
}
if namespace != "" {
fmt.Printf("Setting preferred namespace '%s'\n", namespace)
}

err = config.Save()
if err != nil {
log.Fatal().Msgf("%v", err)
}
},
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Execute() {
rootCmd.AddCommand(viewCmd)
rootCmd.AddCommand(useCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(namespaceCmd)
rootCmd.AddCommand(completionCmd)

completionCmd.AddCommand(completionBashCmd)
Expand Down

0 comments on commit aa1e15c

Please sign in to comment.