package main import ( "encoding/json" "fmt" "io/ioutil" "log" "os" "path/filepath" "strings" ) func release_branch(b string, sDir string, tDir string, dryrun bool, v string) { check_sDir, _ := os.ReadDir(sDir) check_tDir, _ := os.ReadDir(tDir) var newImages bool = false if len(check_sDir) > 0 { newImages = true } sVersion := GetGluonVersion(sDir) tVersion := GetGluonVersion(tDir) fmt.Println("Target Version:", b, tVersion) fmt.Println("Source Version:", b, sVersion) fmt.Println("Wanted Version:", b, v) //check wanted version in target if tVersion == v { log.Println(b, "wanted version is already published!") os.Exit(0) } log.Println(b, "published version is outdated, try to update from source folder") if sVersion != v { log.Println(b, "wanted Version is not availible in source folder") os.Exit(1) } if newImages { // delete old firmware files in public folder // if we are in stable branch, move images to archive if b == "stable" { err := os.Rename(tDir+"/*", tDir+"/archive/"+v) if err != nil { log.Fatalln(err) } } else { for _, file := range check_tDir { if !(dryrun) { err := os.RemoveAll(tDir + "/" + file.Name()) if err != nil { log.Println("error deleting file: ", err) } else { log.Println("removed file: ", file.Name()) } } else { log.Println("DRYRUN: delete folder :", file.Name()) } } } //move new files to folder for _, file := range check_sDir { if !(dryrun) { os.Rename(sDir+file.Name(), tDir+file.Name()) log.Println("moved file: ", file.Name()) } else { log.Println("DRYRUN: move folder: ", file.Name()) } } } } func GetGluonVersion(path string) string { image_folder := path + "/sysupgrade/" files, err := filepath.Glob(filepath.Join(image_folder, "gluon-tdf-*")) if err != nil { fmt.Println("Error reading directory: ", err) os.Exit(1) } if len(files) > 0 { fileName := filepath.Base(files[0]) parts := strings.Split(fileName, "-") if len(parts) >= 4 { version := parts[2] return version } } return "" } func readVersions(branch string) string { filePath := "/srv/fwuploads-tmp/bin/release_state.json" v, err := ioutil.ReadFile(filePath) if err != nil { log.Fatalln("Error reading version JSON file!") } var data Versions err = json.Unmarshal(v, &data) if err != nil { log.Fatalln("Error in JSON data!") } switch branch { case "stable": return data.Stable case "beta": return data.Beta case "experimental": return data.Experimental } return "" }