go-gluon-publisher/manifest.go
Stefan 8d77819f57
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
Added target version
2023-06-02 19:04:31 +02:00

110 lines
2.5 KiB
Go

package main
import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/codingsince1985/checksum"
)
func generate_manifest(dir string) error {
sysdir := dir + "sysupgrade/"
files, err := get_files(sysdir)
if err != nil {
log.Println("Error reading Image Files in folder: ", sysdir)
return err
}
file_struct := split_filenames(files, sysdir)
build_manifest(file_struct, sysdir)
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, "-")
if split[0] != "gluon" {
log.Println("skipping ", current_name)
continue
}
split_name := len(split) - 1
// Routername
router_name := strings.Join(split[4:split_name], "-")
//RELEASE
router_release := strings.Join(split[2:4], "-")
//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[1] + ".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[1] + "\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[1] + ".manifest" + " generated")
return err
}