diff --git a/cmd/ultim8/main.go b/cmd/ultim8/main.go index fdbc7a0..2e5b948 100644 --- a/cmd/ultim8/main.go +++ b/cmd/ultim8/main.go @@ -1,8 +1,11 @@ package main import ( + "bufio" + "errors" "flag" "fmt" + "io" "log" "os" "time" @@ -30,17 +33,24 @@ func main() { flag.Parse() ultim8.DialTimeout = time.Duration(timeoutSeconds) * time.Second n := flag.NArg() - if n == 1 { + if n < 1 { printUsage() return } - path := flag.Args()[0] - u, err := ultim8.New(address) if err != nil { log.Fatalf("ultim8.New %q failed: %v", address, err) } defer u.Close() + + if n > 1 { + if err = processMulti(u, flag.Args(), mount); err != nil { + log.Fatalf("processMulti %v failed: %v", flag.Args(), err) + } + return + } + + path := flag.Args()[0] if err = process(u, path, mount); err != nil { log.Fatalf("process %q failed: %v", path, err) } @@ -64,3 +74,43 @@ func process(u *ultim8.Manager, path string, mount bool) error { } return nil } + +func processMulti(u *ultim8.Manager, files []string, mount bool) error { + fmt.Printf("Multi mode, starting image %q\n", files[0]) + f, err := os.Open(files[0]) + if err != nil { + return fmt.Errorf("os.Open %q failed: %v", files[0], err) + } + defer f.Close() + if mount { + if err = u.Mount(f); err != nil { + return fmt.Errorf("u.Mount failed: %v", err) + } + } else { + if err = u.Run(f); err != nil { + return fmt.Errorf("u.Run failed: %v", err) + } + } + + r := bufio.NewReader(os.Stdin) + for i := 1; i < len(files); i++ { + fmt.Printf("Press enter to mount next image %q", files[i]) + _, err = r.ReadString('\n') + switch { + case errors.Is(err, io.EOF): + fmt.Println("EOF") + return nil + case err != nil: + return fmt.Errorf("r.ReadString failed: %w", err) + } + f, err := os.Open(files[i]) + if err != nil { + return fmt.Errorf("r.ReadString failed: %w", err) + } + defer f.Close() + if err = u.Mount(f); err != nil { + return fmt.Errorf("u.Mount %q failed: %w", files[i], err) + } + } + return nil +}