-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.go
187 lines (149 loc) · 3.24 KB
/
pack.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
package main
import (
"bufio"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/git-lfs/wildmatch"
"github.com/mholt/archiver/v3"
"github.com/urfave/cli/v2"
)
type list map[string]struct{}
var ignoreList cli.StringSlice
func main() {
app := &cli.App{
Name: "pack",
Usage: "Pack create archives while ignoring any hidden or unnecessary files and folders",
ArgsUsage: "<src> <dest>",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "ignore",
Aliases: []string{"ig"},
Usage: "Ignore list from given files",
Destination: &ignoreList,
},
},
Action: pack,
}
if err := app.Run(os.Args); err != nil && err != io.EOF {
panic(err)
}
}
func pack(c *cli.Context) error {
if c.NArg() != 2 {
return cli.ShowAppHelp(c)
}
src, dest := c.Args().Get(0), c.Args().Get(1)
if src == "." || src == "./" {
cwd, err := os.Getwd()
if err != nil {
return err
}
src = cwd
}
accept, err := files(src, ignoreList.Value())
if err != nil {
return err
}
tmp, err := ioutil.TempDir("", "pack")
if err != nil {
return err
}
defer os.RemoveAll(tmp)
rootDir := accept[0]
basePath, err := createRootDir(dest, tmp)
if err != nil {
return err
}
for _, v := range accept {
if v == rootDir {
continue
}
newPath := basePath + strings.Replace(v, rootDir, "", 1)
if err := dupe(v, newPath); err != nil {
return err
}
}
return archiver.Archive([]string{basePath}, dest)
}
func files(path string, ign []string) ([]string, error) {
var accept []string
avoid := ignore(ign)
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
path = filepath.Clean(path)
if isHidden(path) {
if isDir(path) {
return filepath.SkipDir
}
return nil
}
for pattern := range avoid {
wm := wildmatch.NewWildmatch(pattern, wildmatch.Basename)
if wm.Match(path) {
if isDir(path) {
return filepath.SkipDir
}
return nil
}
}
accept = append(accept, path)
return nil
})
return accept, err
}
func ignore(files []string) list {
var avoid = make(list)
for _, file := range files {
path := parsePath(file)
func() {
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
avoid[filepath.Clean(scanner.Text())] = struct{}{}
}
}()
}
return avoid
}
func isHidden(path string) bool {
if len(path) == 0 {
return true
}
base := filepath.Base(path)
return len(base) > 0 && base[0] == '.'
}
func parsePath(path string) string {
homeDir, err := os.UserHomeDir()
if err != nil {
return filepath.Clean(path)
}
return filepath.Clean(strings.ReplaceAll(path, "~", homeDir))
}
func isDir(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return false
}
return fi.Mode().IsDir()
}
func createRootDir(dest, path string) (string, error) {
name, ext := filepath.Base(dest), filepath.Ext(dest)
basePath := path + "/" + strings.Replace(name, ext, "", 1)
return basePath, exec.Command("mkdir", "-p", basePath).Run()
}
func dupe(src, dest string) error {
if isDir(src) {
return exec.Command("mkdir", "-p", dest).Run()
}
return exec.Command("cp", "-rf", src, dest).Run()
}