Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
326 lines (292 loc) · 7.78 KB
/
Copy pathmain.go
File metadata and controls
326 lines (292 loc) · 7.78 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/spf13/cobra"
tea "github.com/charmbracelet/bubbletea"
"github.com/vomlabs/zetup/config"
"github.com/vomlabs/zetup/generator"
"github.com/vomlabs/zetup/tui"
"github.com/vomlabs/zetup/update"
)
var version = "dev"
func main() {
rootCmd := &cobra.Command{
Use: "zetup",
Short: "The quickest way to start building",
Long: "Zetup simplifies setting up a new project — from web to Java, it's set up in seconds.",
}
initCmd := &cobra.Command{
Use: "init",
Short: "Launch the interactive project wizard",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current directory: %w", err)
}
m := tui.InitialModel()
p := tea.NewProgram(m, tea.WithAltScreen())
finalModel, err := p.Run()
if err != nil {
return err
}
model := finalModel.(tui.Model)
if !model.Generated {
fmt.Println("Cancelled.")
return nil
}
cfg := model.ToConfig()
if err := generator.Scaffold(cfg, cwd); err != nil {
return fmt.Errorf("scaffolding failed: %w", err)
}
fmt.Println("\n Done! Happy building!")
return nil
},
}
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the current version and check for updates",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("zetup %s\n\n", version)
latest, err := update.LatestVersion()
if err != nil {
fmt.Printf("Could not check for updates: %v\n", err)
return nil
}
cmp := update.CompareVersions(version, latest)
if cmp >= 0 {
fmt.Println("You're up to date!")
} else {
fmt.Printf("Update available: %s\n", latest)
fmt.Println("Run `zetup upgrade` to update to the latest version.")
}
return nil
},
}
rootCmd.AddCommand(versionCmd)
upgradeCmd := &cobra.Command{
Use: "upgrade",
Short: "Download and install the latest version",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
latest, err := update.LatestVersion()
if err != nil {
return fmt.Errorf("could not check for updates: %w", err)
}
cmp := update.CompareVersions(version, latest)
if cmp >= 0 {
fmt.Println("You're already up to date.")
return nil
}
fmt.Printf("Downloading %s ...\n", latest)
downloadURL, err := update.DownloadLatest()
if err != nil {
return fmt.Errorf("could not find download: %w", err)
}
assetResp, err := http.Get(downloadURL)
if err != nil {
return fmt.Errorf("download failed: %w", err)
}
defer assetResp.Body.Close()
if assetResp.StatusCode != 200 {
return fmt.Errorf("download returned status %d", assetResp.StatusCode)
}
exe, err := os.Executable()
if err != nil {
return fmt.Errorf("could not determine executable path: %w", err)
}
tmp := exe + ".new"
f, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if err != nil {
return fmt.Errorf("could not create temp file: %w", err)
}
written, err := io.Copy(f, assetResp.Body)
if err != nil {
f.Close()
os.Remove(tmp)
return fmt.Errorf("download incomplete after %d bytes: %w", written, err)
}
f.Close()
if err := os.Rename(tmp, exe); err != nil {
os.Remove(tmp)
return fmt.Errorf("could not replace binary: %w", err)
}
fmt.Printf("Updated to %s\n", latest)
return nil
},
}
rootCmd.AddCommand(upgradeCmd)
configCmd := &cobra.Command{
Use: "config",
Short: "Manage user defaults (author, package manager, etc.)",
Long: "View or change persistent defaults used by the wizard.\n" +
"Config is stored at ~/.config/zetup/config.yaml.\n" +
"\nKeys:\n" +
" author Default author name\n" +
" email Default email\n" +
" license Default license (MIT, Apache-2.0, GPL-3.0, etc.)\n" +
" pkg_manager Preferred package manager (npm, pnpm, bun)\n" +
" init_git Whether to init git by default (true/false)",
RunE: func(cmd *cobra.Command, args []string) error {
userCfg, err := cfg.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
return printConfig(userCfg)
},
}
configShowCmd := &cobra.Command{
Use: "show",
Short: "Show all config values",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
userCfg, err := cfg.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
return printConfig(userCfg)
},
}
configCmd.AddCommand(configShowCmd)
configGetCmd := &cobra.Command{
Use: "get <key>",
Short: "Get a config value",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
userCfg, err := cfg.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
val := getField(userCfg, args[0])
if val == "" {
return fmt.Errorf("unknown key %q", args[0])
}
fmt.Println(val)
return nil
},
}
configCmd.AddCommand(configGetCmd)
configSetCmd := &cobra.Command{
Use: "set <key> <value>",
Short: "Set a config value",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
userCfg, err := cfg.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if err := setField(userCfg, args[0], args[1]); err != nil {
return err
}
if err := cfg.Save(userCfg); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
fmt.Printf(" %s = %s\n", args[0], args[1])
return nil
},
}
configCmd.AddCommand(configSetCmd)
configInitCmd := &cobra.Command{
Use: "init",
Short: "Create a default config file",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
userCfg := cfg.Default()
if err := cfg.Save(userCfg); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
p, _ := cfg.Path()
fmt.Printf(" Created config at %s\n", p)
return nil
},
}
configCmd.AddCommand(configInitCmd)
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(initCmd)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func printConfig(c *cfg.UserConfig) error {
p, err := cfg.Path()
if err != nil {
return err
}
fmt.Printf("Config: %s\n\n", p)
fields := []struct {
key, val string
}{
{"author", c.Author},
{"email", c.Email},
{"license", c.License},
{"pkg_manager", c.PkgManager},
}
for _, f := range fields {
if f.val != "" {
fmt.Printf(" %-12s %s\n", f.key, f.val)
}
}
if c.InitGit != nil {
fmt.Printf(" %-12s %v\n", "init_git", *c.InitGit)
}
if c.Author == "" && c.Email == "" && c.License == "" &&
c.PkgManager == "" && c.InitGit == nil {
fmt.Println(" (no values set)")
}
return nil
}
func getField(c *cfg.UserConfig, key string) string {
switch strings.ToLower(key) {
case "author":
return c.Author
case "email":
return c.Email
case "license":
return c.License
case "pkg_manager":
return c.PkgManager
case "init_git":
if c.InitGit == nil {
return ""
}
return fmt.Sprintf("%v", *c.InitGit)
}
return ""
}
func setField(c *cfg.UserConfig, key, val string) error {
switch strings.ToLower(key) {
case "author":
c.Author = val
case "email":
c.Email = val
case "license":
c.License = val
case "pkg_manager":
val = strings.ToLower(val)
switch val {
case "npm", "pnpm", "bun":
default:
return fmt.Errorf("invalid package manager %q (expected npm, pnpm, or bun)", val)
}
c.PkgManager = val
case "init_git":
switch strings.ToLower(val) {
case "true", "yes", "1":
v := true
c.InitGit = &v
case "false", "no", "0":
v := false
c.InitGit = &v
default:
return fmt.Errorf("invalid value %q (expected true or false)", val)
}
default:
return fmt.Errorf("unknown key %q (see zetup config --help)", key)
}
return nil
}