/
main.go
237 lines (192 loc) · 4.38 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// @author Couchbase <info@couchbase.com>
// @copyright 2015-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included in
// the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
// file, in accordance with the Business Source License, use of this software
// will be governed by the Apache License, Version 2.0, included in the file
// licenses/APL2.txt.
package main
import (
"archive/zip"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const (
exitSuccess = 0
exitFailure = 1
)
var (
zipPath string
paths []string
prefix string
recursive bool
stripPath bool
name string
f *flag.FlagSet
errorNotRecursive = errors.New("")
)
func usage(code int) {
fmt.Fprintln(os.Stderr, "Usage:")
fmt.Fprintf(os.Stderr, " %s [options] zipfile [file ...]\n", name)
fmt.Fprintln(os.Stderr, "Options:")
f.SetOutput(os.Stderr)
f.PrintDefaults()
os.Exit(code)
}
func maybeAddExt(name string) string {
if filepath.Ext(name) == ".zip" {
return name
} else {
return name + ".zip"
}
}
func fatal(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(exitFailure)
}
func zipifyPath(path string) string {
path = filepath.Clean(path)
volume := filepath.VolumeName(path)
if volume != "" {
path = path[len(volume):]
}
return filepath.ToSlash(strings.TrimPrefix(path, "/"))
}
type walkFn func(string, *os.File, os.FileInfo) error
func walk(root string, fn walkFn) error {
file, err := os.Open(root)
if err != nil {
return err
}
defer file.Close()
info, err := file.Stat()
if err != nil {
return err
}
err = fn(root, file, info)
if err != nil {
return err
}
if info.IsDir() {
children, err := file.Readdirnames(0)
if err != nil {
return err
}
for _, child := range children {
err = walk(filepath.Join(root, child), fn)
if err != nil {
return err
}
}
}
return nil
}
func isRegular(info os.FileInfo) bool {
return info.Mode()&os.ModeType == 0
}
func compress() {
zipFile, err := os.Create(zipPath)
if err != nil {
fatal("Couldn't create output file: %s", err.Error())
}
defer zipFile.Close()
err = doCompress(zipFile)
if err != nil {
fatal("%s", err.Error())
}
}
func doCompress(zipFile *os.File) error {
zipInfo, err := zipFile.Stat()
if err != nil {
return err
}
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
fn := func(path string, f *os.File, info os.FileInfo) error {
if os.SameFile(zipInfo, info) || !(isRegular(info) || info.IsDir()) {
fmt.Fprintf(os.Stderr, "skipping %s\n", path)
return nil
}
zippedPath := path
if stripPath {
zippedPath = filepath.Base(zippedPath)
}
if prefix != "" {
zippedPath = filepath.Join(prefix, zippedPath)
}
zippedPath = zipifyPath(zippedPath)
var w io.Writer
if (zippedPath != "" && zippedPath != ".") || !info.IsDir() {
if info.IsDir() {
zippedPath += "/"
}
fmt.Fprintf(os.Stderr, "adding: %s -> %s\n", path, zippedPath)
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = zippedPath
header.Method = zip.Deflate
w, err = zipWriter.CreateHeader(header)
if err != nil {
return err
}
}
if info.IsDir() {
if recursive {
return nil
} else {
return errorNotRecursive
}
}
_, err = io.Copy(w, f)
if err != nil {
return fmt.Errorf("failed to copy %s: %s", path, err.Error())
}
return nil
}
for _, path := range paths {
err = walk(path, fn)
if err != nil && err != errorNotRecursive {
return err
}
}
return nil
}
func main() {
name = os.Args[0]
f = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
f.SetOutput(ioutil.Discard)
f.BoolVar(&recursive, "recursive", false, "scan directories recursively")
f.BoolVar(&stripPath, "strip-path", false, "store just file names")
f.StringVar(&prefix, "prefix", "", "prepend each path with prefix")
err := f.Parse(os.Args[1:])
if err == nil {
switch {
case recursive && stripPath:
err = fmt.Errorf("-recursive and -strip-path are mutually exclusive")
case f.NArg() == 0:
err = fmt.Errorf("output zip file is not specified")
default:
zipPath = maybeAddExt(f.Arg(0))
paths = f.Args()[1:]
}
}
if err != nil {
switch err {
case flag.ErrHelp:
usage(exitSuccess)
default:
fmt.Fprintln(os.Stderr, err)
usage(exitFailure)
}
}
compress()
}