forked from naaman/slug
/
slug.go
191 lines (159 loc) · 4.18 KB
/
slug.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
188
189
190
191
package slug
import (
"archive/tar"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/naaman/pf"
)
type ProcessTable struct {
ProcessTypes map[string]string `json:"process_types"`
}
type Slug struct {
Blob map[string]string `json:"blob"`
Commit *string `json:"commit"`
CreatedAt time.Time `json:"created_at"`
Id string `json:"id"`
ProcessTypes map[string]string `json:"process_types"`
UpdatedAt time.Time `json:"updated_at"`
slugDir string
httpClient *http.Client
tarFile *os.File
apiKey string
appName string
}
type Release struct {
Version int `json:"version"`
}
func NewSlug(apiKey, appName, slugDir string) *Slug {
slugJson := &Slug{}
slugJson.apiKey = apiKey
slugJson.appName = appName
slugJson.slugDir = slugDir
client := &http.DefaultClient
res, _ := client.Do(slugJson.createSlug())
bod, _ := ioutil.ReadAll(res.Body)
defer res.Body.Close()
json.Unmarshal(bod, &slugJson)
slugJson.httpClient = *client
slugJson.slugDir = slugDir
return slugJson
}
func (s *Slug) SetArchive(f *os.File) {
s.tarFile = f
}
func (s *Slug) Archive() *os.File {
s.tarFile = tarGz(strings.TrimRight(s.slugDir, "/"))
return s.tarFile
}
func (s *Slug) Push() error {
_, err := s.httpClient.Do(s.putSlug())
defer s.tarFile.Close()
return err
}
func (s *Slug) Release() *Release {
res, _ := s.httpClient.Do(s.createRelease())
bod, _ := ioutil.ReadAll(res.Body)
defer res.Body.Close()
releaseJson := &Release{}
json.Unmarshal(bod, &releaseJson)
return releaseJson
}
func (s *Slug) herokuReq(method string, resource string, body string) *http.Request {
reqUrl := fmt.Sprintf("https://api.heroku.com/apps/%s/%s", s.appName, resource)
req, _ := http.NewRequest(method, reqUrl, strings.NewReader(body))
req.Header.Add("Accept", "application/vnd.heroku+json; version=3")
req.SetBasicAuth("", s.apiKey)
return req
}
func (s *Slug) herokuPost(resource string, body string) *http.Request {
req := s.herokuReq("POST", resource, body)
req.Header.Add("Content-Type", "application/json")
return req
}
func (s *Slug) createSlug() *http.Request {
procTable := new(ProcessTable)
procTable.ProcessTypes = make(map[string]string)
procfile := s.parseProcfile()
for _, e := range procfile.Entries {
procTable.ProcessTypes[e.Type] = e.Command
}
procTableJson, _ := json.Marshal(procTable)
return s.herokuPost("slugs", string(procTableJson))
}
func (s *Slug) createRelease() *http.Request {
slugJson := fmt.Sprintf(`{"slug":"%s"}`, s.Id)
return s.herokuPost("releases", slugJson)
}
func (s *Slug) putSlug() *http.Request {
tarFileStat, err := s.tarFile.Stat()
tarFile, _ := os.Open(s.tarFile.Name())
if err != nil {
panic(err)
}
req, _ := http.NewRequest("PUT", s.Blob["url"], tarFile)
req.ContentLength = tarFileStat.Size()
return req
}
func (s *Slug) parseProcfile() *pf.Procfile {
procfileFile, _ := os.Open(s.slugDir + "/Procfile")
procfile, _ := pf.ParseProcfile(procfileFile)
return procfile
}
func handleError(_e error) {
if _e != nil {
log.Fatal(_e)
panic(_e)
}
}
func targzWalk(dirPath string, tw *tar.Writer) {
var walkfunc filepath.WalkFunc
walkfunc = func(path string, fi os.FileInfo, err error) error {
h, err := tar.FileInfoHeader(fi, "")
handleError(err)
h.Name = "./app/" + path
if fi.Mode()&os.ModeSymlink != 0 {
linkPath, err := os.Readlink(path)
handleError(err)
h.Linkname = linkPath
}
err = tw.WriteHeader(h)
handleError(err)
if fi.Mode()&os.ModeDir == 0 && fi.Mode()&os.ModeSymlink == 0 {
fr, err := os.Open(path)
handleError(err)
defer fr.Close()
_, err = io.Copy(tw, fr)
handleError(err)
}
return nil
}
filepath.Walk(dirPath, walkfunc)
}
func tarGz(inPath string) *os.File {
wd, _ := os.Getwd()
os.Chdir(inPath)
// file write
tarFile, _ := ioutil.TempFile("", "slug")
tarFileName := tarFile.Name() + ".tgz"
os.Rename(tarFile.Name(), tarFileName)
fw, err := os.Create(tarFileName)
handleError(err)
// gzip write
gw := gzip.NewWriter(fw)
defer gw.Close()
// tar write
tw := tar.NewWriter(gw)
defer tw.Close()
targzWalk(".", tw)
os.Chdir(wd)
return fw
}