/
new.go
142 lines (117 loc) · 3.07 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"runtime"
"strings"
"github.com/fatih/color"
"github.com/go-git/go-git/v5"
)
var appURL string
func doNew(appName string) {
appName = strings.ToLower(appName)
appURL = appName
// convert app name to single word
if strings.Contains(appName, "/") {
spltAftr := strings.SplitAfter(appName, "/")
appName = spltAftr[(len(spltAftr) - 1)]
}
log.Println("App name is", appName)
// git clone the skeleton application
color.Green("\tCloning repository...")
_, err := git.PlainClone("./"+appName, false, &git.CloneOptions{
URL: "https://github.com/wtran29/fenix-app.git",
Progress: os.Stdout,
Depth: 1,
})
if err != nil {
exitGracefully(err)
}
// remove .git directory
err = os.RemoveAll(fmt.Sprintf("./%s/.git", appName))
if err != nil {
exitGracefully(err)
}
// create general .env file
color.Yellow("\tCreating .env file...")
data, err := templateFS.ReadFile("templates/env.txt")
if err != nil {
exitGracefully(err)
}
env := string(data)
env = strings.ReplaceAll(env, "${APP_NAME}", appName)
key, _ := fnx.RandomString(32)
env = strings.ReplaceAll(env, "${KEY}", key)
err = copyDataToFile([]byte(env), fmt.Sprintf("./%s/.env", appName))
if err != nil {
exitGracefully(err)
}
// create a makefile
if runtime.GOOS == "windows" {
source, err := os.Open(fmt.Sprintf("./%s/Makefile.windows", appName))
if err != nil {
exitGracefully(err)
}
defer source.Close()
destination, err := os.Create(fmt.Sprintf("./%s/makefile", appName))
if err != nil {
exitGracefully(err)
}
defer destination.Close()
_, err = io.Copy(destination, source)
if err != nil {
exitGracefully(err)
}
} else {
source, err := os.Open(fmt.Sprintf("./%s/Makefile.mac", appName))
if err != nil {
exitGracefully(err)
}
defer source.Close()
destination, err := os.Create(fmt.Sprintf("./%s/makefile", appName))
if err != nil {
exitGracefully(err)
}
defer destination.Close()
_, err = io.Copy(destination, source)
if err != nil {
exitGracefully(err)
}
}
_ = os.Remove("./" + appName + "/Makefile.mac")
_ = os.Remove("./" + appName + "/Makefile.windows")
// update go.mod file
color.Yellow("\tCreating go.mod file...")
_ = os.Remove("./" + appName + "/go.mod")
data, err = templateFS.ReadFile("templates/go.mod.txt")
if err != nil {
exitGracefully(err)
}
mod := string(data)
mod = strings.ReplaceAll(mod, "${APP_NAME}", appURL)
err = copyDataToFile([]byte(mod), "./"+appName+"/go.mod")
if err != nil {
exitGracefully(err)
}
// update existing .go files with correct name/imports
color.Yellow("\tUpdating source files...")
os.Chdir("./" + appName)
updateSource()
// run go mod tidy in the project directory
color.Yellow("\tRunning go mod tidy...")
cmd := exec.Command("go", "get", "github.com/wtran29/fenix/fenix")
err = cmd.Start()
if err != nil {
exitGracefully(err)
}
cmd = exec.Command("go", "mod", "tidy")
err = cmd.Start()
if err != nil {
exitGracefully(err)
}
color.Green("Done building " + appURL)
color.Green("Go build something immortal")
}