From aaa1cb8ff3dd8a141d0174c22967ee6588f76364 Mon Sep 17 00:00:00 2001 From: Stefan Date: Tue, 30 May 2023 19:57:42 +0200 Subject: [PATCH] Added dryrun and folder check --- main.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++------- manifest.go | 2 +- release.go | 24 +++++++++++++-------- 3 files changed, 70 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index e98d4cf..0b4cb0f 100644 --- a/main.go +++ b/main.go @@ -2,15 +2,19 @@ package main import ( "flag" + "fmt" "log" + "os" _ "github.com/codingsince1985/checksum" ) func main() { - var source_dir = flag.String("source_dir", "./", "Path to image folder (/srv/fwuploads-tmp/)") - var target_dir = flag.String("target_dir", "./", "Path to image folder in public (/srv/fwuploads/troisdorf)") - var branch = flag.String("branch", "", "Branch to work with") + var source_dir = flag.String("s", "", "Path to image folder (/srv/fwuploads-tmp/)") + var target_dir = flag.String("t", "", "Path to image folder in public (/srv/fwuploads/troisdorf)") + var branch = flag.String("b", "", "Branch to work with") + var dryrun = flag.Bool("d", false, "Dryrun, just show changes on filesystem") + flag.Parse() sdir := *source_dir tdir := *target_dir @@ -19,10 +23,52 @@ func main() { var source_dir_branch string = sdir + br + "/" var target_dir_branch string = tdir + br + "/" - //generate manifest file for branch - err := generate_manifest(source_dir_branch) - if err != nil { - log.Println("Error generating Manifest file: ", err) + if is_firmware_folder(source_dir_branch) { + //generate manifest file for branch + if !(*dryrun) { + err := generate_manifest(source_dir_branch) + if err != nil { + log.Println("Error generating Manifest file: ", err) + } + } + release_branch(*branch, source_dir_branch, target_dir_branch, *dryrun) + } +} + +func is_firmware_folder(s string) bool { + + // check files in source_folder + files, err := os.ReadDir(s) + if err != nil { + fmt.Println("Fehler beim Lesen des Verzeichnisses:", err) + return false + } + + sysupgradeCount := 0 + factoryCount := 0 + otherCount := 0 + + // look for folders + for _, file := range files { + if file.IsDir() { + // check name + switch file.Name() { + case "sysupgrade": + sysupgradeCount++ + case "factory": + factoryCount++ + case "other": + otherCount++ + } + } + } + + // if there only the 3 folders then continue + if sysupgradeCount == 1 && factoryCount == 1 && otherCount == 1 { + log.Println("this looks like a gluon folder! Go on!") + return true + } else { + log.Println("the source folder does not look like a gluon firmware output.") + return false } - release_branch(*branch, source_dir_branch, target_dir_branch) } diff --git a/manifest.go b/manifest.go index d18032c..8a9a72c 100644 --- a/manifest.go +++ b/manifest.go @@ -39,7 +39,7 @@ func split_filenames(files []fs.FileInfo, dir string) []Files { current_name := files[i].Name() split := strings.Split(current_name, "-") if split[0] != "gluon" { - fmt.Println("skipping ", current_name) + log.Println("skipping ", current_name) continue } split_name := len(split) - 1 diff --git a/release.go b/release.go index 6e86729..3e1704b 100644 --- a/release.go +++ b/release.go @@ -1,12 +1,11 @@ package main import ( - "fmt" "log" "os" ) -func release_branch(branch string, source_dir string, target_dir string) { +func release_branch(branch string, source_dir string, target_dir string, dryrun bool) { checkOldFolder, _ := os.ReadDir(source_dir) checkNewFolder, _ := os.ReadDir(target_dir) var newImages bool = false @@ -17,19 +16,26 @@ func release_branch(branch string, source_dir string, target_dir string) { if newImages { // delete old firmware files in public folder for _, file := range checkNewFolder { - err := os.Remove(target_dir + file.Name()) - if err != nil { - log.Println("error deleting file: ", err) + if !(dryrun) { + err := os.RemoveAll(target_dir + file.Name()) + if err != nil { + log.Println("error deleting file: ", err) + } else { + log.Println("removed file: ", file.Name()) + } } else { - log.Println("removed file: ", file.Name()) + log.Println("DRYRUN: delete folder :", file.Name()) } } //move new files to folder for _, file := range checkOldFolder { - os.Rename(source_dir+file.Name(), target_dir+file.Name()) - fmt.Println("moved file: ", file.Name()) + if !(dryrun) { + os.Rename(source_dir+file.Name(), target_dir+file.Name()) + log.Println("moved file: ", file.Name()) + } else { + log.Println("DRYRUN: move folder: ", file.Name()) + } } - } }