-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy patharprint.go
56 lines (52 loc) · 1.54 KB
/
arprint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Print the names and contents of the files in a tar or zip archive.
// Only makes sense for archives containing only text files.
//
// The archive/tar API doesn't presume random access, and I can't figure out
// how I could present a facade for tars and zips that gives random access to
// individual files in the archive without using syscall.Dup or opening a bunch
// of file descriptors that could possibly be to different files if the
// filesystem changed. I started out with a program that just detected and
// printed archive types, which seemed to satisfy most of the spirit of the
// exercise, but I decided to go a bit further and print file contents.
// Wrapping an io.Reader like in zip/zip.go and tar/tar.go is awkward and not
// that useful, since it basically assumes none of the files are binary.
package main
import (
"fmt"
"io"
"log"
"os"
arprint "github.com/torbiak/gopl/ex10.2"
_ "github.com/torbiak/gopl/ex10.2/tar"
_ "github.com/torbiak/gopl/ex10.2/zip"
)
func printArchive(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
r, err := arprint.Open(f)
if err != nil {
return fmt.Errorf("open archive reader: %s", err)
}
_, err = io.Copy(os.Stdout, r)
if err != nil {
return fmt.Errorf("printing: %s", err)
}
return nil
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "usage: arprint FILE ...")
}
exitCode := 0
for _, filename := range os.Args[1:] {
err := printArchive(filename)
if err != nil {
log.Print(err)
exitCode = 2
}
}
os.Exit(exitCode)
}