-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-go-website.go
79 lines (64 loc) · 1.79 KB
/
setup-go-website.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
package main
import (
"flag"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
)
func main() {
websiteName := flag.String("websiteName", "default-go-website", "The website name")
if len(os.Args) <= 1 {
fmt.Printf("No website name passed. Using default value [%v]\n", *websiteName)
} else if len(os.Args) == 2 {
log.Fatal("Utilize -websiteName flag and then pass parameter\n")
} else if len(os.Args) > 3 {
log.Fatal("Too many arguments passed.\n")
}
flag.Parse()
if checkIfDirExists(*websiteName) {
log.Printf("Exiting.. Folder already exists: [%v]\n", *websiteName)
return
} else {
fmt.Println("Folder doesn't yet exist.")
}
fmt.Printf("Creating recommended folders for your new Go website [%v]\n", *websiteName)
makeDir(*websiteName, 0755)
makeDir(*websiteName+"/internal", 0755)
makeDir(*websiteName+"/cmd/web", 0755)
makeDir(*websiteName+"/ui/html/pages", 0755)
makeDir(*websiteName+"/ui/html/partials", 0755)
makeDir(*websiteName+"/ui/static/css", 0755)
makeDir(*websiteName+"/ui/static/img", 0755)
makeDir(*websiteName+"/ui/static/js", 0755)
makeEmptyFile(*websiteName+"/cmd/web/main.go", 0755)
makeEmptyFile(*websiteName+"/cmd/web/handlers.go", 0755)
}
func checkIfDirExists(dir string) bool {
files, err := ioutil.ReadDir("./")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Searching for %v\n", dir)
for _, file := range files {
if file.Name() == dir {
fmt.Printf("Folder already exists [%v]\n", dir)
return true
}
}
return false
}
func makeDir(path string, permission fs.FileMode) {
fmt.Printf("Creating path: %v\n", path)
err := os.MkdirAll(path, permission)
if err != nil && !os.IsExist(err) {
log.Fatal(err)
}
}
func makeEmptyFile(path string, permission fs.FileMode) {
err := os.WriteFile(path, nil, permission)
if err != nil {
log.Fatal(err)
}
}