Skip to content

Commit

Permalink
Feature: Add mkdir and new component creation logic (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
vshatravenko authored Apr 7, 2021
1 parent e1234df commit c75e058
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
13 changes: 10 additions & 3 deletions cmd/goci/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func actionVersions() error {
tmp := "./tmp"
// Path to versions file
if Path == "" {
Path = fmt.Sprintf("opendax/%s/versions.yaml", cnf.Branch)
Path = fmt.Sprintf("%s/opendax/%s/versions.yaml", tmp, cnf.Branch)
}
// Remove existing git folder
if err := os.RemoveAll(tmp); err != nil {
Expand All @@ -43,8 +43,15 @@ func actionVersions() error {
panic(err)
}

// Create the version directory if it doesn't exist
dir := strings.Split(Path, "/")
err = os.MkdirAll(strings.Join(dir[:len(dir)-1], "/"), os.ModePerm)
if err != nil {
panic(err)
}

fmt.Println("Loading the versions file")
v, err := versions.Load(fmt.Sprintf("%s/%s", tmp, Path))
v, err := versions.Load(Path)
if err != nil {
panic(err)
}
Expand All @@ -53,7 +60,7 @@ func actionVersions() error {
// Read .tags if exists to get tag version
Tag, err = getTag()
if err != nil {
panic(errors.New("Tag is missing"))
panic(errors.New("tag is missing"))
}
}

Expand Down
43 changes: 32 additions & 11 deletions versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"

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

// Load values.yaml
func Load(filename string) (*Versions, error) {
dat, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var v Versions

v := Versions{
data: make(map[string]interface{}),
filename: filename,
}
err = yaml.Unmarshal(dat, v.data)
if err != nil {
if _, err := os.Stat(filename); err == nil {
dat, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}

v := Versions{
data: make(map[string]interface{}),
filename: filename,
}
err = yaml.Unmarshal(dat, v.data)
if err != nil {
return nil, err
}
} else if os.IsNotExist(err) {
fmt.Printf("%s doesn't exist, initializing empty versions\n", filename)
v = Versions{
data: make(map[string]interface{}),
filename: filename,
}
} else {
return nil, err
}

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

// SetTag is a helper to set a tag for a given component
func (v *Versions) SetTag(component, value string) {
v.data[component].(map[string]interface{})["image"].(map[string]interface{})["tag"] = value
if v.data[component] == nil {
v.data[component] = map[string]interface{}{
"image": map[string]interface{}{
"tag": value,
},
}
} else {
v.data[component].(map[string]interface{})["image"].(map[string]interface{})["tag"] = value
}
}

// Save dump the values in yaml to the file
Expand Down

0 comments on commit c75e058

Please sign in to comment.