From d7f9a0853b81da6857a738c758cc412b9cefdd6d Mon Sep 17 00:00:00 2001 From: Mircea-Pavel ANTON Date: Fri, 7 Mar 2025 21:43:57 +0200 Subject: [PATCH 1/5] cleanup configs --- .github/configs/commitlint.config.mjs | 33 +++------------------------ .github/configs/labeler.yaml | 1 - 2 files changed, 3 insertions(+), 31 deletions(-) diff --git a/.github/configs/commitlint.config.mjs b/.github/configs/commitlint.config.mjs index ce9276a..9bff459 100644 --- a/.github/configs/commitlint.config.mjs +++ b/.github/configs/commitlint.config.mjs @@ -1,33 +1,6 @@ -// Extending the conventional commit rules export default { extends: ["@commitlint/config-conventional"], rules: { - // Enforce specific commit types - "type-enum": [ - 2, // Error level - "always", - [ - "feat", // New feature - "fix", // Bug fix - "docs", // Documentation changes - "style", // Formatting, whitespace, missing semicolons, etc. - "refactor",// Code restructuring without functional changes - "perf", // Performance improvements - "test", // Adding or modifying tests - "chore", // Changes to build process or auxiliary tools - "ci", // Continuous Integration changes - "revert" // Reverting a previous commit - ] - ], - // Enforce case style for commit messages - "subject-case": [ - 2, // Error level - "always", - [ - "sentence-case", // Example: "Fix login button issue" - "start-case", // Example: "Fix Login Button Issue" - "lower-case", // Example: "fix login button issue" - ] - ] - } -}; + 'header-max-length': [2, 'always', 128], + }, +}; \ No newline at end of file diff --git a/.github/configs/labeler.yaml b/.github/configs/labeler.yaml index b0c5ced..6e90b3f 100644 --- a/.github/configs/labeler.yaml +++ b/.github/configs/labeler.yaml @@ -3,7 +3,6 @@ area/docker: - changed-files: - any-glob-to-any-file: ".dockerignore" - any-glob-to-any-file: "Dockerfile" - - any-glob-to-any-file: ".devcontainer/devcontainer.json" area/github: - changed-files: From d39c8bbbdea9dbc576caa474a34e203e57aedb0e Mon Sep 17 00:00:00 2001 From: Mircea-Pavel ANTON Date: Fri, 7 Mar 2025 21:44:04 +0200 Subject: [PATCH 2/5] ignore test configs --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d88c0a4..107bce5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ kubectl-switch dist/ +configs/ \ No newline at end of file From e5711449219a435c6aab89387562f378bce567a2 Mon Sep 17 00:00:00 2001 From: Mircea-Pavel ANTON Date: Fri, 7 Mar 2025 21:44:17 +0200 Subject: [PATCH 3/5] refactor + add `kubectl switch -` functionality --- cmd/context.go | 110 ++++----------------- cmd/namespace.go | 65 +++--------- cmd/root.go | 18 +++- pkg/kubeconfig/context.go | 78 +++++++++++++++ pkg/kubeconfig/errors.go | 12 +++ pkg/kubeconfig/kubeconfig.go | 185 +++++++++++++++++++++++++++++++++++ pkg/kubeconfig/namespace.go | 56 +++++++++++ 7 files changed, 378 insertions(+), 146 deletions(-) create mode 100644 pkg/kubeconfig/context.go create mode 100644 pkg/kubeconfig/errors.go create mode 100644 pkg/kubeconfig/kubeconfig.go create mode 100644 pkg/kubeconfig/namespace.go diff --git a/cmd/context.go b/cmd/context.go index 87570c0..1b98d60 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -1,93 +1,46 @@ package cmd import ( - "os" - "path/filepath" - "strings" - "github.com/AlecAivazis/survey/v2" + "github.com/mirceanton/kubectl-switch/pkg/kubeconfig" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "k8s.io/client-go/tools/clientcmd" ) -// contextCmd represents the switch command to change Kubernetes contexts. +var ( + contextManager = kubeconfig.NewContextManager(manager) +) + var contextCmd = &cobra.Command{ Use: "context", - Aliases: []string{"ctx"}, // Add this line to define the alias + Aliases: []string{"ctx"}, Short: "Switch the active Kubernetes context", - Args: cobra.MaximumNArgs(1), // Accept at most one argument (the context name) + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - // Determine the kubeconfig directory - if configDir == "" { - configDir = os.Getenv("KUBECONFIG_DIR") - if configDir == "" { - log.Fatal("kubeconfig directory not provided.") - log.Fatal("Please provide the directory containing kubeconfig files via the --config-dir flag or KUBECONFIG_DIR environment variable") - } - } - if strings.HasPrefix(configDir, "~/") { - homeDir, err := os.UserHomeDir() - if err != nil { - log.Fatalf("Failed to determine home directory: %v", err) - } - configDir = filepath.Join(homeDir, configDir[2:]) - } - - // Get all kubeconfig files in the config directory - files, err := os.ReadDir(configDir) + // Validate and get the config directory + validConfigDir, err := contextManager.ValidateConfigDir(configDir) if err != nil { - log.Fatalf("Failed to read directory: %v", err) + log.Fatal(err) } - // Parse all kubeconfig files in the directory - contextMap := make(map[string]string) // Map storing the context name (key) and the corresponding kubeconfig file (value) - var contextNames []string // List of context names for the interactive prompt - - for _, file := range files { - // Skip directories - if file.IsDir() { - continue - } - - // Skip files that are not YAML - if filepath.Ext(file.Name()) != ".yaml" && filepath.Ext(file.Name()) != ".yml" { - continue - } - - // Parse the kubeconfig file - path := filepath.Join(configDir, file.Name()) - kubeconfig, err := clientcmd.LoadFromFile(path) - if err != nil { - log.WithFields(log.Fields{"file": file.Name()}).Warnf("Failed to parse kubeconfig file: %v", err) - continue - } - - // Add context details to the map - for contextName := range kubeconfig.Contexts { - if _, exists := contextMap[contextName]; exists { - log.Fatalf("Duplicate context name '%s' found in files:\n- %s\n- %s", contextName, contextMap[contextName], path) - } - contextMap[contextName] = path - contextNames = append(contextNames, contextName) - } + // Get all contexts from kubeconfig files + contextMap, contextNames, err := contextManager.GetContextsFromDir(validConfigDir) + if err != nil { + log.Fatalf("Failed to read kubeconfig files: %v", err) } - // Check if any contexts were found if len(contextMap) == 0 { - log.Fatal("No kubernetes contexts found in the provided directory: ", configDir) + log.Fatal("No kubernetes contexts found in the provided directory: ", validConfigDir) } // Determine the target context var selectedContext string if len(args) == 1 { - // Non-interactive mode: use the provided cluster name selectedContext = args[0] if _, exists := contextMap[selectedContext]; !exists { log.Fatalf("Context '%s' not found", selectedContext) } } else { - // Interactive mode: show list of clusters prompt := &survey.Select{ Message: "Choose a context:", Options: contextNames, @@ -98,36 +51,9 @@ var contextCmd = &cobra.Command{ } } - // Determine the target location for copying the file - destPath := os.Getenv("KUBECONFIG") - if destPath == "" { - homeDir, err := os.UserHomeDir() - if err != nil { - log.Fatalf("Failed to determine home directory: %v", err) - } - destPath = filepath.Join(homeDir, ".kube", "config") - } - - // Ensure the destination directory exists - destDir := filepath.Dir(destPath) - err = os.MkdirAll(destDir, 0o755) - if err != nil { - log.Fatalf("Failed to create directory %s: %v", destDir, err) - } - - // Load the kubeconfig file for the selected context - kubeconfig, err := clientcmd.LoadFromFile(contextMap[selectedContext]) - if err != nil { - log.WithFields(log.Fields{"source": contextMap[selectedContext]}).Fatalf("Failed to parse kubeconfig file: %v", err) - } - - // Update the current context - kubeconfig.CurrentContext = selectedContext - - // Write the updated kubeconfig back to the file - err = clientcmd.WriteToFile(*kubeconfig, destPath) - if err != nil { - log.Fatalf("Error writing kubeconfig file: %v", err) + // Switch to the selected context + if err := contextManager.SwitchContext(contextMap[selectedContext], selectedContext); err != nil { + log.Fatalf("Failed to switch context: %v", err) } log.Infof("Switched to context '%s'", selectedContext) diff --git a/cmd/namespace.go b/cmd/namespace.go index ea8ccb5..06d6ce0 100644 --- a/cmd/namespace.go +++ b/cmd/namespace.go @@ -1,53 +1,24 @@ package cmd import ( - "context" - "os" - "path/filepath" - "github.com/AlecAivazis/survey/v2" + "github.com/mirceanton/kubectl-switch/pkg/kubeconfig" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/tools/clientcmd" ) -// namespaceCmd represents the command to change Kubernetes namespaces. +var ( + namespaceManager = kubeconfig.NewNamespaceManager(manager) +) + var namespaceCmd = &cobra.Command{ Use: "namespace", - Aliases: []string{"ns"}, // Add this line to define the alias + Aliases: []string{"ns"}, Short: "Switch the active Kubernetes namespace", - Args: cobra.MaximumNArgs(1), // Accept at most one argument (the namespace name) + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - // Determine the location of the kubeconfig file - kubeconfigPath := os.Getenv("KUBECONFIG") - if kubeconfigPath == "" { - homeDir, err := os.UserHomeDir() - if err != nil { - log.Fatalf("Failed to determine home directory: %v", err) - } - kubeconfigPath = filepath.Join(homeDir, ".kube", "config") - } - - // Build the Kubernetes client configuration - config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath) - if err != nil { - log.Fatalf("Error building kubeconfig: %v", err) - } - - // Create the Kubernetes clientset - clientset, err := kubernetes.NewForConfig(config) - if err != nil { - log.Fatalf("Error creating Kubernetes client: %v", err) - } - - // Get all namespaces - var namespaceNames []string - namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{}) - for _, ns := range namespaces.Items { - namespaceNames = append(namespaceNames, ns.Name) - } + // Get all namespaces from the current context + namespaceNames, err := namespaceManager.GetNamespaces() if err != nil { log.Fatalf("Error listing namespaces: %v", err) } @@ -55,10 +26,8 @@ var namespaceCmd = &cobra.Command{ // Determine the target namespace var selectedNamespace string if len(args) == 1 { - // Non-interactive mode: use the provided ma,es[ace] name selectedNamespace = args[0] } else { - // Interactive mode: show list of clusters prompt := &survey.Select{ Message: "Choose a namespace:", Options: namespaceNames, @@ -69,19 +38,9 @@ var namespaceCmd = &cobra.Command{ } } - // Load the kubeconfig file - kubeconfig, err := clientcmd.LoadFromFile(kubeconfigPath) - if err != nil { - log.WithFields(log.Fields{"source": kubeconfigPath}).Fatalf("Failed to parse kubeconfig file: %v", err) - } - - // Update the kubeconfig - kubeconfig.Contexts[kubeconfig.CurrentContext].Namespace = selectedNamespace - - // Write the updated kubeconfig back to the file - err = clientcmd.WriteToFile(*kubeconfig, kubeconfigPath) - if err != nil { - log.Fatalf("Error writing kubeconfig file: %v", err) + // Switch to the selected namespace + if err := namespaceManager.SwitchNamespace(selectedNamespace); err != nil { + log.Fatalf("Failed to switch namespace: %v", err) } log.Infof("Switched to namespace '%s'", selectedNamespace) diff --git a/cmd/root.go b/cmd/root.go index 394e9fb..54d0f55 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,12 +3,15 @@ package cmd import ( "os" + "github.com/mirceanton/kubectl-switch/pkg/kubeconfig" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) var ( configDir string // Global flag for the kubeconfig directory version string // The version of the tool, set at build time + manager = kubeconfig.NewManager() ) var rootCmd = &cobra.Command{ @@ -16,6 +19,20 @@ var rootCmd = &cobra.Command{ Short: "A tool to switch Kubernetes contexts", Long: `kubectl-switch is a CLI tool to switch Kubernetes contexts from multiple kubeconfig files.`, Version: version, + RunE: func(cmd *cobra.Command, args []string) error { + // Check if the "-" argument is provided to switch to the previous config + if len(args) == 1 && args[0] == "-" { + if err := manager.SwitchToPrevious(); err != nil { + if err == kubeconfig.ErrNoPreviousConfig { + log.Fatal("No previous configuration found") + } else { + log.Fatalf("Failed to switch to previous config: %v", err) + } + } + return nil + } + return cmd.Help() + }, } func Execute() { @@ -26,6 +43,5 @@ func Execute() { } func init() { - // Add any global flags here rootCmd.PersistentFlags().StringVarP(&configDir, "kubeconfig-dir", "", "", "Directory containing kubeconfig files") } diff --git a/pkg/kubeconfig/context.go b/pkg/kubeconfig/context.go new file mode 100644 index 0000000..8c548da --- /dev/null +++ b/pkg/kubeconfig/context.go @@ -0,0 +1,78 @@ +package kubeconfig + +import ( + "os" + "path/filepath" + "strings" +) + +// ContextManager handles operations related to Kubernetes contexts +type ContextManager struct { + kubeconfigManager *Manager +} + +// NewContextManager creates a new context manager +func NewContextManager(km *Manager) *ContextManager { + return &ContextManager{ + kubeconfigManager: km, + } +} + +// GetContextsFromDir gets all contexts from kubeconfig files in a directory +func (cm *ContextManager) GetContextsFromDir(configDir string) (map[string]string, []string, error) { + // Handle ~/ in path + if strings.HasPrefix(configDir, "~/") { + homeDir, err := os.UserHomeDir() + if err != nil { + return nil, nil, err + } + configDir = filepath.Join(homeDir, configDir[2:]) + } + + return cm.kubeconfigManager.GetContextsFromDir(configDir) +} + +// SwitchContext switches to the specified context +func (cm *ContextManager) SwitchContext(contextFilePath string, contextName string) error { + // Ensure the destination directory exists + if err := cm.kubeconfigManager.EnsureDir(); err != nil { + return err + } + + // Switch to the selected context + if err := cm.kubeconfigManager.SwitchContext(contextFilePath, contextName); err != nil { + return err + } + + return nil +} + +// ValidateConfigDir ensures the kubeconfig directory is valid and available +func (cm *ContextManager) ValidateConfigDir(configDir string) (string, error) { + if configDir == "" { + configDir = os.Getenv("KUBECONFIG_DIR") + if configDir == "" { + return "", ErrNoConfigDir + } + } + + // Handle ~/ in path + if strings.HasPrefix(configDir, "~/") { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", err + } + configDir = filepath.Join(homeDir, configDir[2:]) + } + + // Check if the directory exists + info, err := os.Stat(configDir) + if err != nil { + return "", err + } + if !info.IsDir() { + return "", ErrNotADirectory + } + + return configDir, nil +} diff --git a/pkg/kubeconfig/errors.go b/pkg/kubeconfig/errors.go new file mode 100644 index 0000000..830a24f --- /dev/null +++ b/pkg/kubeconfig/errors.go @@ -0,0 +1,12 @@ +package kubeconfig + +import ( + "errors" +) + +// Common errors for kubeconfig operations +var ( + ErrNoConfigDir = errors.New("kubeconfig directory not provided, please provide the directory containing kubeconfig files via the --config-dir flag or KUBECONFIG_DIR environment variable") + ErrNotADirectory = errors.New("the provided path is not a directory") + ErrNoPreviousConfig = errors.New("no previous configuration found") +) diff --git a/pkg/kubeconfig/kubeconfig.go b/pkg/kubeconfig/kubeconfig.go new file mode 100644 index 0000000..663caf6 --- /dev/null +++ b/pkg/kubeconfig/kubeconfig.go @@ -0,0 +1,185 @@ +package kubeconfig + +import ( + "os" + "path/filepath" + + log "github.com/sirupsen/logrus" + "k8s.io/client-go/tools/clientcmd" +) + +// Manager handles operations related to kubeconfig files +type Manager struct{} + +// NewManager creates a new kubeconfig manager +func NewManager() *Manager { + return &Manager{} +} + +// GetPath returns the path to the current kubeconfig file +func (m *Manager) GetPath() string { + path := os.Getenv("KUBECONFIG") + if path == "" { + homeDir, err := os.UserHomeDir() + if err != nil { + log.Fatalf("Failed to determine home directory: %v", err) + } + path = filepath.Join(homeDir, ".kube", "config") + } + return path +} + +// GetPreviousPath returns the path to the previous kubeconfig file +func (m *Manager) GetPreviousPath() string { + return filepath.Join(filepath.Dir(m.GetPath()), "config.previous") +} + +// SaveCurrent backs up the current kubeconfig file +func (m *Manager) SaveCurrent() error { + kubeconfigPath := m.GetPath() + if _, err := os.Stat(kubeconfigPath); os.IsNotExist(err) { + return err + } + + prevPath := m.GetPreviousPath() + data, err := os.ReadFile(kubeconfigPath) + if err != nil { + return err + } + + return os.WriteFile(prevPath, data, 0600) +} + +// SwitchToPrevious switches back to the previous Kubernetes config +func (m *Manager) SwitchToPrevious() error { + kubeconfigPath := m.GetPath() + prevPath := m.GetPreviousPath() + + // Check if previous config exists + if _, err := os.Stat(prevPath); os.IsNotExist(err) { + return ErrNoPreviousConfig + } + + // Swap the current and previous configs + tempPath := filepath.Join(filepath.Dir(kubeconfigPath), "config.temp") + + // Read current config + currentConfig, err := os.ReadFile(kubeconfigPath) + if err != nil { + return err + } + + // Read previous config + prevConfig, err := os.ReadFile(prevPath) + if err != nil { + return err + } + + // Write current to temp + if err := os.WriteFile(tempPath, currentConfig, 0600); err != nil { + return err + } + + // Write previous to current + if err := os.WriteFile(kubeconfigPath, prevConfig, 0600); err != nil { + return err + } + + // Write temp to previous + if err := os.WriteFile(prevPath, currentConfig, 0600); err != nil { + return err + } + + // Remove temp file + if err := os.Remove(tempPath); err != nil { + log.Warnf("Failed to remove temporary file: %v", err) + } + + return nil +} + +// EnsureDir ensures that the directory for the kubeconfig file exists +func (m *Manager) EnsureDir() error { + destDir := filepath.Dir(m.GetPath()) + return os.MkdirAll(destDir, 0o755) +} + +// GetContextsFromDir gets all contexts from kubeconfig files in a directory +func (m *Manager) GetContextsFromDir(configDir string) (map[string]string, []string, error) { + contextMap := make(map[string]string) + var contextNames []string + + files, err := os.ReadDir(configDir) + if err != nil { + return nil, nil, err + } + + for _, file := range files { + if file.IsDir() { + continue + } + + ext := filepath.Ext(file.Name()) + if ext != ".yaml" && ext != ".yml" { + continue + } + + path := filepath.Join(configDir, file.Name()) + kubeconfig, err := clientcmd.LoadFromFile(path) + if err != nil { + log.WithFields(log.Fields{"file": file.Name()}).Warnf("Failed to parse kubeconfig file: %v", err) + continue + } + + for contextName := range kubeconfig.Contexts { + if _, exists := contextMap[contextName]; exists { + log.Warnf("Duplicate context name '%s' found in files:\n- %s\n- %s", contextName, contextMap[contextName], path) + continue + } + contextMap[contextName] = path + contextNames = append(contextNames, contextName) + } + } + + return contextMap, contextNames, nil +} + +// SwitchContext switches to the specified context +func (m *Manager) SwitchContext(contextFilePath string, contextName string) error { + // Save the current config as the previous config + if err := m.SaveCurrent(); err != nil { + log.Warnf("Failed to save current configuration as previous: %v", err) + } + + // Load the kubeconfig file for the selected context + kubeconfig, err := clientcmd.LoadFromFile(contextFilePath) + if err != nil { + return err + } + + // Update the current context + kubeconfig.CurrentContext = contextName + + // Write the updated kubeconfig back to the file + return clientcmd.WriteToFile(*kubeconfig, m.GetPath()) +} + +// SwitchNamespace switches the namespace in the current context +func (m *Manager) SwitchNamespace(namespace string) error { + // Save the current config as the previous config + if err := m.SaveCurrent(); err != nil { + log.Warnf("Failed to save current configuration as previous: %v", err) + } + + // Load the kubeconfig file + kubeconfig, err := clientcmd.LoadFromFile(m.GetPath()) + if err != nil { + return err + } + + // Update the namespace in the current context + kubeconfig.Contexts[kubeconfig.CurrentContext].Namespace = namespace + + // Write the updated kubeconfig back to the file + return clientcmd.WriteToFile(*kubeconfig, m.GetPath()) +} diff --git a/pkg/kubeconfig/namespace.go b/pkg/kubeconfig/namespace.go new file mode 100644 index 0000000..6f9f413 --- /dev/null +++ b/pkg/kubeconfig/namespace.go @@ -0,0 +1,56 @@ +package kubeconfig + +import ( + "context" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/clientcmd" +) + +// NamespaceManager handles operations related to Kubernetes namespaces +type NamespaceManager struct { + kubeconfigManager *Manager +} + +// NewNamespaceManager creates a new namespace manager +func NewNamespaceManager(km *Manager) *NamespaceManager { + return &NamespaceManager{ + kubeconfigManager: km, + } +} + +// GetNamespaces gets all namespaces from the current context +func (nm *NamespaceManager) GetNamespaces() ([]string, error) { + kubeconfigPath := nm.kubeconfigManager.GetPath() + + // Build the Kubernetes client configuration + config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath) + if err != nil { + return nil, err + } + + // Create the Kubernetes clientset + clientset, err := kubernetes.NewForConfig(config) + if err != nil { + return nil, err + } + + // Get all namespaces + var namespaceNames []string + namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{}) + if err != nil { + return nil, err + } + + for _, ns := range namespaces.Items { + namespaceNames = append(namespaceNames, ns.Name) + } + + return namespaceNames, nil +} + +// SwitchNamespace switches the namespace in the current context +func (nm *NamespaceManager) SwitchNamespace(namespace string) error { + return nm.kubeconfigManager.SwitchNamespace(namespace) +} From 0269ae4e87fe9a91fa5b3023143c488f9eb48692 Mon Sep 17 00:00:00 2001 From: Mircea-Pavel ANTON Date: Fri, 7 Mar 2025 21:45:54 +0200 Subject: [PATCH 4/5] update tests --- scripts/test.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/scripts/test.sh b/scripts/test.sh index d73b197..bd19ecc 100644 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -86,7 +86,17 @@ run_tests() { exit 1 } + echo "Switching to previous context..." + ./kubectl-switch - + + echo "Validating cluster switch to test-cluster-1..." + kubectl get nodes | grep "test-cluster-1" || { + echo "Error: test-cluster-1 not found in node list!" >&2 + exit 1 + } + echo "=========================================================================================" echo "Tests completed successfully!" + echo "=========================================================================================" } # Set up trap to ensure cleanup happens on exit From 7138f9b5f286719f2ce517d31d5412cd496d29e8 Mon Sep 17 00:00:00 2001 From: Mircea-Pavel ANTON Date: Fri, 7 Mar 2025 21:50:38 +0200 Subject: [PATCH 5/5] fix lint --- cmd/context.go | 4 +--- cmd/namespace.go | 4 +--- pkg/kubeconfig/kubeconfig.go | 8 ++++---- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/cmd/context.go b/cmd/context.go index 1b98d60..3751993 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -7,9 +7,7 @@ import ( "github.com/spf13/cobra" ) -var ( - contextManager = kubeconfig.NewContextManager(manager) -) +var contextManager = kubeconfig.NewContextManager(manager) var contextCmd = &cobra.Command{ Use: "context", diff --git a/cmd/namespace.go b/cmd/namespace.go index 06d6ce0..4e8ba68 100644 --- a/cmd/namespace.go +++ b/cmd/namespace.go @@ -7,9 +7,7 @@ import ( "github.com/spf13/cobra" ) -var ( - namespaceManager = kubeconfig.NewNamespaceManager(manager) -) +var namespaceManager = kubeconfig.NewNamespaceManager(manager) var namespaceCmd = &cobra.Command{ Use: "namespace", diff --git a/pkg/kubeconfig/kubeconfig.go b/pkg/kubeconfig/kubeconfig.go index 663caf6..1582b81 100644 --- a/pkg/kubeconfig/kubeconfig.go +++ b/pkg/kubeconfig/kubeconfig.go @@ -47,7 +47,7 @@ func (m *Manager) SaveCurrent() error { return err } - return os.WriteFile(prevPath, data, 0600) + return os.WriteFile(prevPath, data, 0o600) } // SwitchToPrevious switches back to the previous Kubernetes config @@ -76,17 +76,17 @@ func (m *Manager) SwitchToPrevious() error { } // Write current to temp - if err := os.WriteFile(tempPath, currentConfig, 0600); err != nil { + if err := os.WriteFile(tempPath, currentConfig, 0o600); err != nil { return err } // Write previous to current - if err := os.WriteFile(kubeconfigPath, prevConfig, 0600); err != nil { + if err := os.WriteFile(kubeconfigPath, prevConfig, 0o600); err != nil { return err } // Write temp to previous - if err := os.WriteFile(prevPath, currentConfig, 0600); err != nil { + if err := os.WriteFile(prevPath, currentConfig, 0o600); err != nil { return err }