-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (113 loc) · 2.82 KB
/
main.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
package main
import (
"fmt"
homedir "github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
)
type Environment struct {
Scheme string `yaml:"scheme"`
Value string `yaml:"value"`
}
type Field struct {
Keys []string `yaml:"keys""`
Type string `yaml:"type"`
Environment map[string]Environment `yaml:"environment"`
}
func (field *Field) GetEnvironment(environment string) *Environment {
env := field.Environment[environment]
return &env
}
type Information struct {
Fields []Field `yaml:"fields"`
AbsoluteConfig []AbsoluteConfig `yaml:"absolute-configs"`
}
type AbsoluteConfig struct {
Key string `yaml:"config-key"`
Environment map[string]interface{} `yaml:"environment"`
}
type V1TrainerYaml struct {
Version string `yaml:"version""`
Information Information `yaml:"information"`
}
func NewV1TrainerYaml(file string) (*V1TrainerYaml, error) {
y, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
var trainer V1TrainerYaml
err = yaml.Unmarshal(y, &trainer)
if err != nil {
return nil, err
}
return &trainer, nil
}
func main() {
Execute()
}
func RunGenerate(args []string, profile, output string) {
var mlist []*map[string]interface{}
for _, v := range args {
m := NewMapWithFile(v)
mlist = append(mlist, &m)
}
m3 := MergeMaps(mlist...)
homeDir := getHomeDir()
trainer, err := NewV1TrainerYaml(homeDir + "/.spg/config.yml")
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
g := NewGenerator(trainer, profile)
yaml := ToYaml(g.Generate(m3))
ioutil.WriteFile(output, yaml, 0644)
}
func HandleConfig(action, path string) {
if action == "set" {
file, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("Error reading file: %v", err)
os.Exit(1)
}
var config interface{}
err = yaml.Unmarshal(file, &config)
if err != nil {
fmt.Printf("Error parsing file: %v. It may be not yaml file.", err)
os.Exit(1)
}
homeDir := getHomeDir()
err = os.MkdirAll(homeDir+"/.spg", 0755)
if err != nil {
fmt.Printf("Error creating directory: %v", err)
os.Exit(1)
}
configPath := homeDir + "/.spg/config.yml"
err = ioutil.WriteFile(configPath, file, 0644)
if err != nil {
fmt.Printf("Error saving file: %v", err)
os.Exit(1)
}
} else if action == "unset" {
homeDir := getHomeDir()
configPath := homeDir + "/.spg/config.yml"
err := os.Remove(configPath)
if err != nil {
fmt.Printf("Error removing config: %v", err)
os.Exit(1)
}
} else if action == "print" {
homeDir := getHomeDir()
configPath := homeDir + "/.spg/config.yml"
m := NewMapWithFile(configPath)
fmt.Println(Pretty(m))
}
}
func getHomeDir() string {
h, err := homedir.Dir()
if err != nil {
fmt.Printf("Error getting home directory: %v", err)
os.Exit(1)
}
return h
}