Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed committed Jan 10, 2024
1 parent 341f331 commit 6b6bbcc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion internal/config/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,6 @@ func (a *Aliases) SaveAliases(path string) error {
if err != nil {
return err
}
return os.WriteFile(path, cfg, 0644)

return os.WriteFile(path, cfg, data.DefaultFileMod)
}
8 changes: 6 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *Config) Refine(flags *genericclioptions.ConfigFlags, k9sFlags *Flags, c
}
_, err = c.K9s.ActivateContext(n)
if err != nil {
return fmt.Errorf("unable to activate context %q: %w", *flags.Context, err)
return fmt.Errorf("unable to activate context %q: %w", n, err)
}
}
log.Debug().Msgf("Active Context %q", c.K9s.ActiveContextName())
Expand Down Expand Up @@ -149,6 +149,10 @@ func (c *Config) FavNamespaces() []string {

// SetActiveNamespace set the active namespace in the current context.
func (c *Config) SetActiveNamespace(ns string) error {
if ns == client.NotNamespaced {
log.Debug().Msgf("[SetNS] No namespace given. skipping!")
return nil
}
ct, err := c.K9s.ActiveContext()
if err != nil {
return err
Expand Down Expand Up @@ -251,7 +255,7 @@ func (c *Config) SaveFile(path string) error {
return err
}

return os.WriteFile(path, cfg, 0644)
return os.WriteFile(path, cfg, data.DefaultFileMod)
}

// Validate the configuration.
Expand Down
8 changes: 8 additions & 0 deletions internal/config/data/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os"
"sync"

"github.com/derailed/k9s/internal/client"
"gopkg.in/yaml.v2"
Expand All @@ -16,6 +17,7 @@ import (
// Config tracks a context configuration.
type Config struct {
Context *Context `yaml:"k9s"`
mx sync.RWMutex
}

// NewConfig returns a new config.
Expand All @@ -27,6 +29,9 @@ func NewConfig(ct *api.Context) *Config {

// Validate ensures config is in norms.
func (c *Config) Validate(conn client.Connection, ks KubeSettings) {
c.mx.Lock()
defer c.mx.Unlock()

if c.Context == nil {
c.Context = NewContext()
}
Expand All @@ -42,6 +47,9 @@ func (c *Config) Dump(w io.Writer) {

// Save saves the config to disk.
func (c *Config) Save(path string) error {
c.mx.RLock()
defer c.mx.RUnlock()

if err := EnsureDirPath(path, DefaultDirMod); err != nil {
return err
}
Expand Down
5 changes: 1 addition & 4 deletions internal/config/k9s.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,12 @@ func (k *K9s) ActivateContext(n string) (*data.Context, error) {
// If the context specifies a namespace, use it!
if ns := ct.Namespace; ns != client.BlankNamespace {
k.activeConfig.Context.Namespace.Active = ns
} else {
} else if k.activeConfig.Context.Namespace.Active == "" {
k.activeConfig.Context.Namespace.Active = client.DefaultNamespace
}
if k.activeConfig.Context == nil {
return nil, fmt.Errorf("context activation failed for: %s", n)
}
if k.activeConfig.Context == nil {
return nil, fmt.Errorf("context activation failed for: %s", n)
}

return k.activeConfig.Context, nil
}
Expand Down

0 comments on commit 6b6bbcc

Please sign in to comment.