go-gluon-publisher/manifest.go

110 lines
2.5 KiB
Go
Raw Permalink Normal View History

2023-05-29 19:33:50 +00:00
package main
import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/codingsince1985/checksum"
)
func generate_manifest(dir string) error {
2023-05-29 20:27:28 +00:00
sysdir := dir + "sysupgrade/"
files, err := get_files(sysdir)
2023-05-29 19:33:50 +00:00
if err != nil {
2023-05-29 20:27:28 +00:00
log.Println("Error reading Image Files in folder: ", sysdir)
2023-05-29 19:33:50 +00:00
return err
}
2023-05-29 20:27:28 +00:00
file_struct := split_filenames(files, sysdir)
build_manifest(file_struct, sysdir)
2023-05-29 19:33:50 +00:00
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" {
2023-05-30 17:57:42 +00:00
log.Println("skipping ", current_name)
continue
}
2023-05-29 19:33:50 +00:00
split_name := len(split) - 1
// Routername
2023-05-30 20:00:11 +00:00
router_name := strings.Join(split[4:split_name], "-")
2023-05-29 19:33:50 +00:00
//RELEASE
2023-05-30 20:00:11 +00:00
router_release := strings.Join(split[2:4], "-")
2023-05-29 19:33:50 +00:00
//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
}