package main import ( "fmt" "io/fs" "io/ioutil" "log" "os" "strconv" "strings" "time" "github.com/codingsince1985/checksum" ) func generate_manifest(dir string) error { files, err := get_files(dir) if err != nil { log.Println("Error reading Image Files in folder: ", dir) return err } file_struct := split_filenames(files, dir) build_manifest(file_struct, dir) return err } func get_files(dir string) ([]fs.FileInfo, error) { files, err := ioutil.ReadDir(dir) if err != nil { return nil, err } return files, nil } func split_filenames(files []fs.FileInfo, dir string) []Files { var data []Files for i := range files { current_name := files[i].Name() split := strings.Split(current_name, "-") split_name := len(split) - 1 // Routername router_name := strings.Join(split[5:split_name], "-") //RELEASE router_release := strings.Join(split[3:5], "-") //Checksum sha256, _ := checksum.SHA256sum(dir + "/" + current_name) //FileSize filesize := files[i].Size() //Build Struct data = append(data, Files{Model: router_name, Name: current_name, Release: router_release, Checksum: sha256, Size: filesize}) } return data } func build_manifest(files []Files, dir string) error { // Datei zum Schreiben öffnen release := strings.Split(files[0].Release, "-") file, err := os.Create(dir + release[0] + ".manifest") if err != nil { fmt.Println("error creating manifest file") return err } defer file.Close() // Aktuelles Datum und Uhrzeit abrufen now := time.Now() // Datum im gewünschten Format formatieren formatted := now.Format("2006-01-02 15:04:05-07:00") branch_line := "BRANCH=" + release[0] + "\n" date_line := "DATE=" + formatted + "\n" prio_line := "PRIORITY=0" + "\n" // Zeilen Schreiben // Header _, err = file.WriteString(branch_line) if err != nil { return err } _, err = file.WriteString(date_line) if err != nil { return err } _, err = file.WriteString(prio_line) if err != nil { return err } _, err = file.WriteString("\n") if err != nil { return err } // Router Lines for i := range files { size_str := strconv.Itoa(int(files[i].Size)) line := files[i].Model + " " + files[i].Release + " " + files[i].Checksum + " " + size_str + " " + files[i].Name + "\n" _, err := file.WriteString(line) if err != nil { return err } } log.Println("Manifest file " + release[0] + ".manifest" + " generated") return err }