-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (75 loc) · 1.89 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/grokify/goheroku/templates"
)
// File is a struct to represent a quicktemplate.
type File struct {
ProductSlug string
TemplateFunc func(string) string
}
func main() {
projectSlug := ""
argsWithoutProg := os.Args
if len(argsWithoutProg) > 1 {
projectSlug = strings.TrimSpace(argsWithoutProg[1])
}
if len(projectSlug) == 0 {
fmt.Println("Please specify a project name: goheroku <projectName>")
os.Exit(1)
} else if regexp.MustCompile(`\s`).MatchString(projectSlug) {
fmt.Println("Please do not spaces in project names.")
os.Exit(1)
}
exists, err := exists(projectSlug)
if err != nil {
log.Fatal(err)
} else if exists {
fmt.Printf("Projects [%v] exists, exiting...\n", projectSlug)
os.Exit(1)
}
err = os.Mkdir(projectSlug, 0755)
if err != nil {
panic(err)
}
files := []File{{
ProductSlug: ".env",
TemplateFunc: templates.DotEnvTemplate}, {
ProductSlug: "app.json",
TemplateFunc: templates.AppJsonTemplate}, {
ProductSlug: "Dockerfile",
TemplateFunc: templates.DockerfileTemplate}, {
ProductSlug: "heroku.yml",
TemplateFunc: templates.HerokuYmlTemplate}, {
ProductSlug: "Makefile",
TemplateFunc: templates.MakefileTemplate}, {
ProductSlug: "Procfile",
TemplateFunc: templates.ProcfileTemplate}}
for i, file := range files {
str := file.TemplateFunc(projectSlug)
filename := filepath.Join(projectSlug, file.ProductSlug)
err := os.WriteFile(filename, []byte(str), 0600)
fmt.Printf("Writing file %v: %v", i+1, filename)
if err != nil {
panic(err)
}
fmt.Printf("\n")
}
fmt.Println("DONE")
}
// exists checks whether the named filepath exists or not for
// a file or directory.
func exists(name string) (bool, error) {
_, err := os.Stat(name)
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}