-
Notifications
You must be signed in to change notification settings - Fork 1
/
compression.go
105 lines (94 loc) · 2.16 KB
/
compression.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package building
import (
"fmt"
"io"
"os"
"path/filepath"
)
type archive interface {
Write(w io.Writer, level int, dst string, srcs []fileset) error
}
type compression struct {
output io.Writer
filesets []fileset
archive archive
level int
}
func makeCompression(a archive, args []string) compression {
c := compression{
archive: a,
level: -1,
}
if len(args) > 0 {
c.Run(args[0], args[1:]...)
}
return c
}
func (t compression) WithOutput(w io.Writer) compression {
t.output = w
return t
}
// WithFiles adds files to compress.
func (t compression) WithFiles(paths ...string) compression {
t.filesets = append(t.filesets, fileset{
includes: paths,
})
return t
}
// WithFileset adds a fileset to compress.
func (t compression) WithFileset(dir, includes, excludes string) compression {
t.filesets = append(t.filesets, makeFileset(dir, includes, excludes))
return t
}
// WithLevel sets the compression level from 0 (no compression) to 9 (best compression).
// -1 can be used for default compression level.
func (t compression) WithLevel(level int) compression {
if level < -1 && level > 9 {
b.Fatalln("invalid compression level", level)
}
t.level = level
return t
}
func (t compression) Run(dst string, args ...string) {
if len(args) > 0 {
t.filesets = append(t.filesets, fileset{
includes: args,
})
}
if t.output == nil {
t.output = os.Stdout
}
if err := compress(t.archive, t.output, t.level, dst, t.filesets...); err != nil {
b.Fatalln(err)
}
}
func compress(a archive, w io.Writer, level int, dst string, srcs ...fileset) error {
fs, err := resolve(srcs, true)
if err != nil {
return err
}
if len(fs) == 0 {
return fmt.Errorf("needs at least one file")
}
if dst != "-" {
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
return err
}
}
return a.Write(w, level, dst, fs)
}
func walk(fs []fileset, fn func(path, rel string, info os.FileInfo) error) error {
for _, f := range fs {
err := f.walk(func(path, rel string, info os.FileInfo, err error) error {
if err != nil {
return err
}
b.Debugln("compressing", rel)
return fn(path, rel, info)
})
if err != nil {
return err
}
}
return nil
}