Skip to content

Commit 359951b

Browse files
authored
Merge pull request #9 from bvankampen/bug-fix
bugfixes and multiple supported extensions (yaml, config, conf)
2 parents 4c9dca0 + 4d77471 commit 359951b

4 files changed

Lines changed: 34 additions & 9 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.3.1
1+
1.4

internal/config/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
var defaultConf = AppConfig{
1818
KubeconfigDir: "~/.kube",
1919
KubeconfigFile: "config",
20-
ExtraKubeconfigDirs: []string{"~/Downloads"},
20+
ExtraKubeconfigDirs: []string{},
2121
ShowKubeConfig: true,
2222
CreateLink: true,
2323
}

internal/kubeconfig/main.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,28 @@ func loadKubeConfig(dir string, file string) (api.Config, error) {
3636
return *config, nil
3737
}
3838

39+
func checkSuffixes(fileName string) bool {
40+
suffixes := []string{".yaml", ".config", ".conf"}
41+
for _, suffix := range suffixes {
42+
if strings.HasSuffix(fileName, suffix) {
43+
return true
44+
}
45+
}
46+
return false
47+
}
48+
3949
func loadKubeConfigsFromDirectory(dir string) []api.Config {
4050
var apiConfigs []api.Config
4151
dir, _ = homedir.Expand(dir)
4252
files, err := os.ReadDir(dir)
4353
if err != nil {
44-
logrus.Fatalf("Error reading directory: %s (%v)", dir, err)
54+
logrus.Errorf("Error reading directory: %s (%v)", dir, err)
55+
return nil
4556
}
4657
for _, file := range files {
4758
if !file.IsDir() {
48-
if strings.HasSuffix(file.Name(), ".yaml") {
59+
// if strings.HasSuffix(file.Name(), ".yaml") {
60+
if checkSuffixes(file.Name()) {
4961
config, err := loadKubeConfig(dir, file.Name())
5062
if err == nil {
5163
apiConfigs = append(apiConfigs, config)
@@ -57,6 +69,11 @@ func loadKubeConfigsFromDirectory(dir string) []api.Config {
5769
}
5870

5971
func LoadKubeConfigs(appconfig config.AppConfig) ([]api.Config, api.Config) {
72+
kubeConfigDir, _ := homedir.Expand(appconfig.KubeconfigDir)
73+
_, err := os.ReadDir(kubeConfigDir)
74+
if err != nil {
75+
logrus.Fatalf("Error reading kubeconfig directory: %s (%v)", kubeConfigDir, err)
76+
}
6077
apiConfigs := loadKubeConfigsFromDirectory(appconfig.KubeconfigDir)
6178
for _, dir := range appconfig.ExtraKubeconfigDirs {
6279
apiConfigs = append(apiConfigs, loadKubeConfigsFromDirectory(dir)...)

internal/ui/app_main.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ func (ui *UI) deleteConfigByIndex(index int) {
117117
}
118118

119119
func (ui *UI) getConfigByIndex(index int) (string, api.Config, *api.Context) {
120+
if ui.list.GetItemCount() == 0 {
121+
return "", api.Config{}, &api.Context{}
122+
}
120123
contextName, _ := ui.list.GetItemText(index)
121124
for _, config := range ui.kubeConfigs {
122125
for name, context := range config.Contexts {
@@ -133,12 +136,14 @@ func (ui *UI) createInfoTable() *tview.Table {
133136
infoTable.SetBorder(true)
134137
infoTable.SetTitle("Cluster")
135138
name, config, context := ui.getConfigByIndex(ui.list.GetCurrentItem())
136-
addtoTable(infoTable, "Context", name)
137-
addtoTable(infoTable, "Cluster", context.Cluster)
138-
addtoTable(infoTable, "User", context.AuthInfo)
139+
if name != "" {
140+
addtoTable(infoTable, "Context", name)
141+
addtoTable(infoTable, "Cluster", context.Cluster)
142+
addtoTable(infoTable, "User", context.AuthInfo)
139143

140-
addtoTable(infoTable, "Server", config.Clusters[context.Cluster].Server)
141-
addtoTable(infoTable, "File", context.LocationOfOrigin)
144+
addtoTable(infoTable, "Server", config.Clusters[context.Cluster].Server)
145+
addtoTable(infoTable, "File", context.LocationOfOrigin)
146+
}
142147
return infoTable
143148
}
144149

@@ -186,6 +191,9 @@ func (ui *UI) createAppMain() {
186191
ui.mainFlex.AddItem(ui.views, 0, 2, false)
187192
ui.list.SetCurrentItem(currentIndex)
188193
ui.redrawAppMain()
194+
if ui.list.GetItemCount() == 0 {
195+
ui.ErrorMessage("No (other) configs found, nothing to choose from.")
196+
}
189197
}
190198

191199
func (ui *UI) redrawAppMain() {

0 commit comments

Comments
 (0)