/
new.go
68 lines (58 loc) · 1.74 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
package project
import (
"context"
"fmt"
"os"
"path"
"github.com/ravilushqa/boilerplate/cmd/boilerplate/internal/base"
"github.com/AlecAivazis/survey/v2"
"github.com/fatih/color"
)
// Project is a project template.
type Project struct {
Name string
Path string
}
// New new a project from remote repo.
func (p *Project) New(ctx context.Context, dir string, layout string, branch string) error {
to := path.Join(dir, p.Name)
if _, err := os.Stat(to); !os.IsNotExist(err) {
fmt.Printf("🚫 %s already exists\n", p.Name)
override := false
prompt := &survey.Confirm{
Message: "📂 Do you want to override the folder ?",
Help: "Delete the existing folder and create the project.",
}
e := survey.AskOne(prompt, &override)
if e != nil {
return e
}
if !override {
return err
}
os.RemoveAll(to)
}
fmt.Printf("🚀 Creating service %s, layout repo is %s, please wait a moment.\n\n", p.Name, layout)
repo := base.NewRepo(layout, branch)
if err := repo.CopyTo(ctx, to, p.Path, []string{".git", ".github"}); err != nil {
return err
}
// replace substring in files in the chart folder, Makefile and README.md
base.ReplaceInFiles(
[]string{path.Join(to, "chart"), path.Join(to, "Makefile")},
[]string{"boilerplate", p.Name},
)
// remove cmd folder
err := os.RemoveAll(path.Join(to, "cmd"))
if err != nil {
return err
}
base.Tree(to, dir)
fmt.Printf("\n🍺 Project creation succeeded %s\n", color.GreenString(p.Name))
fmt.Print("💻 Use the following command to start the project 👇:\n\n")
fmt.Println(color.WhiteString("$ cd %s", p.Name))
fmt.Println(color.WhiteString("$ make build"))
fmt.Println(color.WhiteString("$ ./bin/app"))
fmt.Println(color.WhiteString("$ Help: ./bin/app --help"))
return nil
}