-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (100 loc) · 2.45 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
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
package main
import (
"embed"
"flag"
"fmt"
"github.com/mlctrez/goappcreate/internal"
"io/fs"
"log"
"os"
"reflect"
"runtime"
"strings"
)
//go:embed goapp/* Makefile
var Files embed.FS
type Step func() error
var steps = []Step{validateGoMod, createGoAppFiles, createLogos, createMakeFile}
var copyAppCss bool
func main() {
flag.BoolVar(©AppCss, "dark", false, "copies in the dark theme app.css")
flag.Parse()
for i, step := range steps {
fmt.Println(i, "execute", runtime.FuncForPC(reflect.ValueOf(step).Pointer()).Name())
err := step()
if err != nil {
log.Fatal(err)
}
}
}
func validateGoMod() (err error) {
if _, err = os.Stat("go.mod"); os.IsNotExist(err) {
return fmt.Errorf("missing go.mod - it is needed to determine module path")
}
var modulePath string
if modulePath, err = internal.GetModulePath(); err != nil {
return
}
if modulePath == "" {
return fmt.Errorf("unable to get module path from go.mod")
}
return
}
func createGoAppFiles() (err error) {
modulePath, _ := internal.GetModulePath()
oldModule := "github.com/mlctrez/goappcreate"
err = fs.WalkDir(Files, "goapp", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return os.MkdirAll(path, 0755)
}
var fileBytes []byte
fileBytes, err = Files.ReadFile(path)
if err != nil {
return err
}
// skip logo copy, they are generated
if strings.HasPrefix(d.Name(), "logo-") {
return nil
}
if !copyAppCss && d.Name() == "app.css" {
return nil
}
if strings.HasSuffix(path, ".go") {
replaced := strings.ReplaceAll(string(fileBytes), oldModule, modulePath)
fileBytes = []byte(replaced)
}
return writeFileWithCheck(path, fileBytes)
})
return
}
func createMakeFile() (err error) {
var makefile []byte
if makefile, err = Files.ReadFile("Makefile"); err != nil {
return
}
return writeFileWithCheck("Makefile", makefile)
}
func createLogos() (err error) {
var pngBytes []byte
for _, size := range []int{192, 512} {
if pngBytes, err = internal.CreatePng(size, size); err != nil {
return err
}
path := fmt.Sprintf("goapp/web/logo-%d.png", size)
if err = writeFileWithCheck(path, pngBytes); err != nil {
return
}
}
return
}
func writeFileWithCheck(path string, fileBytes []byte) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return os.WriteFile(path, fileBytes, 0644)
} else {
fmt.Printf("skipping file %s - it already exists\n", path)
return nil
}
}