-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd): add 'namespace' command for easy namespace switching (#26)
- Loading branch information
1 parent
9d7c653
commit aa1e15c
Showing
3 changed files
with
51 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -18,7 +18,6 @@ policies: | |
- style | ||
- test | ||
scopes: | ||
- "*" | ||
- cmd | ||
- pkg | ||
- build |
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,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) | ||
} | ||
}, | ||
} |
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