-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
81 lines (66 loc) · 1.63 KB
/
init.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
package tempcube
import (
"bytes"
"fmt"
"os"
"github.com/pkg/errors"
)
func InitFile(p string, b bytes.Buffer) error {
_, err := os.Stat(p)
if err != nil {
f, err := os.Create(p)
if err != nil {
return errors.Wrapf(err, "failed to create file %s", p)
}
_, err = f.WriteString(b.String())
if err != nil {
return errors.Wrapf(err, "failed to write file %s", p)
}
}
return nil
}
func Init(p string) error {
_, err := os.Stat(p)
if err != nil {
err = os.Mkdir(p, 0777)
if err != nil {
return errors.Wrapf(err, "failed to mkdir %s", p)
}
}
fmt.Println("create starting ...")
var tmpl bytes.Buffer
tmpl.WriteString("package ")
tmpl.WriteString(p)
// sample/sample.go
pt := fmt.Sprintf("%s/%s.go", p, p)
err = InitFile(pt, tmpl)
if err != nil {
return errors.Wrapf(err, "failed to create file %s", pt)
}
fmt.Printf("---> create %s\n", pt)
// sample/sample_helper.go
pt = fmt.Sprintf("%s/%s_helper.go", p, p)
err = InitFile(pt, tmpl)
if err != nil {
return errors.Wrapf(err, "failed to create file %s", pt)
}
fmt.Printf("---> create %s\n", pt)
tmpl.WriteString("\n")
tmpl.WriteString("import \"testing\"")
// sample/sample_test.go
pt = fmt.Sprintf("%s/%s_test.go", p, p)
err = InitFile(pt, tmpl)
if err != nil {
return errors.Wrapf(err, "failed to create file %s", pt)
}
fmt.Printf("---> create %s\n", pt)
// sample/sample_helper_test.go
pt = fmt.Sprintf("%s/%s_helper_test.go", p, p)
err = InitFile(pt, tmpl)
if err != nil {
return errors.Wrapf(err, "failed to create file %s", pt)
}
fmt.Printf("---> create %s\n", pt)
fmt.Println("Finishing creating initial files.")
return nil
}