-
Notifications
You must be signed in to change notification settings - Fork 11
/
new.go
101 lines (76 loc) · 1.6 KB
/
new.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/template"
"github.com/jingweno/nut/vendor/_nuts/github.com/codegangsta/cli"
)
const nutTomlTmpl = `[application]
name = "{{.Name}}"
version = "0.0.1"
authors = ["Your Name <[email protected]>"]
`
const readmeMd = `# {{.Name}}
This is an awesome Go projeect.
`
const gitIgnore = `{{.Name}}
`
const mainGo = `package main
import "fmt"
func main() {
fmt.Println("Hello Gophers!")
}
`
var newCmd = cli.Command{
Name: "new",
Usage: "create a nut project",
Action: runNew,
}
func runNew(c *cli.Context) {
if len(c.Args()) == 0 {
check(fmt.Errorf("Usage: \n nut new <path>"))
}
name := c.Args()[0]
err := os.Mkdir(name, os.ModePerm)
check(err)
err = createNutToml(name)
check(err)
err = createReadmeMd(name)
check(err)
err = createGitIgnore(name)
check(err)
err = createMainGo(name)
check(err)
}
func createNutToml(name string) error {
return createFile(name, nutTomlTmpl, "Nut.toml")
}
func createReadmeMd(name string) error {
return createFile(name, readmeMd, "README.md")
}
func createGitIgnore(name string) error {
return createFile(name, gitIgnore, ".gitignore")
}
func createMainGo(name string) error {
return ioutil.WriteFile(filepath.Join(name, "main.go"), []byte(mainGo), 0644)
}
func createFile(name, templ, fileName string) error {
t, err := template.New(name).Parse(templ)
if err != nil {
return err
}
n := struct {
Name string
}{
name,
}
var b bytes.Buffer
err = t.Execute(&b, n)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(name, fileName), b.Bytes(), 0644)
}