-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
108 lines (98 loc) · 2.59 KB
/
io.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
package main
import (
"fmt"
"github.com/krrrr38/gpshow/utils"
"io/ioutil"
"os"
"sync"
)
// StaticDownload has external url and save point
type StaticDownload struct {
url string
outDir string
filename string
}
// CopyResourceDir copy template files into project dir
func CopyResourceDir(srcDir, outDir string) {
os.MkdirAll(outDir, 0755)
targetResourcePath := "resources/" + srcDir
templateFiles, err := AssetDir(targetResourcePath)
utils.DieIf(err)
for _, filename := range templateFiles {
copyResourceDir(targetResourcePath, outDir, filename)
}
}
func copyResourceDir(srcDir, outDir, filename string) {
srcPath := srcDir + "/" + filename
outPath := outDir + "/" + filename
_, err := AssetInfo(srcPath)
if err != nil {
err := os.Mkdir(outPath, 0755)
utils.DieIf(err)
srcFiles, err := AssetDir(srcPath)
utils.DieIf(err)
for _, nextFilename := range srcFiles {
copyResourceDir(srcPath, outPath, nextFilename)
}
} else {
bytes, err := Asset(srcPath)
utils.DieIf(err)
ioutil.WriteFile(outPath, bytes, 0644)
}
}
// DownloadStaticFiles try to download files and save them
func DownloadStaticFiles(targetDir string, files []StaticDownload) {
var wg sync.WaitGroup
for _, file := range files {
wg.Add(1)
go func(file StaticDownload) {
bytes, err := FetchFile(file.url)
if !utils.ErrorIf(err) {
dirpath := targetDir + "/" + file.outDir
filepath := dirpath + file.filename
if _, err := os.Stat(dirpath); err != nil {
os.MkdirAll(dirpath, 0755)
}
utils.ErrorIf(ioutil.WriteFile(filepath, bytes, 0644))
}
wg.Done()
}(file)
}
wg.Wait()
}
// CopyLocalStaticFiles copy static dir into outdir
func CopyLocalStaticFiles(outDir, showPath string, ignoreDirs []string) {
files, _ := ioutil.ReadDir(showPath)
ignoreDirs = append(ignoreDirs, outDir)
for _, file := range files {
if file.Name() != "conf.js" && !arrayContain(file.Name(), ignoreDirs) {
copyDir(showPath, outDir, file)
}
}
}
func arrayContain(target string, arr []string) bool {
for _, str := range arr {
if str == target {
return true
}
}
return false
}
func copyDir(srcDir, outDir string, file os.FileInfo) {
outFilepath := fmt.Sprintf("%s/%s", outDir, file.Name())
srcFilepath := fmt.Sprintf("%s/%s", srcDir, file.Name())
if file.IsDir() {
os.Mkdir(outFilepath, file.Mode())
files, err := ioutil.ReadDir(srcFilepath)
if !utils.ErrorIf(err) {
for _, child := range files {
copyDir(srcFilepath, outFilepath, child)
}
}
} else {
bytes, err := ioutil.ReadFile(srcFilepath)
if !utils.ErrorIf(err) {
ioutil.WriteFile(outFilepath, bytes, file.Mode())
}
}
}