Skip to content

Commit

Permalink
Move funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
mikutas committed Nov 9, 2023
1 parent 74bb63a commit 719ee59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
43 changes: 0 additions & 43 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,56 +132,13 @@ func (c *Config) CurrentCluster() *Cluster {
return nil
}

// ActiveNamespace returns the active namespace in the current cluster.
func (c *Config) ActiveNamespace() string {
if c.K9s.Clusters == nil {
log.Warn().Msgf("No context detected returning default namespace")
return "default"
}
cl := c.CurrentCluster()
if cl != nil && cl.Namespace != nil {
return cl.Namespace.Active
}
if cl == nil {
cl = NewCluster()
c.K9s.Clusters[c.K9s.CurrentCluster] = cl
}
if ns, err := c.settings.CurrentNamespaceName(); err == nil && ns != "" {
if cl.Namespace == nil {
cl.Namespace = NewNamespace()
}
cl.Namespace.Active = ns
return ns
}

return "default"
}

// ValidateFavorites ensure favorite ns are legit.
func (c *Config) ValidateFavorites() {
cl := c.K9s.ActiveCluster()
cl.Validate(c.client, c.settings)
cl.Namespace.Validate(c.client, c.settings)
}

// FavNamespaces returns fav namespaces in the current cluster.
func (c *Config) FavNamespaces() []string {
cl := c.K9s.ActiveCluster()

return cl.Namespace.Favorites
}

// SetActiveNamespace set the active namespace in the current cluster.
func (c *Config) SetActiveNamespace(ns string) error {
if cl := c.K9s.ActiveCluster(); cl != nil {
return cl.Namespace.SetActive(ns, c.settings)
}
err := errors.New("no active cluster. unable to set active namespace")
log.Error().Err(err).Msg("SetActiveNamespace")

return err
}

// ActiveView returns the active view in the current cluster.
func (c *Config) ActiveView() string {
cl := c.K9s.ActiveCluster()
Expand Down
45 changes: 45 additions & 0 deletions internal/config/ns.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"errors"

"github.com/derailed/k9s/internal/client"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -95,3 +97,46 @@ func (n *Namespace) rmFavNS(ns string) {

n.Favorites = append(n.Favorites[:victim], n.Favorites[victim+1:]...)
}

// ActiveNamespace returns the active namespace in the current cluster.
func (c *Config) ActiveNamespace() string {
if c.K9s.Clusters == nil {
log.Warn().Msgf("No context detected returning default namespace")
return "default"
}
cl := c.CurrentCluster()
if cl != nil && cl.Namespace != nil {
return cl.Namespace.Active
}
if cl == nil {
cl = NewCluster()
c.K9s.Clusters[c.K9s.CurrentCluster] = cl
}
if ns, err := c.settings.CurrentNamespaceName(); err == nil && ns != "" {
if cl.Namespace == nil {
cl.Namespace = NewNamespace()
}
cl.Namespace.Active = ns
return ns
}

return "default"
}

// SetActiveNamespace set the active namespace in the current cluster.
func (c *Config) SetActiveNamespace(ns string) error {
if cl := c.K9s.ActiveCluster(); cl != nil {
return cl.Namespace.SetActive(ns, c.settings)
}
err := errors.New("no active cluster. unable to set active namespace")
log.Error().Err(err).Msg("SetActiveNamespace")

return err
}

// FavNamespaces returns fav namespaces in the current cluster.
func (c *Config) FavNamespaces() []string {
cl := c.K9s.ActiveCluster()

return cl.Namespace.Favorites
}

0 comments on commit 719ee59

Please sign in to comment.