-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
84 lines (70 loc) · 1.46 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"context"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"strings"
"github.com/jonjohnsonjr/targz/gsip"
"github.com/jonjohnsonjr/targz/ranger"
"github.com/jonjohnsonjr/targz/tarfs"
)
func main() {
if err := run(os.Args[1:]); err != nil {
log.Fatal(err)
}
}
func run(args []string) error {
if strings.HasPrefix(args[0], "http") {
resp, err := http.Head(args[0])
if err != nil {
return err
}
rra := ranger.New(context.TODO(), args[0], http.DefaultTransport)
zr, err := gsip.NewReader(rra, resp.ContentLength)
if err != nil {
return err
}
fsys, err := tarfs.New(zr, resp.ContentLength)
if err != nil {
return err
}
return fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
fmt.Println(p)
return nil
})
}
f, err := os.Open(args[0])
if err != nil {
return err
}
info, err := f.Stat()
if err != nil {
return err
}
zr, err := gsip.NewReader(f, info.Size())
if err != nil {
return err
}
// We don't know the size until we decompress it.
fsys, err := tarfs.New(zr, 1<<63-1)
if err != nil {
return err
}
if len(args) == 1 {
return fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
if p == "." {
return nil
}
e, err := fsys.Entry(p)
if err != nil {
return fmt.Errorf("Entry(%q): %w", p, err)
}
fmt.Printf("%d+%d: %s\n", e.Offset, e.Size(), p)
return nil
})
}
return http.ListenAndServe(args[1], http.FileServer(http.FS(fsys)))
}