From 39ac2ae2acdde12716f8e8b86760afa9fabea82a Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Thu, 27 May 2021 06:04:17 +0700 Subject: [PATCH] Cleanup wim package for use on non Windows platforms Cleans up the wim package (removing unnecessary build tag guards), and cleans up the wim/validate command to be slightly more "Go idiomatic". --- wim/decompress.go | 2 -- wim/validate/noop.go | 5 ----- wim/validate/validate.go | 46 +++++++++++++++++++--------------------- wim/wim.go | 2 -- 4 files changed, 22 insertions(+), 33 deletions(-) delete mode 100644 wim/validate/noop.go diff --git a/wim/decompress.go b/wim/decompress.go index 12d5196b..f4e67f84 100644 --- a/wim/decompress.go +++ b/wim/decompress.go @@ -1,5 +1,3 @@ -// +build windows - package wim import ( diff --git a/wim/validate/noop.go b/wim/validate/noop.go deleted file mode 100644 index bf98ceb4..00000000 --- a/wim/validate/noop.go +++ /dev/null @@ -1,5 +0,0 @@ -// +build !windows - -package main - -func main() {} diff --git a/wim/validate/validate.go b/wim/validate/validate.go index 2536c002..1e121777 100644 --- a/wim/validate/validate.go +++ b/wim/validate/validate.go @@ -1,9 +1,6 @@ -// +build windows - package main import ( - "flag" "fmt" "os" @@ -11,41 +8,42 @@ import ( ) func main() { - flag.Parse() - f, err := os.Open(flag.Arg(0)) - if err != nil { - panic(err) + if len(os.Args) < 2 { + fmt.Fprintf(os.Stderr, "usage: %s \n", os.Args[0]) + os.Exit(1) } + if err := run(os.Args[1]); err != nil { + fmt.Fprintf(os.Stderr, "error: %v\n", err) + os.Exit(1) + } +} - w, err := wim.NewReader(f) +func run(file string) error { + f, err := os.OpenFile(file, os.O_RDONLY, 0) if err != nil { - panic(err) - + return err } - - fmt.Printf("%#v\n%#v\n", w.Image[0], w.Image[0].Windows) - - dir, err := w.Image[0].Open() + r, err := wim.NewReader(f) if err != nil { - panic(err) + return err } - - err = recur(dir) + fmt.Fprintf(os.Stdout, "%#v\n%#v\n", r.Image[0], r.Image[0].Windows) + dir, err := r.Image[0].Open() if err != nil { - panic(err) + return err } + return recur(dir) } -func recur(d *wim.File) error { - files, err := d.Readdir() +func recur(dir *wim.File) error { + files, err := dir.Readdir() if err != nil { - return fmt.Errorf("%s: %s", d.Name, err) + return fmt.Errorf("cannot read %q: %w", dir.Name, err) } for _, f := range files { if f.IsDir() { - err = recur(f) - if err != nil { - return fmt.Errorf("%s: %s", f.Name, err) + if err := recur(f); err != nil { + return fmt.Errorf("cannot recurse directory %q: %w", f.Name, err) } } } diff --git a/wim/wim.go b/wim/wim.go index e3ca2f18..1d02e920 100644 --- a/wim/wim.go +++ b/wim/wim.go @@ -1,5 +1,3 @@ -// +build windows - // Package wim implements a WIM file parser. // // WIM files are used to distribute Windows file system and container images.