forked from loosheng/drone-zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
176 lines (145 loc) · 3.09 KB
/
plugin.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
package main
import (
"io"
"math"
"os"
"runtime"
"sync"
glob "github.com/bmatcuk/doublestar/v4"
"github.com/klauspost/compress/zip"
"github.com/sirupsen/logrus"
)
type Plugin struct {
Input []string
Output string
}
func (p Plugin) Exec() error {
if len(p.Input) == 0 {
logrus.Fatalf("please enter the file or directory to be packed")
}
if p.Output == "" {
logrus.Fatalf("please enter the zip output path")
}
var (
input []string
)
for _, inputPath := range p.Input {
filePath := getFilePaths(inputPath)
input = append(input, filePath...)
}
logrus.Infof("glob path: %s", input)
Zip(p.Output, input)
return nil
}
func Zip(fileName string, inputList []string) {
fw, err := os.Create(fileName)
if err != nil {
logrus.Fatalf("create %s error: %v", fileName, err)
}
defer func(fw *os.File) {
err := fw.Close()
if err != nil {
logrus.Errorf("close %s error: %v", fileName, err)
}
}(fw)
w := zip.NewWriter(fw)
defer func(w *zip.Writer) {
err := w.Close()
if err != nil {
logrus.Errorf("close %s error: %v", fileName, err)
}
}(w)
// set maxGoroutines
cpu80Percent := int(math.Ceil(float64(runtime.NumCPU()) * 0.8))
fileCount := len(inputList)
maxGoroutines := getMin(cpu80Percent, fileCount)
sem := make(chan struct{}, maxGoroutines)
errCh := make(chan error, fileCount)
quit := make(chan struct{})
wg := &sync.WaitGroup{}
for _, input := range inputList {
select {
case <-quit:
return
default:
wg.Add(1)
sem <- struct{}{}
go func(filePath string) {
defer wg.Done()
defer func() { <-sem }() // Release semaphore.
if err := addFileToZip(w, filePath); err != nil {
select {
case errCh <- err:
case <-quit: // If other goroutines have already detected an error, do not send.
}
}
}(input)
}
}
go func() {
wg.Wait()
close(errCh)
}()
for err := range errCh {
if err != nil {
close(quit) // When an error is detected, notify all goroutines to stop.
logrus.Fatalf("%v", err)
return
}
}
}
func addFileToZip(w *zip.Writer, filePath string) error {
targetFile, err := w.Create(filePath)
if err != nil {
return err
}
sourceFile, err := os.Open(filePath)
if err != nil {
return err
}
defer func(sourceFile *os.File) {
err := sourceFile.Close()
if err != nil {
}
}(sourceFile)
_, err = io.Copy(targetFile, sourceFile)
if err != nil {
return err
}
logrus.Infof("compression %s file success", filePath)
return nil
}
func getMin(a, b int) int {
if a < b {
return a
}
return b
}
func IsDir(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.Mode().IsDir()
}
func getFilePaths(path string) []string {
var paths, resultPaths []string
var patternPath string
if IsDir(path) {
patternPath = path + "/**/*"
} else {
patternPath = path
}
globedPaths, err := glob.FilepathGlob(patternPath)
if err != nil {
logrus.Fatalf("glob error: %v", err)
}
paths = append(paths, globedPaths...)
// remove directory
for _, path := range paths {
if !IsDir(path) {
resultPaths = append(resultPaths, path)
}
}
return resultPaths
}