forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathup.go
More file actions
206 lines (174 loc) · 3.88 KB
/
Copy pathup.go
File metadata and controls
206 lines (174 loc) · 3.88 KB
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
package controller
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/railwayapp/cli/entity"
gitignore "github.com/railwayapp/cli/gateway"
)
var validIgnoreFile = map[string]bool{
".gitignore": true,
".railwayignore": true,
}
var skipDirs = []string{
".git",
"node_modules",
}
type ignoreFile struct {
prefix string
ignore *gitignore.GitIgnore
}
func scanIgnoreFiles(src string) ([]ignoreFile, error) {
ignoreFiles := []ignoreFile{}
if err := filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
// no sense scanning for ignore files in skipped dirs
for _, s := range skipDirs {
if filepath.Base(path) == s {
return filepath.SkipDir
}
}
return nil
}
fname := filepath.Base(path)
if validIgnoreFile[fname] {
igf, err := gitignore.CompileIgnoreFile(path)
if err != nil {
return err
}
prefix := filepath.Dir(path)
if prefix == "." {
prefix = "" // Handle root dir properly.
}
ignoreFiles = append(ignoreFiles, ignoreFile{
prefix: prefix,
ignore: igf,
})
}
return nil
}); err != nil {
return nil, err
}
return ignoreFiles, nil
}
func compress(src string, buf io.Writer) error {
// tar > gzip > buf
zr := gzip.NewWriter(buf)
tw := tar.NewWriter(zr)
// find all ignore files, including those in subdirs
ignoreFiles, err := scanIgnoreFiles(src)
if err != nil {
return err
}
// walk through every file in the folder
err = filepath.WalkDir(src, func(file string, de os.DirEntry, passedErr error) error {
if passedErr != nil {
return err
}
if de.IsDir() {
// skip directories if we can (for perf)
// e.g., want to avoid walking node_modules dir
for _, s := range skipDirs {
if filepath.Base(file) == s {
return filepath.SkipDir
}
}
return nil
}
// follow symlinks by default
ln, err := filepath.EvalSymlinks(file)
if err != nil {
return err
}
// get info about the file the link points at
fi, err := os.Lstat(ln)
if err != nil {
return err
}
for _, igf := range ignoreFiles {
if strings.HasPrefix(file, igf.prefix) { // if ignore file applicable
trimmed := strings.TrimPrefix(file, igf.prefix)
if igf.ignore.MatchesPath(trimmed) {
return nil
}
}
}
// read file into a buffer to prevent tar overwrites
data := bytes.NewBuffer(nil)
f, err := os.Open(ln)
if err != nil {
return err
}
_, err = io.Copy(data, f)
if err != nil {
return err
}
// close the file to avoid hitting fd limit
if err := f.Close(); err != nil {
return err
}
// generate tar headers
header, err := tar.FileInfoHeader(fi, ln)
if err != nil {
return err
}
// must provide real name
// (see https://golang.org/src/archive/tar/common.go?#L626)
header.Name = filepath.ToSlash(file)
// size when we first observed the file
header.Size = int64(data.Len())
// write header
if err := tw.WriteHeader(header); err != nil {
return err
}
// not a dir, write file content
if _, err := io.Copy(tw, data); err != nil {
return err
}
return err
})
if err != nil {
return err
}
// produce tar
if err := tw.Close(); err != nil {
return err
}
// produce gzip
if err := zr.Close(); err != nil {
return err
}
return nil
}
func (c *Controller) Upload(
ctx context.Context,
req *entity.UploadRequest,
) (*entity.UpResponse, error) {
var buf bytes.Buffer
if err := compress(req.RootDir, &buf); err != nil {
return nil, err
}
res, err := c.gtwy.Up(ctx, &entity.UpRequest{
Data: buf,
ProjectID: req.ProjectID,
EnvironmentID: req.EnvironmentID,
ServiceID: req.ServiceID,
})
if err != nil {
return nil, err
}
return res, nil
}
func (c *Controller) GetFullUrlFromStaticUrl(staticUrl string) string {
return fmt.Sprintf("https://%s", staticUrl)
}