/
main.go
98 lines (79 loc) · 1.94 KB
/
main.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/acsellers/inflections"
"github.com/urfave/cli"
"github.com/coreos/go-semver/semver"
"github.com/fatih/color"
De "github.com/visionmedia/go-debug"
)
var debug = De.Debug("governator:main")
type templateVars struct {
ProjectName string
PROJECTNAME string
}
func main() {
app := cli.NewApp()
app.Name = "go-go"
app.Version = version()
app.ArgsUsage = "<project-name>"
app.Action = run
app.Flags = []cli.Flag{}
app.Run(os.Args)
}
func run(context *cli.Context) {
debug("run")
name := getOpts(context)
vars := templateVars{
ProjectName: name,
PROJECTNAME: strings.ToUpper(inflections.Underscore(name)),
}
for _, assetName := range AssetNames() {
asset := parseTemplate(vars, assetName)
dir := filepath.Dir(assetName)
os.MkdirAll(dir, 0755)
ioutil.WriteFile(assetName, []byte(asset), 0644)
}
os.Chmod("build.sh", 0755)
fmt.Println("To restore dependencies, run: \n\ngvt restore")
}
func parseTemplate(vars templateVars, assetName string) string {
asset := MustAsset(assetName)
assetStr := string(asset)
assetTemplate, err := template.New(assetName).Parse(assetStr)
PanicIfError("template.Parse failed", err)
buffer := bytes.NewBufferString("")
assetTemplate.Execute(buffer, vars)
return buffer.String()
}
func getOpts(context *cli.Context) string {
name := context.Args().First()
if name == "" {
cli.ShowAppHelp(context)
color.Red(" Missing required arg <project-name>")
os.Exit(1)
}
return name
}
func version() string {
version, err := semver.NewVersion(VERSION)
if err != nil {
errorMessage := fmt.Sprintf("Error with version number: %v", VERSION)
log.Panicln(errorMessage, err.Error())
}
return version.String()
}
// PanicIfError prints error and dies if error is non nil
func PanicIfError(msg string, err error) {
if err == nil {
return
}
log.Panicf("ERROR(%v):\n\n%v", msg, err)
}