/
uyaml.go
194 lines (171 loc) · 4.14 KB
/
uyaml.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package yamlst
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/cwxstat/dopt/constants"
"gopkg.in/yaml.v2"
)
type FilesUpdate struct {
Files []string `yaml:"files,omitempty"`
}
type Platform struct {
OS string `yaml:"os,omitempty"`
}
type Spec struct {
Timestamp time.Time `yaml:"timestamp,omitempty"`
Platform Platform `yaml:"platform,omitempty"`
DockerCMD string `yaml:"cmd"`
DockerPush string `yaml:"push"`
LastCMD string `yaml:"last-cmd"`
Files FilesUpdate `yaml:"changes,omitempty"`
}
type TopYaml struct {
Image string `yaml:"image"`
Version string `yaml:"version"`
Config string `yaml:"local-config,omitempty"`
Git string `yaml:"dopt-git,omitempty"`
Spec Spec `yaml:"spec,omitempty"`
}
func NewDY() *TopYaml {
fu := FilesUpdate{
Files: []string{"pod.yaml, dev-pod.yaml"},
}
t := &TopYaml{
Image: "us-central1-docker.pkg.dev/mchirico/public/septa",
Version: "v0.0.5",
Config: "~/.docTag/config",
Git: "~/.docTag/dopt-git",
Spec: Spec{
Timestamp: time.Now(),
DockerCMD: "docker buildx build --no-cache --progress=plain --platform linux/amd64 --no-cache -t us-central1-docker.pkg.dev/mchirico/public/septa:v0.0.5 -f Dockerfile .",
LastCMD: "docker buildx build --no-cache --progress=plain --platform linux/amd64 --no-cache -t us-central1-docker.pkg.dev/mchirico/public/septa:v0.0.4 -f Dockerfile .",
DockerPush: "docker push us-central1-docker.pkg.dev/mchirico/public/septa:v0.0.5",
Platform: Platform{
OS: "linux/amd64",
},
Files: fu,
},
}
return t
}
func (dy *TopYaml) Comments() (string, error) {
a, err := yaml.Marshal(dy)
if err != nil {
return "", err
}
return addComments(string(a)), nil
}
func (dy *TopYaml) CommentsWithTags(tagBegin, tagEnd string) (string, error) {
a, err := yaml.Marshal(dy)
if err != nil {
return "", err
}
s := tagBegin + "\n" + string(a) + "\n" + tagEnd + "\n"
return addComments(s), nil
}
func (dy *TopYaml) UnMarshal(s string) error {
ts := removeComments(s)
err := yaml.Unmarshal([]byte(ts), dy)
return err
}
func (dy *TopYaml) ImageVersion(image, version string) {
dy.Image = image
dy.Version = version
dy.Spec.DockerCMD = fmt.Sprintf(constants.DOCKER_DFT_CMD, dy.Image+":"+dy.Version)
}
func (dy *TopYaml) NextMinor() error {
if dy.Version == "" {
dy.Version = "v0.0.1"
return nil
}
// Assume v0.0.1
split := strings.Split(dy.Version, ".")
if len(split) != 3 {
return fmt.Errorf("version not in `v0.0.1` format: %+v", dy.Version)
}
n, err := strconv.Atoi(split[2])
if err != nil {
return err
}
n = n + 1
dy.Version = fmt.Sprintf("%s.%s.%d", split[0], split[1], n)
return nil
}
func (dy *TopYaml) NextMajor() error {
if dy.Version == "" {
dy.Version = "v0.1.0"
return nil
}
// Assume v0.0.1
split := strings.Split(dy.Version, ".")
if len(split) != 3 {
return fmt.Errorf("version not in `v0.0.1` format: %+v", dy.Version)
}
n, err := strconv.Atoi(split[1])
if err != nil {
return err
}
n = n + 1
dy.Version = fmt.Sprintf("%s.%d.%s", split[0], n, split[2])
return nil
}
func MyTest() string {
fu := FilesUpdate{
Files: []string{"pod.yaml, dev-pod.yaml"},
}
t := &TopYaml{
Image: "ubuntu",
Version: "v0.0.1",
Config: "~/.docTag/config",
Spec: Spec{
Timestamp: time.Now(),
Platform: Platform{
OS: "linux/amd64",
},
Files: fu,
},
}
if a, err := yaml.Marshal(t); err == nil {
fmt.Println(a)
os.WriteFile("sample.yaml", []byte(a), 0644)
return string(a)
}
return ""
}
func addComments(s string) string {
split := strings.Split(s, "\n")
newString := ""
sep := ""
for _, v := range split {
if strings.HasPrefix(s, "#") {
newString = newString + sep + v
sep = "\n"
continue
}
newString = newString + sep + "# " + v
sep = "\n"
}
return newString
}
func removeComments(s string) string {
split := strings.Split(s, "\n")
newString := ""
sep := ""
for _, v := range split {
if strings.HasPrefix(s, "# ") {
newString = newString + sep + v[2:]
sep = "\n"
continue
}
if strings.HasPrefix(s, "#") {
newString = newString + sep + v[1:]
sep = "\n"
continue
}
newString = newString + sep + v
}
return newString
}