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
95 lines (87 loc) · 1.78 KB
/
Copy pathup.go
File metadata and controls
95 lines (87 loc) · 1.78 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
package controller
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"os"
"path/filepath"
"github.com/monochromegane/go-gitignore"
"github.com/railwayapp/cli/entity"
)
func compress(src string, buf io.Writer) error {
// tar > gzip > buf
zr := gzip.NewWriter(buf)
tw := tar.NewWriter(zr)
ignore, err := gitignore.NewGitIgnore(".gitignore", ".")
if err != nil {
return err
}
// walk through every file in the folder
filepath.Walk(src, func(file string, fi os.FileInfo, err error) error {
if ignore.Match(file, fi.IsDir()) {
return nil
}
// generate tar header
header, err := tar.FileInfoHeader(fi, file)
if err != nil {
return err
}
if err != nil {
return err
}
// must provide real name
// (see https://golang.org/src/archive/tar/common.go?#L626)
header.Name = filepath.ToSlash(file)
// write header
if err := tw.WriteHeader(header); err != nil {
return err
}
// if not a dir, write file content
if !fi.IsDir() {
data, err := os.Open(file)
if err != nil {
return err
}
if _, err := io.Copy(tw, data); err != nil {
return err
}
}
return nil
})
// 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) Up(ctx context.Context) error {
projectID, err := c.cfg.GetProject()
if err != nil {
return err
}
environmentID, err := c.cfg.GetEnvironment()
if err != nil {
return err
}
var buf bytes.Buffer
if err := compress("./", &buf); err != nil {
return err
}
res, err := c.gtwy.Up(ctx, &entity.UpRequest{
Data: buf,
ProjectID: projectID,
EnvironmentID: environmentID,
})
if err != nil {
return err
}
fmt.Printf("Deploy available at %s\n", res.URL)
return nil
}