-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.go
More file actions
94 lines (73 loc) · 1.35 KB
/
create.go
File metadata and controls
94 lines (73 loc) · 1.35 KB
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
package main
import (
"archive/tar"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type Creator struct {
Dir string
Project []byte
}
func (c *Creator) Copy(name string, reader io.Reader) error {
var err error
if hasExt(name) == false {
err = os.MkdirAll(name, 0775)
if err != nil {
return err
}
} else {
out, err := os.Create(name)
defer out.Close()
_, err = io.Copy(out, reader)
if err != nil {
return err
}
}
return err
}
func (c *Creator) Create() error {
var err error
r := bytes.NewReader(DefaultProject())
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
// Strip the project directory from the name
name := filepath.Join(c.Dir, strings.TrimLeft(hdr.Name, "project"))
// Exclude root directory and dot files
if name == c.Dir || strings.HasPrefix(filepath.Base(name), ".") {
continue
}
c.Copy(name, tr)
}
return err
}
func Create(dst string) error {
var err error
dst, err = expand(dst)
if err != nil {
return err
}
found, err := exists(dst)
if err != nil {
return err
}
if found {
return fmt.Errorf("Destination directory '%s' already exist.", dst)
}
creator := &Creator{Dir: dst, Project: DefaultProject()}
err = ensure(creator.Dir, false)
if err != nil {
return err
}
return creator.Create()
}