-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.go
93 lines (79 loc) · 1.81 KB
/
deploy.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
package osoba
import (
"archive/zip"
"io"
"net/http"
"os"
"path/filepath"
"strings"
)
func (a App) Deploy(path string) error {
dirName := filepath.Join(a.DocumentRoot, path)
// make directory
if _, err := os.Stat(dirName); os.IsNotExist(err) {
if err := os.MkdirAll(dirName, os.ModePerm); err != nil {
return err
}
}
// fetch zip file
a.Contents.Mu.Lock()
r, err := http.Get(a.Contents.V[path].URL)
if err != nil {
return err
}
a.Contents.Mu.Unlock()
defer r.Body.Close()
// create tmpolary directory
tmpDir, err := os.MkdirTemp("", strings.Replace("osoba"+path, string(os.PathSeparator), "", -1))
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)
// create zip file
zipPath := filepath.Join(tmpDir, "donwload.zip")
f, err := os.Create(zipPath)
if err != nil {
return err
}
io.Copy(f, r.Body)
// expand zip file
if err := a.unzip(zipPath, tmpDir); err != nil {
return err
}
// copy contents from tmpolary directory to document root
dirCopyAll(filepath.Join(tmpDir, a.TmpDirContentsPrefix), a.DocumentRoot)
return nil
}
func (a App) unzip(source, dir string) error {
r, err := zip.OpenReader(source)
if err != nil {
return err
}
prefix := strings.Split(r.File[0].Name, string(os.PathSeparator))[0]
for _, file := range r.File {
tmp := strings.Split(file.Name, string(os.PathSeparator))[0]
if tmp != prefix {
prefix = ""
break
}
}
for _, file := range r.File {
if file.Mode().IsDir() {
continue
}
os.MkdirAll(filepath.Join(dir, filepath.Dir(file.Name)), os.ModePerm)
rc, err := file.Open()
if err != nil {
return err
}
f, err := os.Create(filepath.Join(dir, file.Name))
if err != nil {
return err
}
f.ReadFrom(rc)
}
if prefix != "" {
os.Rename(filepath.Join(dir, prefix), filepath.Join(dir, a.TmpDirContentsPrefix))
}
return nil
}