package main
import (
"errors"
"fmt"
"io"
"os"
"../../contrib/bindings/go/pathrs"
)
func usage() {
fmt.Println("usage: cat <root> <unsafe-path>")
os.Exit(1)
}
func main() {
if len(os.Args) < 3 {
usage()
}
rootPath := os.Args[1]
path := os.Args[2]
root, err := pathrs.Open(rootPath)
if err != nil {
printPathError(err)
}
defer root.Close()
handle, err := root.Resolve(path)
if err != nil {
printPathError(err)
}
defer handle.Close()
file, err := handle.Open()
if err != nil {
printPathError(err)
}
defer file.Close()
_, err = io.Copy(os.Stdout, file)
if err != nil {
fmt.Printf("Cannot write content of file to stdout, %v\n", err)
os.Exit(1)
}
}
func printPathError(err error) {
fmt.Println("Error", err)
fmt.Println("Unwrapped error", errors.Unwrap(err))
fmt.Println("Backtrace:")
if pathrsErr, ok := err.(*pathrs.Error); ok {
fmt.Println(pathrsErr.Backtrace())
}
os.Exit(1)
}