This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
/
action.go
123 lines (104 loc) · 3.38 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package list
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/bygui86/konf-sh/pkg/commons"
"github.com/bygui86/konf-sh/pkg/kubeconfig"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
)
func list(ctx *cli.Context) error {
zap.L().Debug("🐛 Executing LIST command")
cfgsErr := listKubeCfgs(ctx)
if cfgsErr != nil {
return cfgsErr
}
kfgsErr := listSingleKfgs(ctx)
if kfgsErr != nil {
return kfgsErr
}
zap.L().Info("")
return nil
}
func listKubeCfgs(ctx *cli.Context) error {
zap.L().Debug("🐛 Get Kubernetes configuration file path")
kCfgFilePath := ctx.String(commons.KubeConfigFlagName)
zap.S().Infof("📖 Load Kubernetes configuration from '%s'", kCfgFilePath)
kCfg := kubeconfig.Load(kCfgFilePath)
// INFO: no need to check if kubeConfig is nil, because the inner method called will exit if it does not find the configuration file
zap.S().Debugf("🐛 Validate Kubernetes configuration from '%s'", kCfgFilePath)
valErr := kubeconfig.Validate(kCfg)
if valErr != nil {
return cli.Exit(
fmt.Sprintf("❌ Error validating Kubernetes configuration from '%s': %s",
kCfgFilePath, valErr.Error()), 12)
}
zap.S().Debugf("🐛 List contexts in Kubernetes configuration '%s'", kCfgFilePath)
ctxs := kubeconfig.ListContexts(kCfg)
if len(ctxs) > 0 {
zap.S().Infof("📚 Available Kubernetes contexts in '%s':", kCfgFilePath)
for _, v := range ctxs {
zap.S().Infof("\t%s", v)
}
} else {
zap.S().Warnf("🚨 No available Kubernetes context in '%s'", kCfgFilePath)
}
zap.L().Info("")
return nil
}
func listSingleKfgs(ctx *cli.Context) error {
zap.L().Debug("🐛 Get single Kubernetes konfigurations path")
singleKfgsPath := ctx.String(commons.SingleKonfigsFlagName)
zap.S().Debugf("🐛 Single Kubernetes konfigurations path: '%s'", singleKfgsPath)
checkErr := commons.CheckIfFolderExist(singleKfgsPath, false)
if checkErr != nil {
zap.S().Warnf("🚨 Single Kubernetes konfigurations path not found ('%s')", singleKfgsPath)
zap.L().Warn("💬 Tip: run 'konf split' before anything else")
} else {
validCfgs := make([]string, 0)
invalidCfgs := make([]string, 0)
err := filepath.Walk(
singleKfgsPath,
func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if strings.HasPrefix(info.Name(), ".") {
return nil
}
kCfg := kubeconfig.Load(path)
// INFO: no need to check if kubeConfig is nil, because the inner method called will exit if it does not find the configuration file
if kubeconfig.Validate(kCfg) != nil {
invalidCfgs = append(invalidCfgs, info.Name())
return nil
}
validCfgs = append(validCfgs, info.Name())
return nil
},
)
if err != nil {
return cli.Exit(
fmt.Sprintf("❌ Error listing single Kubernetes konfigurations in '%s': %s", singleKfgsPath, err.Error()),
21)
}
if len(validCfgs) > 0 {
zap.S().Infof("📚 Available Kubernetes konfigurations in '%s':", singleKfgsPath)
for _, v := range validCfgs {
zap.S().Infof("\t%s", v)
}
} else {
zap.S().Warnf("🚨 No available Kubernetes konfigurations in '%s'", singleKfgsPath)
zap.L().Warn("💬 Tip: run 'konf split' before anything else")
}
if len(invalidCfgs) > 0 {
zap.L().Info("")
zap.S().Infof("❓ Invalid Kubernetes konfigurations in '%s':", singleKfgsPath)
for _, iv := range invalidCfgs {
zap.S().Infof("\t%s", iv)
}
}
}
return nil
}