Skip to content

Commit c75e058

Browse files
authored
Feature: Add mkdir and new component creation logic (#6)
1 parent e1234df commit c75e058

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

cmd/goci/versions.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func actionVersions() error {
2222
tmp := "./tmp"
2323
// Path to versions file
2424
if Path == "" {
25-
Path = fmt.Sprintf("opendax/%s/versions.yaml", cnf.Branch)
25+
Path = fmt.Sprintf("%s/opendax/%s/versions.yaml", tmp, cnf.Branch)
2626
}
2727
// Remove existing git folder
2828
if err := os.RemoveAll(tmp); err != nil {
@@ -43,8 +43,15 @@ func actionVersions() error {
4343
panic(err)
4444
}
4545

46+
// Create the version directory if it doesn't exist
47+
dir := strings.Split(Path, "/")
48+
err = os.MkdirAll(strings.Join(dir[:len(dir)-1], "/"), os.ModePerm)
49+
if err != nil {
50+
panic(err)
51+
}
52+
4653
fmt.Println("Loading the versions file")
47-
v, err := versions.Load(fmt.Sprintf("%s/%s", tmp, Path))
54+
v, err := versions.Load(Path)
4855
if err != nil {
4956
panic(err)
5057
}
@@ -53,7 +60,7 @@ func actionVersions() error {
5360
// Read .tags if exists to get tag version
5461
Tag, err = getTag()
5562
if err != nil {
56-
panic(errors.New("Tag is missing"))
63+
panic(errors.New("tag is missing"))
5764
}
5865
}
5966

versions/versions.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io/ioutil"
7+
"os"
78
"strings"
89

910
"gopkg.in/yaml.v3"
@@ -17,17 +18,29 @@ type Versions struct {
1718

1819
// Load values.yaml
1920
func Load(filename string) (*Versions, error) {
20-
dat, err := ioutil.ReadFile(filename)
21-
if err != nil {
22-
return nil, err
23-
}
21+
var v Versions
2422

25-
v := Versions{
26-
data: make(map[string]interface{}),
27-
filename: filename,
28-
}
29-
err = yaml.Unmarshal(dat, v.data)
30-
if err != nil {
23+
if _, err := os.Stat(filename); err == nil {
24+
dat, err := ioutil.ReadFile(filename)
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
v := Versions{
30+
data: make(map[string]interface{}),
31+
filename: filename,
32+
}
33+
err = yaml.Unmarshal(dat, v.data)
34+
if err != nil {
35+
return nil, err
36+
}
37+
} else if os.IsNotExist(err) {
38+
fmt.Printf("%s doesn't exist, initializing empty versions\n", filename)
39+
v = Versions{
40+
data: make(map[string]interface{}),
41+
filename: filename,
42+
}
43+
} else {
3144
return nil, err
3245
}
3346

@@ -36,7 +49,15 @@ func Load(filename string) (*Versions, error) {
3649

3750
// SetTag is a helper to set a tag for a given component
3851
func (v *Versions) SetTag(component, value string) {
39-
v.data[component].(map[string]interface{})["image"].(map[string]interface{})["tag"] = value
52+
if v.data[component] == nil {
53+
v.data[component] = map[string]interface{}{
54+
"image": map[string]interface{}{
55+
"tag": value,
56+
},
57+
}
58+
} else {
59+
v.data[component].(map[string]interface{})["image"].(map[string]interface{})["tag"] = value
60+
}
4061
}
4162

4263
// Save dump the values in yaml to the file

0 commit comments

Comments
 (0)