-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml.go
136 lines (96 loc) · 2.91 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright © 2023 Patrick Hermann [email protected]
*/
package cli
import (
"bytes"
"fmt"
"log"
"path/filepath"
"strings"
yamlv2 "gopkg.in/yaml.v2"
yamlv3 "gopkg.in/yaml.v3"
"sigs.k8s.io/yaml"
"github.com/spf13/viper"
)
func GetYamlStringKey(key, pathToConfig, extension string) string {
// READ YAML
viper.SetConfigName(strings.ReplaceAll(filepath.Base(pathToConfig), extension, ""))
viper.AddConfigPath(filepath.Dir(pathToConfig))
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
return viper.GetString(key)
}
func ReadYamlKeyValuesFromFile(yamlFileContent []byte) (yamlStructure map[string]interface{}) {
yamlStructure = make(map[string]interface{})
data := make(map[interface{}]interface{})
err := yamlv3.Unmarshal(yamlFileContent, &data)
if err != nil {
log.Fatal(err)
}
for k, v := range data {
yamlStructure[fmt.Sprintf("%v", k)] = fmt.Sprintf("%v", v)
}
return
}
func ReadYamlToObject(pathToYAML, fileExtension string, yamlObject interface{}) interface{} {
// READ YAML
viper.SetConfigName(strings.ReplaceAll(filepath.Base(pathToYAML), fileExtension, ""))
viper.AddConfigPath(filepath.Dir(pathToYAML))
if err := viper.ReadInConfig(); err != nil {
return yamlObject
}
if err := viper.Unmarshal(&yamlObject); err != nil {
fmt.Println(err)
return yamlObject
}
return yamlObject
}
func ReadInlineYamlToObject(inlineYAML []byte, yamlObject interface{}) interface{} {
viper.SetConfigType("yaml")
// READ YAML
viper.ReadConfig(bytes.NewBuffer(inlineYAML))
if err := viper.ReadConfig(bytes.NewBuffer(inlineYAML)); err != nil {
return yamlObject
}
if err := viper.Unmarshal(&yamlObject); err != nil {
fmt.Println(err)
return yamlObject
}
return yamlObject
}
func ConvertYAMLToJSON(yamlString string) string {
convertedJSON, err := yaml.YAMLToJSON([]byte(strings.Replace(yamlString, "\t", "\n", 5)))
if err != nil {
fmt.Printf("err: %v\n", err)
}
return string(convertedJSON)
}
func ConvertJSONToYAML(jsonString string) string {
// REMOVE ALL MAY EXISTING IVALID CHARS
jsonString = strings.ReplaceAll(jsonString, "\\", "")
jsonString = strings.TrimRight(jsonString, "\"")
jsonString = strings.TrimLeft(jsonString, "\"")
convertedYAML, err := yaml.JSONToYAML([]byte(jsonString))
if err != nil {
fmt.Printf("err: %v\n", err)
}
return string(convertedYAML)
}
func GetYAMLMapValues(content []byte, dictName string) (transformedValues map[string]interface{}) {
obj := make(map[string]interface{})
transformedValues = make(map[string]interface{})
err := yamlv2.Unmarshal(content, obj)
if err != nil {
fmt.Printf("UNMARSHALING: %v", err)
}
// ITERATE OVER KEYS AND VALUES
for dictKey, subKey := range obj[dictName].(map[interface{}]interface{}) {
for subKey, value := range subKey.(map[interface{}]interface{}) {
transformedValues[dictName+"_"+fmt.Sprint(dictKey)+"_"+fmt.Sprint(subKey)] = fmt.Sprint(value)
}
}
return transformedValues
}