@@ -16,17 +16,41 @@ import (
1616
1717// wrapKubeconfigCommand adds special handling for kubeconfig command
1818func wrapKubeconfigCommand (wrappedCmd * cobra.Command , originalRunE func (* cobra.Command , []string ) error ) {
19+ // Add --login flag to update system kubeconfig file instead of local one
20+ wrappedCmd .Flags ().BoolP ("login" , "l" , false , "update system kubeconfig file, not local one" )
21+
1922 wrappedCmd .RunE = func (cmd * cobra.Command , args []string ) error {
20- // Always use kubeconfig path from Chart.yaml globalOptions
21- kubeconfigPath := Config .GlobalOptions .Kubeconfig
22- if kubeconfigPath == "" {
23- // Default to "kubeconfig" if not specified in Chart.yaml
24- kubeconfigPath = "kubeconfig"
23+ // Check if --login flag is set
24+ loginFlagValue , _ := cmd .Flags ().GetBool ("login" )
25+
26+ var newArgs []string
27+ var kubeconfigPath string
28+
29+ // If --login flag is set, use original args without auto-substitution
30+ if loginFlagValue {
31+ newArgs = args
32+ // Still need kubeconfigPath for post-processing (permissions, encryption, etc.)
33+ // Use first arg if provided, otherwise fall back to default
34+ if len (args ) > 0 {
35+ kubeconfigPath = args [0 ]
36+ } else {
37+ kubeconfigPath = Config .GlobalOptions .Kubeconfig
38+ if kubeconfigPath == "" {
39+ kubeconfigPath = "kubeconfig"
40+ }
41+ }
42+ } else {
43+ // Always use kubeconfig path from Chart.yaml globalOptions
44+ kubeconfigPath = Config .GlobalOptions .Kubeconfig
45+ if kubeconfigPath == "" {
46+ // Default to "kubeconfig" if not specified in Chart.yaml
47+ kubeconfigPath = "kubeconfig"
48+ }
49+ // Replace args with path from Chart.yaml
50+ newArgs = []string {kubeconfigPath }
2551 }
2652
27- // Replace args with path from Chart.yaml
28- newArgs := []string {kubeconfigPath }
29- // Execute original command with path from Chart.yaml
53+ // Execute original command with args
3054 if originalRunE != nil {
3155 if err := originalRunE (cmd , newArgs ); err != nil {
3256 return err
@@ -77,27 +101,36 @@ func wrapKubeconfigCommand(wrappedCmd *cobra.Command, originalRunE func(*cobra.C
77101 }
78102
79103 // Automatically update kubeconfig.encrypted if it exists and talm.key exists
80- encryptedKubeconfigPath := kubeconfigPath + ".encrypted"
81- encryptedKubeconfigFile := filepath .Join (Config .RootDir , encryptedKubeconfigPath )
82- keyFile := filepath .Join (Config .RootDir , "talm.key" )
83-
84- encryptedExists := fileExists (encryptedKubeconfigFile )
85- keyExists := fileExists (keyFile )
86-
87- if encryptedExists && keyExists {
88- // Both files exist, encrypt kubeconfig
89- if err := age .EncryptYAMLFile (Config .RootDir , kubeconfigPath , encryptedKubeconfigPath ); err != nil {
90- // Don't fail the command if encryption fails, but log warning
91- fmt .Fprintf (os .Stderr , "Warning: failed to encrypt kubeconfig: %v\n " , err )
92- } else {
93- fmt .Fprintf (os .Stderr , "Updated %s\n " , encryptedKubeconfigPath )
104+ // Skip this if --login flag is set
105+ if ! loginFlagValue {
106+ encryptedKubeconfigPath := kubeconfigPath + ".encrypted"
107+ encryptedKubeconfigFile := filepath .Join (Config .RootDir , encryptedKubeconfigPath )
108+ keyFile := filepath .Join (Config .RootDir , "talm.key" )
109+
110+ encryptedExists := fileExists (encryptedKubeconfigFile )
111+ keyExists := fileExists (keyFile )
112+
113+ if encryptedExists && keyExists {
114+ // Both files exist, encrypt kubeconfig
115+ if err := age .EncryptYAMLFile (Config .RootDir , kubeconfigPath , encryptedKubeconfigPath ); err != nil {
116+ // Don't fail the command if encryption fails, but log warning
117+ fmt .Fprintf (os .Stderr , "Warning: failed to encrypt kubeconfig: %v\n " , err )
118+ } else {
119+ fmt .Fprintf (os .Stderr , "Updated %s\n " , encryptedKubeconfigPath )
120+ }
94121 }
95122 }
96123 }
97124
98125 return nil
99126 }
100- // Remove Args validation to allow command to work without arguments
101- wrappedCmd .Args = cobra .NoArgs
127+ // Set custom Args validation: allow no args by default, but allow args when --login is set
128+ wrappedCmd .Args = func (cmd * cobra.Command , args []string ) error {
129+ loginFlagValue , _ := cmd .Flags ().GetBool ("login" )
130+ if ! loginFlagValue && len (args ) > 0 {
131+ return fmt .Errorf ("kubeconfig command does not accept arguments (use --login flag to pass arguments)" )
132+ }
133+ return nil
134+ }
102135}
103136
0 commit comments