forked from netgusto/nodebook
/
defaultinitnotebook.go
61 lines (49 loc) · 1.81 KB
/
defaultinitnotebook.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
package helper
import (
"io"
"os"
"path"
"path/filepath"
"github.com/a112121788/nodebook/src/core/shared/types"
"github.com/markbates/pkger"
"github.com/pkg/errors"
)
func defaultInitNotebook(recipe types.Recipe, notebookspath, name string) error {
dirPerms := os.FileMode(0755)
filePerm := os.FileMode(0644)
srcPath := path.Join(recipe.GetDir(), "defaultcontent")
destPathRoot := path.Join(notebookspath, name)
if err := os.MkdirAll(destPathRoot, dirPerms); err != nil {
return errors.Wrap(err, "defaultInitNotebook: Could not create notebook directory "+destPathRoot)
}
return pkger.Walk(srcPath, func(pathStr string, info os.FileInfo, err error) error {
pathParts, _ := pkger.Parse(pathStr)
relPath := pathParts.Name[len(srcPath):] // make path relative to "defaultcontent"
destPath := path.Join(destPathRoot, relPath)
if info.IsDir() {
if err := os.MkdirAll(destPath, dirPerms); err != nil {
return errors.Wrap(err, "defaultInitNotebook: Could not create notebook directory "+destPath)
}
} else {
dir := filepath.Dir(destPath)
if err := os.MkdirAll(dir, dirPerms); err != nil {
return errors.Wrap(err, "defaultInitNotebook: Could not create notebook directory "+dir)
}
source, err := pkger.Open(pathStr)
if err != nil {
return errors.Wrap(err, "defaultInitNotebook: Could not open notebook default content file "+pathStr)
}
defer source.Close()
destination, err := os.OpenFile(destPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, filePerm)
if err != nil {
return errors.Wrap(err, "defaultInitNotebook: Could not create notebook file "+destPath)
}
defer destination.Close()
_, err = io.Copy(destination, source)
if err != nil {
return errors.Wrap(err, "defaultInitNotebook: Could not copy notebook default content file "+pathStr)
}
}
return nil
})
}