forked from anthdm/superkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.go
122 lines (108 loc) · 2.63 KB
/
install.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
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io/fs"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
const (
replaceID = "AABBCCDD"
bootstrapFolderName = "bootstrap"
reponame = "https://github.com/anthdm/gothkit.git"
)
func main() {
args := os.Args[1:]
if len(args) == 0 {
fmt.Println()
fmt.Println("install requires your project name as the first argument")
fmt.Println()
fmt.Println("\tgo run gothkit/install.go [your_project_name]")
fmt.Println()
os.Exit(1)
}
projectName := args[0]
// check if gothkit folder already exists, if so, delete
_, err := os.Stat("gothkit")
if !os.IsNotExist(err) {
fmt.Println("-- deleting gothkit folder cause its already present")
if err := os.RemoveAll("gothkit"); err != nil {
log.Fatal(err)
}
}
fmt.Println("-- cloning", reponame)
clone := exec.Command("git", "clone", reponame)
if err := clone.Run(); err != nil {
log.Fatal(err)
}
fmt.Println("-- renaming bootstrap ->", projectName)
if err := os.Rename(path.Join("gothkit", bootstrapFolderName), projectName); err != nil {
log.Fatal(err)
}
err = filepath.Walk(path.Join(projectName), func(fullPath string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
b, err := os.ReadFile(fullPath)
if err != nil {
return err
}
contentStr := string(b)
if strings.Contains(contentStr, replaceID) {
replacedContent := strings.ReplaceAll(contentStr, replaceID, projectName)
file, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(replacedContent)
if err != nil {
return err
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
fmt.Println("-- renaming .env.local -> .env")
if err := os.Rename(
path.Join(projectName, ".env.local"),
path.Join(projectName, ".env"),
); err != nil {
log.Fatal(err)
}
fmt.Println("-- generating secure secret")
pathToDotEnv := path.Join(projectName, ".env")
b, err := os.ReadFile(pathToDotEnv)
if err != nil {
log.Fatal(err)
}
secret := generateSecret()
replacedContent := strings.Replace(string(b), "{{app_secret}}", secret, -1)
file, err := os.OpenFile(pathToDotEnv, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
_, err = file.WriteString(replacedContent)
if err != nil {
log.Fatal(err)
}
fmt.Printf("-- project (%s) successfully installed!\n", projectName)
}
func generateSecret() string {
bytes := make([]byte, 32)
if _, err := rand.Read(bytes); err != nil {
log.Fatal(err)
}
return hex.EncodeToString(bytes)
}