Skip to content

Commit

Permalink
Rename to --kubeconfig, better output on context switch. (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Sep 20, 2021
1 parent 1522655 commit a0ddc94
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
25 changes: 17 additions & 8 deletions cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -109,13 +110,17 @@ func contextSet(args []string) error {
if err != nil {
return err
}
curr := args[0]
_, ok := ctxs.Contexts[curr]
nextCtx := args[0]
_, ok := ctxs.Contexts[nextCtx]
if !ok {
return fmt.Errorf("context %s not found", curr)
return fmt.Errorf("context %s not found", nextCtx)
}
if nextCtx == ctxs.CurrentContext {
fmt.Printf("%s context \"%s\" already active\n", color.GreenString("✔"), color.GreenString(ctxs.CurrentContext))
return nil
}
ctxs.PreviousContext = ctxs.CurrentContext
ctxs.CurrentContext = curr
ctxs.CurrentContext = nextCtx
return writeContexts(ctxs)
}

Expand All @@ -126,7 +131,7 @@ func previous() error {
}
prev := ctxs.PreviousContext
if prev == "" {
prev = ctxs.CurrentContext
return fmt.Errorf("no previous context found")
}
curr := ctxs.CurrentContext
ctxs.PreviousContext = curr
Expand Down Expand Up @@ -166,11 +171,15 @@ func getContexts() (*Contexts, error) {
}

func writeContexts(ctxs *Contexts) error {
cfgFile := viper.GetViper().ConfigFileUsed()
fmt.Printf("update config:%s\n", cfgFile)
c, err := yaml.Marshal(ctxs)
if err != nil {
return err
}
return os.WriteFile(cfgFile, c, 0600)
cfgFile := viper.GetViper().ConfigFileUsed()
err = os.WriteFile(cfgFile, c, 0600)
if err != nil {
return err
}
fmt.Printf("%s switched context to \"%s\"\n", color.GreenString("✔"), color.GreenString(ctxs.CurrentContext))
return nil
}
2 changes: 1 addition & 1 deletion cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var loginCmd = &cobra.Command{
return err
}
console = os.Stdout
handler = auth.NewUpdateKubeConfigHandler(viper.GetString("kubeConfig"), console, auth.WithContextName(formatContextName(cloudContext, cs.CurrentContext)))
handler = auth.NewUpdateKubeConfigHandler(viper.GetString("kubeconfig"), console, auth.WithContextName(formatContextName(cloudContext, cs.CurrentContext)))
}

scopes := auth.DexScopes
Expand Down
2 changes: 1 addition & 1 deletion cmd/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ func machineConsole(driver *metalgo.Driver, args []string) error {
if err != nil {
return err
}
authContext, err := getAuthContext(viper.GetString("kubeConfig"))
authContext, err := getAuthContext(viper.GetString("kubeconfig"))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ apitoken: "alongtoken"
`)
rootCmd.PersistentFlags().StringP("url", "u", "", "api server address. Can be specified with METALCTL_URL environment variable.")
rootCmd.PersistentFlags().String("apitoken", "", "api token to authenticate. Can be specified with METALCTL_APITOKEN environment variable.")
rootCmd.PersistentFlags().String("kubeconfig", "", "Path to the kube-config to use for authentication and authorization. Is updated by login.")
rootCmd.PersistentFlags().String("kubeconfig", "", "Path to the kube-config to use for authentication and authorization. Is updated by login. Uses default path if not specified.")
rootCmd.PersistentFlags().StringP("order", "", "", "order by (comma separated) column(s), possible values: size|id|status|event|when|partition|project")
rootCmd.PersistentFlags().StringP("output-format", "o", "table", "output format (table|wide|markdown|json|yaml|template), wide is a table with more columns.")
rootCmd.PersistentFlags().StringP("template", "", "", `output template for template output-format, go template format.
Expand Down Expand Up @@ -187,7 +187,7 @@ func initConfig() {

// if there is no api token explicitly specified we try to pull it out of the kubeconfig context
if apiToken == "" {
authContext, err := getAuthContext(viper.GetString("kubeConfig"))
authContext, err := getAuthContext(viper.GetString("kubeconfig"))
// if there is an error, no kubeconfig exists for us ... this is not really an error
// since metalctl can be used in scripting with an hmac-key
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var whoamiCmd = &cobra.Command{
Long: "shows the current user, that will be used to authenticate commands.",
RunE: func(cmd *cobra.Command, args []string) error {

authContext, err := getAuthContext(viper.GetString("kubeConfig"))
authContext, err := getAuthContext(viper.GetString("kubeconfig"))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion crush/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func getAPIToken() string {
// if there is no api token explicitly specified we try to pull it out of
// the kubeconfig context
if apiToken == "" {
kubeconfig := viper.GetString("kubeConfig")
kubeconfig := viper.GetString("kubeconfig")
authContext, err := auth.CurrentAuthContext(kubeconfig)
// if there is an error, no kubeconfig exists for us ... this is not really an error
// if metalctl is used in scripting with an hmac-key
Expand Down

0 comments on commit a0ddc94

Please sign in to comment.