diff --git a/cmd/goci/versions.go b/cmd/goci/versions.go index e55f8f6..8190f33 100644 --- a/cmd/goci/versions.go +++ b/cmd/goci/versions.go @@ -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 { @@ -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) } @@ -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")) } } diff --git a/versions/versions.go b/versions/versions.go index e667fae..8e5560c 100644 --- a/versions/versions.go +++ b/versions/versions.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "io/ioutil" + "os" "strings" "gopkg.in/yaml.v3" @@ -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 } @@ -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