Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
Implemented reset-cfg command
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Baiguini committed Feb 2, 2020
1 parent 526f65f commit 34c3ae4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ clean-ctx : build ## Remove context list from Kubernetes configuration
rename-ctx : build ## Rename specified context in Kubernetes configuration
$(KONF_PREFIX) konf rename-ctx --kube-config ./examples/config context_a NEW_context_a

reset-cfg-local : build ## Reset local Kubernetes configuration (current shell)
@echo "It's useless to run an 'eval' command from the Makefile as each line is executed in a new shell instance"
@echo "Please manually execute 'eval $(konf reset-cfg local)'"
@echo ""

reset-cfg-global : build ## Reset global Kubernetes configuration
$(KONF_PREFIX) konf reset-cfg global --kube-config ./examples/config

## helpers

Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ make build

`konf rename-ctx <context-to-rename> <new-context-name>` renames the specified context in Kubernetes configuration (per default in `~/.kube/config` Kubernetes configuration if not otherwise specified)

`eval $(konf reset-cfg local)` resets the local (current shell) Kubernetes configuration, un-setting `KUBECONFIG` environment variable

`konf reset-cfg global` resets `currentContext` to N/A in Kubernetes configuration (per default in `~/.kube/config` Kubernetes configuration if not otherwise specified)

`konf completion [bash | zsh]` outputs the auto-completion script for the selected. See [auto-completion](#auto-completion) section below.

`konf help` shows the helper
Expand Down Expand Up @@ -239,8 +243,8 @@ konf
- [x] clean-ctx (remove specified context and relatives from kubernetes configuration)
- [x] rename-ctx (rename specified context in kubernetes configuration)
- [ ] reset-cfg
- [ ] reset-cfg local removing KUBECONFIG environment variable
- [ ] reset-cfg global ? to default ~/.kube/config
- [ ] reset-cfg local removes KUBECONFIG environment variable
- [ ] reset-cfg global resets currentContext to N/A in Kubernetes configuration

---

Expand Down
2 changes: 2 additions & 0 deletions application/konf.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package application

import (
"github.com/bygui86/konf/commands/reset"
"os"
"sort"

Expand Down Expand Up @@ -55,6 +56,7 @@ func addCommands(app *cli.App) {
*set.BuildCommand(),
*clean.BuildCommand(),
*rename.BuildCommand(),
*reset.BuildCommand(),
*completion.BuildCommand(),
}
}
Expand Down
32 changes: 29 additions & 3 deletions commands/reset/action.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
package reset

import (
"fmt"
"github.com/bygui86/konf/commands"
"github.com/bygui86/konf/commons"
"github.com/bygui86/konf/kubeconfig"
"github.com/urfave/cli"

"github.com/bygui86/konf/logger"
)

func reset(ctx *cli.Context) error {
func resetLocal(ctx *cli.Context) error {
logger.Logger.Debug("")
logger.Logger.Debug("🐛 Executing RESET-CFG-LOCAL command")
logger.Logger.Debug("")

logger.Logger.Info(fmt.Sprintf("unset %s", commons.KubeConfigEnvVar))
return nil
}

func resetGlobal(ctx *cli.Context) error {
logger.Logger.Info("")
logger.Logger.Debug("🐛 Executing RESET command")
logger.Logger.Debug("🐛 Executing RESET-CFG-GLOBAL command")
logger.Logger.Debug("")

logger.Logger.Warn("⚠️ Command not yet implemented ☢️")
logger.Logger.Debug("🐛 Get Kubernetes configuration file path")
kubeConfigFilePath := ctx.String(commons.CustomKubeConfigFlagName)
logger.SugaredLogger.Infof("📖 Load Kubernetes configuration from '%s'", kubeConfigFilePath)
kubeConfig := kubeconfig.Load(kubeConfigFilePath)
// INFO: no need to check if kubeConfig is nil, because the inner method called will exit if it does not find the configuration file

logger.SugaredLogger.Debugf("♻️ Reset Kubernetes configuration '%s'", kubeConfigFilePath)
kubeConfig.CurrentContext = ""

valWrErr := commands.ValidateAndWrite(kubeConfig, kubeConfigFilePath)
if valWrErr != nil {
return valWrErr
}

logger.SugaredLogger.Infof("✅ Completed! Kubernetes configuration reset")
logger.Logger.Info("")
return nil
}
34 changes: 30 additions & 4 deletions commands/reset/command.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
package reset

import (
"github.com/bygui86/konf/commons"
"github.com/bygui86/konf/kubeconfig"
"github.com/bygui86/konf/utils"
"github.com/urfave/cli"

"github.com/bygui86/konf/logger"
)

func BuildCommand() *cli.Command {
logger.Logger.Debug("🐛 Create RESET command")
logger.Logger.Debug("🐛 Create RESET-CONFIG command")
home := utils.GetHomeDirOrExit("reset-cfg")
return &cli.Command{
Name: "reset",
Usage: "",
Action: reset,
Name: "reset-cfg",
Usage: "Reset local or global Kubernetes configuration",
Subcommands: cli.Commands{
{
Name: "local",
Usage: "Reset local Kubernetes configuration (current shell)",
ArgsUsage: "<context>",
Action: resetLocal,
},
{
Name: "global",
Usage: "Reset global Kubernetes configuration",
ArgsUsage: "<context>",
Action: resetGlobal,
Flags: []cli.Flag{
cli.StringFlag{
Name: utils.GetUrfaveFlagName(commons.CustomKubeConfigFlagName, commons.CustomKubeConfigFlagShort),
Usage: commons.CustomKubeConfigFlagDescription,
EnvVar: commons.CustomKubeConfigPathEnvVar,
Value: kubeconfig.GetCustomKubeConfigPathDefault(home),
Required: false,
},
},
},
},
}
}

0 comments on commit 34c3ae4

Please sign in to comment.