-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
60 lines (51 loc) · 1.31 KB
/
common.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
package fa
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"text/template"
rice "github.com/GeertJohan/go.rice"
)
var errNoWorkspace = errors.New("no workspace directory found")
var errAlreadyExist = errors.New("path already exist")
//var errIsAbsPath = errors.New("input is absolute path")
func makeDirs(dirs ...string) error {
for _, dir := range dirs {
if err := os.MkdirAll(dir, 0775); err != nil {
return err
}
}
return nil
}
func boxCopyAll(list []*struct {
from string
to string
perm os.FileMode
}, box *rice.Box) error {
for _, entry := range list {
if err := boxCopy(box, entry.from, entry.to, entry.perm); err != nil {
return err
}
}
return nil
}
func boxCopy(box *rice.Box, from, to string, perm os.FileMode) error {
if err := os.MkdirAll(filepath.Dir(to), 0775); err != nil {
return err
}
return ioutil.WriteFile(to, box.MustBytes(from), perm)
}
func boxCopyTemplate(box *rice.Box, from, to string, perm os.FileMode, holder map[string]string) error {
if err := os.MkdirAll(filepath.Dir(to), 0775); err != nil {
return err
}
tmpl := template.New(filepath.Base(from))
tmpl, _ = tmpl.Parse(box.MustString(from))
output, err := os.OpenFile(to, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, perm)
if err != nil {
return err
}
defer output.Close()
return tmpl.Execute(output, holder)
}