Skip to content

Commit 7d2a774

Browse files
authored
Fix: Allow empty --kubeconfig (#4)
1 parent 67ebd72 commit 7d2a774

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

.goreleaser.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ builds:
55
- linux
66
- windows
77
- freebsd
8+
- darwin
89
goarch:
910
- amd64
1011
- arm64

main.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type KubectlClusterWithName struct {
5656

5757
const KUBECONFIG_ENV = "KUBECONFIG"
5858
const KUBECONFIG_ENV_KEY = "$KUBECONFIG"
59-
const KUBECONFIG_DEFAULT_PATH = "~/kube/config"
59+
const KUBECONFIG_DEFAULT_PATH = "~/.kube/config"
6060

6161
func ParseKubeConfig(path string) (*KubectlConfig, error) {
6262

@@ -77,32 +77,32 @@ func ParseKubeConfig(path string) (*KubectlConfig, error) {
7777
func validate(kubectlConfig KubectlConfig, name string) error {
7878

7979
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")
8181
}
8282

8383
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")
8585
}
8686

8787
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")
8989
}
9090

9191
for _, v := range kubectlConfig.Clusters {
9292
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)
9494
}
9595
}
9696

9797
for _, v := range kubectlConfig.Contexts {
9898
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)
100100
}
101101
}
102102

103103
for _, v := range kubectlConfig.Users {
104104
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)
106106
}
107107
}
108108

@@ -144,10 +144,8 @@ func getKubeConfigPath(passedValue string) string {
144144

145145
// case1: env variable exists
146146
kubeConfigPath = os.Getenv(KUBECONFIG_ENV)
147+
// case2: fallback to default path
147148
if len(kubeConfigPath) == 0 {
148-
log.Fatalf("%s exists with no value!", KUBECONFIG_ENV)
149-
} else {
150-
// case2: fallback to default path
151149
log.Printf("%s env variable does not exist. Default %s path will be used\n", KUBECONFIG_ENV, KUBECONFIG_DEFAULT_PATH)
152150
}
153151
}
@@ -158,7 +156,7 @@ func getKubeConfigPath(passedValue string) string {
158156
func main() {
159157
kubeConfigPtr := flag.String("kubeconfig", "", fmt.Sprintf("path to the kubeconfig file (defaults '%s' or '%s')", KUBECONFIG_ENV_KEY, KUBECONFIG_DEFAULT_PATH))
160158
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)")
162160
flag.Parse()
163161

164162
var kubeConfigPath = getKubeConfigPath(*kubeConfigPtr)
@@ -185,5 +183,8 @@ func main() {
185183
}
186184

187185
err = os.WriteFile(kubeConfigPath, data, 0644)
186+
if err != nil {
187+
log.Panic(err)
188+
}
188189
fmt.Printf("%s was modified successfully\n", kubeConfigPath)
189190
}

0 commit comments

Comments
 (0)