package main
import (
"errors"
"fmt"
"io"
"os"
"cyphar.com/go-pathrs"
)
func Main(args ...string) error {
if len(args) != 2 {
fmt.Fprintln(os.Stderr, "usage: cat <root> <unsafe-path>")
os.Exit(1)
}
rootPath, unsafePath := args[0], args[1]
root, err := pathrs.OpenRoot(rootPath)
if err != nil {
return fmt.Errorf("open root %q: %w", rootPath, err)
}
defer root.Close()
file, err := root.Open(unsafePath)
if err != nil {
return fmt.Errorf("open %q: %w", unsafePath, err)
}
defer file.Close()
fmt.Fprintf(os.Stderr, "== file %q (from root %q) ==\n", file.Name(), root.IntoFile().Name())
if _, err := io.Copy(os.Stdout, file); err != nil {
return fmt.Errorf("copy file contents to stdout: %w", err)
}
return nil
}
func main() {
if err := Main(os.Args[1:]...); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
fmt.Fprintf(os.Stderr, "Source: %v\n", errors.Unwrap(err))
os.Exit(1)
}
}