From 1488d4b081b6b2cf6716e358a3473bc4d3f71a55 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Sat, 1 Dec 2018 20:18:43 +0000 Subject: [PATCH] Implements fallback if rootfs image not found --- config/config.go | 6 ++++++ main.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/config/config.go b/config/config.go index 7e7c3d5..6fa3338 100644 --- a/config/config.go +++ b/config/config.go @@ -150,6 +150,12 @@ func (dt DistroType) MarshalTOML() (data []byte, err error) { return } +type ByRootFS []KernelInfo + +func (a ByRootFS) Len() int { return len(a) } +func (a ByRootFS) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByRootFS) Less(i, j int) bool { return a[i].RootFS < a[j].RootFS } + type KernelInfo struct { DistroType DistroType DistroRelease string // 18.04/7.4.1708/9.1 diff --git a/main.go b/main.go index e5e7afe..fc3df77 100644 --- a/main.go +++ b/main.go @@ -9,12 +9,46 @@ import ( "os" "os/exec" "os/user" + "sort" kingpin "gopkg.in/alecthomas/kingpin.v2" "github.com/jollheef/out-of-tree/config" ) +func findFallback(kcfg config.KernelConfig, ki config.KernelInfo) (rootfs string) { + for _, k := range kcfg.Kernels { + if !exists(k.RootFS) || k.DistroType != ki.DistroType { + continue + } + if k.RootFS < ki.RootFS { + rootfs = k.RootFS + return + } + } + return +} + +func handleFallbacks(kcfg config.KernelConfig) { + sort.Sort(sort.Reverse(config.ByRootFS(kcfg.Kernels))) + + for i, k := range kcfg.Kernels { + if !exists(k.RootFS) { + newRootFS := findFallback(kcfg, k) + + s := k.RootFS + " does not exists " + if newRootFS != "" { + s += "(fallback to " + newRootFS + ")" + } else { + s += "(no fallback found)" + } + + kcfg.Kernels[i].RootFS = newRootFS + log.Println(s) + } + } +} + func main() { app := kingpin.New( "out-of-tree", @@ -109,6 +143,8 @@ func main() { } } + handleFallbacks(kcfg) + switch kingpin.MustParse(app.Parse(os.Args[1:])) { case pewCommand.FullCommand(): err = pewHandler(kcfg, *path, *pewKernel, *pewBinary,