-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
130 lines (101 loc) · 2.66 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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/ziputil"
"github.com/bitrise-tools/go-steputils/stepconf"
)
type config struct {
SourcePath string `env:"source_path,file"`
Destination string `env:"destination"`
}
func main() {
var cfg config
if err := stepconf.Parse(&cfg); err != nil {
log.Errorf("Error: %s\n", err)
os.Exit(1)
}
stepconf.Print(cfg)
destination := cfg.Destination
destination, err := fixDestination(destination, cfg.SourcePath)
if err != nil {
failf("Issue with compress: %s", err)
}
if err := checkAlreadyExist(destination); err != nil {
failf("Issue with compress: %s", err)
}
if err := ensureZIP(cfg.SourcePath, destination); err != nil {
failf("Issue with compress: %s", err)
}
}
func ensureZIP(sourcePath string, destination string) error {
info, err := os.Lstat(sourcePath)
if err != nil {
return err
}
if info.IsDir() {
return ziputil.ZipDir(sourcePath, destination, false)
}
return ziputil.ZipFile(sourcePath, destination)
}
// checkAlreadyExist will return an error if the zip has already exist at the destination.
func checkAlreadyExist(destination string) error {
targetName := filepath.Base(destination)
exist, err := pathutil.IsPathExists(destination)
if err != nil {
return err
}
if exist {
return fmt.Errorf("The - %s - already exists at location: %s", targetName, destination)
}
return nil
}
func fixDestination(destination string, sourcePath string) (string, error) {
destination = cleanDestination(destination)
if err := ensureDestinationPath(destination); err != nil {
return "", err
}
isDir, err := checkDestinationIsDir(destination)
if err != nil {
return "", err
}
if isDir {
destination = filepath.Join(destination, filepath.Base(sourcePath))
}
destination = fixDestinationExt(destination)
return destination, nil
}
func cleanDestination(destination string) string {
return filepath.Clean(destination)
}
func fixDestinationExt(destination string) string {
if filepath.Ext(destination) != "zip" {
destination += ".zip"
}
return destination
}
func ensureDestinationPath(destination string) error {
dirOftargetPath := filepath.Dir(destination)
return os.MkdirAll(dirOftargetPath, 0755)
}
func checkDestinationIsDir(destination string) (bool, error) {
exist, err := pathutil.IsPathExists(destination)
if err != nil {
return false, err
}
if !exist {
return false, nil
}
info, err := os.Lstat(destination)
if err != nil {
return false, err
}
return info.IsDir(), nil
}
func failf(format string, v ...interface{}) {
log.Errorf(format, v...)
os.Exit(1)
}