/
create.go
174 lines (150 loc) · 4.52 KB
/
create.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"context"
"errors"
"flag"
"fmt"
"path"
"strings"
"github.com/gocopper/cli/cmd/copper/app/appbase"
"github.com/gocopper/cli/cmd/copper/storage/mysql"
"github.com/gocopper/cli/cmd/copper/storage/postgres"
"github.com/gocopper/cli/cmd/copper/storage/sqlite3"
"github.com/gocopper/cli/cmd/copper/storage/storagebase"
"github.com/gocopper/cli/cmd/copper/web/frontendnone"
"github.com/gocopper/cli/cmd/copper/web/tailwind"
"github.com/gocopper/cli/cmd/copper/web/tailwindpostcss"
"github.com/gocopper/cli/cmd/copper/web/vitereact"
"github.com/gocopper/cli/cmd/copper/web/webbase"
"github.com/gocopper/cli/pkg/mk"
"github.com/gocopper/cli/pkg/term"
"github.com/gocopper/copper/cerrors"
"github.com/google/subcommands"
)
func NewCreateCmd(term *term.Terminal) *CreateCmd {
return &CreateCmd{
term: term,
}
}
type CreateCmd struct {
term *term.Terminal
frontend string
storage string
}
func (c *CreateCmd) Name() string {
return "create"
}
func (c *CreateCmd) Synopsis() string {
return "Creates a new copper project"
}
func (c *CreateCmd) Usage() string {
return `copper create [-frontend=] [-storage=] github.com/user/foo
`
}
func (c *CreateCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.frontend, "frontend", "go", "go, go:tailwind, vite:react, vite:react:tailwind, none")
f.StringVar(&c.storage, "storage", "sqlite3", "sqlite3, postgres, mysql, none")
}
func (c *CreateCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if f.NArg() == 0 {
c.term.Text(c.Usage())
return subcommands.ExitUsageError
}
var (
module = f.Arg(0)
wd = path.Join(".", path.Base(module))
)
c.term.InProgressTask(fmt.Sprintf("Create %s with frontend=%s, storage=%s", module, c.frontend, c.storage))
err := appbase.Apply(wd, module)
if err != nil {
c.term.TaskFailed(cerrors.New(err, "failed to apply server code mod", nil))
return subcommands.ExitFailure
}
if c.frontend != "none" {
err = webbase.Apply(wd)
if err != nil {
c.term.TaskFailed(cerrors.New(err, "failed to apply web code mod", nil))
return subcommands.ExitFailure
}
}
switch c.frontend {
case "none":
err = frontendnone.Apply(wd)
if err != nil {
c.term.TaskFailed(cerrors.New(err, "failed to apply frontend none code mod", nil))
return subcommands.ExitFailure
}
case "go":
// note: already applied the web code mod
break
case "go:tailwind":
err = tailwind.Apply(wd)
if err != nil {
c.term.TaskFailed(cerrors.New(err, "failed to apply go:tailwind code mod", nil))
return subcommands.ExitFailure
}
case "vite:react":
err = vitereact.Apply(wd)
if err != nil {
c.term.TaskFailed(cerrors.New(err, "failed to apply vite:react code mod", nil))
return subcommands.ExitFailure
}
case "vite:react:tailwind":
err = vitereact.Apply(wd)
if err != nil {
c.term.TaskFailed(cerrors.New(err, "failed to apply vite:react code mod", nil))
return subcommands.ExitFailure
}
err = tailwindpostcss.Apply(wd)
if err != nil {
c.term.TaskFailed(cerrors.New(err, "failed to apply tailwind (postcss) code mod", nil))
return subcommands.ExitFailure
}
default:
c.term.TaskFailed(errors.New("unknown frontend stack"))
return subcommands.ExitUsageError
}
if c.storage != "none" {
err = storagebase.Apply(wd)
if err != nil {
c.term.TaskFailed(cerrors.New(err, "failed to apply storage code mod", nil))
return subcommands.ExitFailure
}
switch c.storage {
case "sqlite3":
err = sqlite3.Apply(wd)
if err != nil {
c.term.TaskFailed(cerrors.New(err, "failed to apply sqlite3 code mod", nil))
return subcommands.ExitFailure
}
case "postgres":
err = postgres.Apply(wd)
if err != nil {
c.term.TaskFailed(cerrors.New(err, "failed to apply postgres code mod", nil))
return subcommands.ExitFailure
}
case "mysql":
err = mysql.Apply(wd)
if err != nil {
c.term.TaskFailed(cerrors.New(err, "failed to apply mysql code mod", nil))
return subcommands.ExitFailure
}
default:
c.term.TaskFailed(errors.New("unknown storage stack"))
return subcommands.ExitUsageError
}
}
_ = mk.GoFmt(ctx, ".")
c.term.TaskSucceeded()
c.term.Section("Run App Server")
c.term.Box(fmt.Sprintf(`$ cd %s && copper run -watch`, wd))
if c.frontend == "go:tailwind" {
c.term.Section("Run Tailwind Server")
c.term.Box(fmt.Sprintf(`$ cd %s/web && npm run dev`, wd))
}
if strings.Contains(c.frontend, "vite") {
c.term.Section("Run Vite Server")
c.term.Box(fmt.Sprintf(`$ cd %s/web && npm run dev`, wd))
}
return subcommands.ExitSuccess
}