@@ -56,7 +56,7 @@ type KubectlClusterWithName struct {
56
56
57
57
const KUBECONFIG_ENV = "KUBECONFIG"
58
58
const KUBECONFIG_ENV_KEY = "$KUBECONFIG"
59
- const KUBECONFIG_DEFAULT_PATH = "~/kube/config"
59
+ const KUBECONFIG_DEFAULT_PATH = "~/. kube/config"
60
60
61
61
func ParseKubeConfig (path string ) (* KubectlConfig , error ) {
62
62
@@ -77,32 +77,32 @@ func ParseKubeConfig(path string) (*KubectlConfig, error) {
77
77
func validate (kubectlConfig KubectlConfig , name string ) error {
78
78
79
79
if len (kubectlConfig .Clusters ) != 1 {
80
- return errors .New ("Only one cluster can be merged into original kubeconfig" )
80
+ return errors .New ("only one cluster can be merged into original kubeconfig" )
81
81
}
82
82
83
83
if len (kubectlConfig .Users ) != 1 {
84
- return errors .New ("Only one user can be merged into original kubeconfig" )
84
+ return errors .New ("only one user can be merged into original kubeconfig" )
85
85
}
86
86
87
87
if len (kubectlConfig .Contexts ) != 1 {
88
- return errors .New ("Only one context can be merged into original kubeconfig" )
88
+ return errors .New ("only one context can be merged into original kubeconfig" )
89
89
}
90
90
91
91
for _ , v := range kubectlConfig .Clusters {
92
92
if strings .EqualFold (v .Name , name ) {
93
- return errors . New ( fmt .Sprintf ( "A cluster entry with %s already exists in kubeconfig, merge failed! " , name ) )
93
+ return fmt .Errorf ( "a cluster entry with %s already exists in kubeconfig, merge failed" , name )
94
94
}
95
95
}
96
96
97
97
for _ , v := range kubectlConfig .Contexts {
98
98
if strings .EqualFold (v .Name , name ) {
99
- return errors . New ( fmt .Sprintf ( "A context entry with %s already exists in kubeconfig, merge failed! " , name ) )
99
+ return fmt .Errorf ( "a context entry with %s already exists in kubeconfig, merge failed" , name )
100
100
}
101
101
}
102
102
103
103
for _ , v := range kubectlConfig .Users {
104
104
if strings .EqualFold (v .Name , name ) {
105
- return errors . New ( fmt .Sprintf ( "A user entry with %s already exists in kubeconfig, merge failed! " , name ) )
105
+ return fmt .Errorf ( "a user entry with %s already exists in kubeconfig, merge failed" , name )
106
106
}
107
107
}
108
108
@@ -144,10 +144,8 @@ func getKubeConfigPath(passedValue string) string {
144
144
145
145
// case1: env variable exists
146
146
kubeConfigPath = os .Getenv (KUBECONFIG_ENV )
147
+ // case2: fallback to default path
147
148
if len (kubeConfigPath ) == 0 {
148
- log .Fatalf ("%s exists with no value!" , KUBECONFIG_ENV )
149
- } else {
150
- // case2: fallback to default path
151
149
log .Printf ("%s env variable does not exist. Default %s path will be used\n " , KUBECONFIG_ENV , KUBECONFIG_DEFAULT_PATH )
152
150
}
153
151
}
@@ -158,7 +156,7 @@ func getKubeConfigPath(passedValue string) string {
158
156
func main () {
159
157
kubeConfigPtr := flag .String ("kubeconfig" , "" , fmt .Sprintf ("path to the kubeconfig file (defaults '%s' or '%s')" , KUBECONFIG_ENV_KEY , KUBECONFIG_DEFAULT_PATH ))
160
158
filePtr := flag .String ("file" , "" , "path to the yaml file that to be append into kubeconfig" )
161
- namePtr := flag .String ("name" , "" , fmt . Sprintf ( "Replaces the name of context, user and cluster (default file name of --file argument)" ) )
159
+ namePtr := flag .String ("name" , "" , "Replaces the name of context, user and cluster (default file name of --file argument)" )
162
160
flag .Parse ()
163
161
164
162
var kubeConfigPath = getKubeConfigPath (* kubeConfigPtr )
@@ -185,5 +183,8 @@ func main() {
185
183
}
186
184
187
185
err = os .WriteFile (kubeConfigPath , data , 0644 )
186
+ if err != nil {
187
+ log .Panic (err )
188
+ }
188
189
fmt .Printf ("%s was modified successfully\n " , kubeConfigPath )
189
190
}
0 commit comments