-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
327 lines (290 loc) · 7.99 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
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
327
// Copyright 2022 Tristan Isham. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/tristanisham/zvm/cli"
"github.com/tristanisham/zvm/cli/meta"
opts "github.com/urfave/cli/v2"
"github.com/charmbracelet/log"
)
var (
zvm cli.ZVM
printUpgradeNotice bool = true
)
var zvmApp = &opts.App{
Name: "ZVM",
Usage: "Zig Version Manager",
Description: "zvm lets you easily install, upgrade, and switch between different versions of Zig.",
HelpName: "zvm",
Version: meta.VerCopy,
Copyright: "Copyright © 2022 Tristan Isham",
Suggest: true,
Before: func(ctx *opts.Context) error {
zvm = *cli.Initialize()
return nil
},
// app-global flags
Flags: []opts.Flag{
&opts.StringFlag{
Name: "color",
Usage: "enable (on, yes/y, enabled, true) or disable (off, no/n, disabled, false) colored ZVM output",
Value: "toggle",
Action: func(ctx *opts.Context, val string) error {
switch val {
case "on", "yes", "enabled", "y", "true":
zvm.Settings.YesColor()
case "off", "no", "disabled", "n", "false":
zvm.Settings.NoColor()
default:
zvm.Settings.ToggleColor()
}
return nil
},
},
},
Commands: []*opts.Command{
{
Name: "install",
Usage: "download and install a version of Zig",
Aliases: []string{"i"},
Flags: []opts.Flag{
&opts.BoolFlag{
Name: "zls",
// Aliases: []string{"z"},
Usage: "install ZLS",
},
&opts.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "force installation even if the version is already installed",
},
&opts.BoolFlag{
Name: "full",
Usage: "use the 'full' zls compatibility mode",
},
},
Description: "To install the latest version, use `master`",
Args: true,
ArgsUsage: " <ZIG VERSION>",
Action: func(ctx *opts.Context) error {
versionArg := strings.TrimPrefix(ctx.Args().First(), "v")
if versionArg == "" {
return errors.New("no version provided")
}
req := cli.ExtractInstall(versionArg)
req.Version = strings.TrimPrefix(req.Version, "v")
force := zvm.Settings.AlwaysForceInstall
if ctx.Bool("force") {
force = ctx.Bool("force")
}
zlsCompat := "only-runtime"
if ctx.Bool("full") {
zlsCompat = "full"
}
// Install Zig
err := zvm.Install(req.Package, force)
if err != nil {
return err
}
// Install ZLS (if requested)
if ctx.Bool("zls") {
if err := zvm.InstallZls(req.Package, zlsCompat, force); err != nil {
return err
}
}
return nil
},
},
{
Name: "use",
Usage: "switch between versions of Zig",
Args: true,
Flags: []opts.Flag{
&opts.BoolFlag{
Name: "sync",
Usage: "sync your current version of Zig with the repository",
},
},
Action: func(ctx *opts.Context) error {
if ctx.Bool("sync") {
return zvm.Sync()
} else {
versionArg := strings.TrimPrefix(ctx.Args().First(), "v")
return zvm.Use(versionArg)
}
},
},
{
Name: "run",
Usage: "run a command with the given Zig version",
Args: true,
Action: func(ctx *opts.Context) error {
versionArg := strings.TrimPrefix(ctx.Args().First(), "v")
cmds := ctx.Args().Tail()
return zvm.Run(versionArg, cmds)
},
},
{
Name: "list",
Usage: "list installed Zig versions. Flag `--all` to see remote options",
Aliases: []string{"ls"},
Args: true,
Flags: []opts.Flag{
&opts.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Usage: "list remote Zig versions available for download, based on your version map",
},
&opts.BoolFlag{
Name: "vmu",
Usage: "list set version maps",
},
},
Action: func(ctx *opts.Context) error {
log.Debug("Version Map", "url", zvm.Settings.VersionMapUrl, "cmd", "list/ls")
if ctx.Bool("all") {
return zvm.ListRemoteAvailable()
} else if ctx.Bool("vmu") {
if len(zvm.Settings.VersionMapUrl) == 0 {
if err := zvm.Settings.ResetVersionMap(); err != nil {
return err
}
}
if len(zvm.Settings.ZlsVMU) == 0 {
if err := zvm.Settings.ResetZlsVMU(); err != nil {
return err
}
}
vmu := zvm.Settings.VersionMapUrl
zrw := zvm.Settings.ZlsVMU
fmt.Printf("Zig VMU: %s\nZLS VMU: %s\n", vmu, zrw)
return nil
} else {
return zvm.ListVersions()
}
},
},
{
Name: "uninstall",
Usage: "remove an installed version of Zig",
Aliases: []string{"rm"},
Args: true,
Action: func(ctx *opts.Context) error {
versionArg := strings.TrimPrefix(ctx.Args().First(), "v")
return zvm.Uninstall(versionArg)
},
},
{
Name: "clean",
Usage: "remove build artifacts (good if you're a scrub)",
Action: func(ctx *opts.Context) error {
return zvm.Clean()
},
},
{
Name: "upgrade",
Usage: "self-upgrade ZVM",
Action: func(ctx *opts.Context) error {
printUpgradeNotice = false
return zvm.Upgrade()
},
},
{
Name: "vmu",
Usage: "set ZVM's version map URL for custom Zig distribution servers",
Args: true,
Subcommands: []*opts.Command{
{
Name: "zig",
Usage: "set ZVM's version map URL for custom Zig distribution servers",
Args: true,
ArgsUsage: "",
Action: func(ctx *opts.Context) error {
url := ctx.Args().First()
log.Debug("user passed VMU", "url", url)
switch url {
case "default":
return zvm.Settings.ResetVersionMap()
case "mach":
if err := zvm.Settings.SetVersionMapUrl("https://machengine.org/zig/index.json"); err != nil {
log.Info("Run `zvm vmu zig default` to reset your version map.")
return err
}
default:
if err := zvm.Settings.SetVersionMapUrl(url); err != nil {
log.Info("Run `zvm vmu zig default` to reset your verison map.")
return err
}
}
return nil
},
},
{
Name: "zls",
Usage: "set ZVM's version map URL for custom ZLS Release Workers",
Args: true,
Action: func(ctx *opts.Context) error {
url := ctx.Args().First()
log.Debug("user passed zrw", "url", url)
switch url {
case "default":
return zvm.Settings.ResetZlsVMU()
default:
if err := zvm.Settings.SetZlsVMU(url); err != nil {
log.Info("Run `zvm vmu zls default` to reset your release worker.")
return err
}
}
return nil
},
},
},
},
},
}
func main() {
if _, ok := os.LookupEnv("ZVM_DEBUG"); ok {
log.SetLevel(log.DebugLevel)
}
_, checkUpgradeDisabled := os.LookupEnv("ZVM_SET_CU")
log.Debug("Automatic Upgrade Checker", "disabled", checkUpgradeDisabled)
// Upgrade
upSig := make(chan string, 1)
if !checkUpgradeDisabled {
go func(out chan<- string) {
if tag, ok, _ := cli.CanIUpgrade(); ok {
out <- tag
} else {
out <- ""
}
}(upSig)
} else {
upSig <- ""
}
// run and report errors
if err := zvmApp.Run(os.Args); err != nil {
// if meta.VERSION == "v0.7.9" && errors.Is(err, cli.ErrInvalidVersionMap) {
// meta.CtaGeneric("Help", `Encountered an issue while trying to install ZLS for Zig 'master'.
// Problem: ZVM v0.7.7 and v0.7.8 may have saved an invalid 'zlsVersionMapUrl' to your settings,
// which causes this error. The latest version, v0.7.9, can fix this issue by using the correct URL.
// To resolve this:
// 1. Open your ZVM settings file: '~/.zvm/settings.json'
// 2. Remove the 'zlsVersionMapUrl' key & value from the file (if present).
// What happens next: ZVM will automatically use the correct version map the next time you run it
// If the issue persists, please double-check your settings and try again, or create a GitHub Issue.`)
// }
meta.CtaFatal(err)
}
if tag := <-upSig; tag != "" {
if printUpgradeNotice {
meta.CtaUpgradeAvailable(tag)
} else {
log.Infof("You are now using ZVM %s\n", tag)
}
}
}