This repository was archived by the owner on Apr 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew.go
190 lines (165 loc) · 3.55 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package cmd
import (
"fmt"
"github.com/short-d/app/fw/cli"
"github.com/short-d/fwcli/git"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
const serviceTemplateURL = "https://github.com/short-d/app-template.git"
const defaultAppName = "sampleapp"
func newNew(factory cli.CommandFactory) cli.Command {
config := cli.CommandConfig{
Usage: "new [service_name]",
ShortHelpMsg: "Generate a new project at current directory",
OnExecute: func(cmd cli.Command, args []string) {
if len(args) < 1 {
err := cmd.Help()
if err != nil {
crash(err)
}
return
}
serviceName := args[0]
cloneTemplate(serviceName)
updateAppName(serviceName)
initializeRepo(serviceName)
installDeps()
},
}
return factory.NewCommand(config)
}
func initializeRepo(serviceName string) {
fmt.Println("Initializing repository")
gitPath := filepath.Join(serviceName, ".git")
err := os.RemoveAll(gitPath)
if err != nil {
crash(err)
return
}
err = git.InitializeRepo(serviceName)
if err != nil {
crash(err)
return
}
err = os.Chdir(serviceName)
if err != nil {
crash(err)
return
}
err = git.StageAllChanges()
if err != nil {
crash(err)
return
}
err = git.Commit("Initialize repository")
if err != nil {
crash(err)
return
}
}
func installDeps() {
installFrontendDeps()
installBackendDeps()
}
func installFrontendDeps() {
fmt.Println("Installing frontend dependencies")
err := os.Chdir("web")
if err != nil {
crash(err)
return
}
command := exec.Command("yarn")
err = command.Run()
if err != nil {
crash(err)
return
}
err = os.Chdir("../")
if err != nil {
crash(err)
return
}
}
func installBackendDeps() {
fmt.Println("Installing backend dependencies")
err := os.Chdir("backend")
if err != nil {
crash(err)
return
}
command := exec.Command("go", "mod", "download")
err = command.Run()
if err != nil {
crash(err)
return
}
err = os.Chdir("../")
if err != nil {
crash(err)
return
}
}
func updateAppName(serviceName string) {
fmt.Println("Updating service name in .drone.yml")
droneConfigPath := filepath.Join(serviceName, ".drone.yml")
err := replaceString(droneConfigPath, defaultAppName, serviceName)
if err != nil {
crash(err)
return
}
fmt.Println("Updating service name in Kubernetes configs")
k8sConfigPath := filepath.Join(serviceName, "k8s")
err = replaceString(k8sConfigPath, defaultAppName, serviceName)
if err != nil {
crash(err)
}
fmt.Println("Updating go module name")
backendCodePath := filepath.Join(serviceName, "backend")
err = replaceString(backendCodePath, "github.com/short-d/app-template/backend", serviceName)
if err != nil {
crash(err)
}
}
func replaceString(dir string, original string, new string) error {
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
buf, err := ioutil.ReadFile(path)
if err != nil {
return err
}
content := strings.ReplaceAll(string(buf), original, new)
return ioutil.WriteFile(path, []byte(content), info.Mode())
})
}
func cloneTemplate(serviceName string) {
fmt.Printf("Cloning template to %s\n", serviceName)
if fileExists(serviceName) {
fmt.Printf(
`%s already exists.
Please first remove it or choose a different service name.
`,
serviceName)
return
}
err := git.CloneRepo(serviceTemplateURL, serviceName)
if err != nil {
crash(err)
}
}
func fileExists(fileName string) bool {
_, err := os.Stat(fileName)
return !os.IsNotExist(err)
}
func crash(err error) {
fmt.Println(err)
os.Exit(1)
}