-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
yaml.go
89 lines (72 loc) · 2.29 KB
/
yaml.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
package view
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/tview"
"github.com/rs/zerolog/log"
)
var (
keyValRX = regexp.MustCompile(`\A(\s*)([\w|\-|\.|\/|\s]+):\s(.+)\z`)
keyRX = regexp.MustCompile(`\A(\s*)([\w|\-|\.|\/|\s]+):\s*\z`)
)
const (
yamlFullFmt = "%s[key::b]%s[colon::-]: [val::]%s"
yamlKeyFmt = "%s[key::b]%s[colon::-]:"
yamlValueFmt = "[val::]%s"
)
func colorizeYAML(style config.Yaml, raw string) string {
lines := strings.Split(tview.Escape(raw), "\n")
fullFmt := strings.Replace(yamlFullFmt, "[key", "["+style.KeyColor.String(), 1)
fullFmt = strings.Replace(fullFmt, "[colon", "["+style.ColonColor.String(), 1)
fullFmt = strings.Replace(fullFmt, "[val", "["+style.ValueColor.String(), 1)
keyFmt := strings.Replace(yamlKeyFmt, "[key", "["+style.KeyColor.String(), 1)
keyFmt = strings.Replace(keyFmt, "[colon", "["+style.ColonColor.String(), 1)
valFmt := strings.Replace(yamlValueFmt, "[val", "["+style.ValueColor.String(), 1)
buff := make([]string, 0, len(lines))
for _, l := range lines {
res := keyValRX.FindStringSubmatch(l)
if len(res) == 4 {
buff = append(buff, enableRegion(fmt.Sprintf(fullFmt, res[1], res[2], res[3])))
continue
}
res = keyRX.FindStringSubmatch(l)
if len(res) == 3 {
buff = append(buff, enableRegion(fmt.Sprintf(keyFmt, res[1], res[2])))
continue
}
buff = append(buff, enableRegion(fmt.Sprintf(valFmt, l)))
}
return strings.Join(buff, "\n")
}
func enableRegion(str string) string {
return strings.ReplaceAll(strings.ReplaceAll(str, "<<<", "["), ">>>", "]")
}
func saveYAML(cluster, name, data string) (string, error) {
dir := filepath.Join(config.K9sDumpDir, sanitizeFilename(cluster))
if err := ensureDir(dir); err != nil {
return "", err
}
now := time.Now().UnixNano()
fName := fmt.Sprintf("%s-%d.yml", sanitizeFilename(name), now)
path := filepath.Join(dir, fName)
mod := os.O_CREATE | os.O_WRONLY
file, err := os.OpenFile(path, mod, 0600)
if err != nil {
log.Error().Err(err).Msgf("YAML create %s", path)
return "", nil
}
defer func() {
if err := file.Close(); err != nil {
log.Error().Err(err).Msg("Closing yaml file")
}
}()
if _, err := file.Write([]byte(data)); err != nil {
return "", err
}
return path, nil
}