-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
209 lines (170 loc) · 4.63 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
// 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 (
"flag"
"fmt"
"html/template"
"os"
"strings"
"github.com/tristanisham/zvm/cli"
"github.com/tristanisham/zvm/cli/meta"
"github.com/charmbracelet/log"
_ "embed"
)
//go:embed help.txt
var helpTxt string
func main() {
zvm := cli.Initialize()
args := os.Args[1:]
if _, ok := os.LookupEnv("ZVM_DEBUG"); ok {
log.SetLevel(log.DebugLevel)
}
if len(args) == 0 {
helpMsg()
// zvm.AlertIfUpgradable()
os.Exit(0)
}
// zvm.AlertIfUpgradable()
versionFlag := flag.Bool("version", false, "Print ZVM version information")
// Install flags
installFlagSet := flag.NewFlagSet("install", flag.ExitOnError)
installDeps := installFlagSet.String("D", "", "Specify additional dependencies to install with Zig")
// LS flags
lsFlagSet := flag.NewFlagSet("ls", flag.ExitOnError)
lsRemote := lsFlagSet.Bool("all", false, "List all available versions of Zig to install")
// Global config
sVersionMapUrl := flag.String("vmu", "", "Set ZVM's version map URL for custom Zig distribution servers")
sColorToggle := flag.Bool("color", true, "Turn on or off ZVM's color output")
flag.Parse()
if *versionFlag {
fmt.Println(meta.VerCopy)
os.Exit(0)
}
if sVersionMapUrl != nil && len(*sVersionMapUrl) != 0 {
log.Debug("user passed vmu", "url", *sVersionMapUrl)
switch *sVersionMapUrl {
case "default":
if err := zvm.Settings.ResetVersionMap(); err != nil {
meta.CtaFatal(err)
}
case "mach":
if err := zvm.Settings.SetVersionMapUrl("https://machengine.org/zig/index.json"); err != nil {
log.Info("Run `-vmu default` to reset your version map.")
meta.CtaFatal(err)
}
default:
if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil {
log.Info("Run `-vmu default` to reset your version map.")
meta.CtaFatal(err)
}
}
}
if sColorToggle != nil {
if *sColorToggle != zvm.Settings.UseColor {
if *sColorToggle {
zvm.Settings.YesColor()
} else {
zvm.Settings.NoColor()
}
}
}
args = flag.Args()
for i, arg := range args {
switch arg {
case "install", "i":
installFlagSet.Parse(args[i+1:])
// signal to install zls after zig
req := cli.ExtractInstall(args[len(args)-1])
req.Version = strings.TrimPrefix(req.Version, "v")
// log.Debug(req, "deps", *installDeps)
if err := zvm.Install(req.Package); err != nil {
meta.CtaFatal(err)
}
if *installDeps != "" {
switch *installDeps {
case "zls":
zvm.InstallZls(req.Package)
}
}
return
case "use":
if len(args) > i+1 {
version := strings.TrimPrefix(args[i+1], "v")
if err := zvm.Use(version); err != nil {
meta.CtaFatal(err)
}
}
return
case "ls":
lsFlagSet.Parse(args[i+1:])
log.Debug("Version Map", "url", zvm.Settings.VersionMapUrl)
if *lsRemote {
if err := zvm.ListRemoteAvailable(); err != nil {
meta.CtaFatal(err)
}
} else {
if err := zvm.ListVersions(); err != nil {
meta.CtaFatal(err)
}
}
return
case "uninstall", "rm":
if len(args) > i+1 {
version := strings.TrimPrefix(args[i+1], "v")
if err := zvm.Uninstall(version); err != nil {
meta.CtaFatal(err)
}
}
return
case "sync":
if err := zvm.Sync(); err != nil {
meta.CtaFatal(err)
}
case "clean":
// msg := "Clean is a beta command, and may not be included in the next release."
// if zvm.Settings.UseColor {
// fmt.Println(clr.Blue(msg))
// } else {
// fmt.Println(msg)
// }
if err := zvm.Clean(); err != nil {
if zvm.Settings.UseColor {
meta.CtaFatal(err)
} else {
meta.CtaFatal(err)
}
}
return
case "upgrade":
if err := zvm.Upgrade(); err != nil {
log.Error("this is a new command, and may have some issues.\nConsider reporting your problem on Github :)", "github", "https://github.com/tristanisham/zvm/issues")
meta.CtaFatal(err)
}
case "version":
fmt.Println(meta.VerCopy)
return
case "help":
//zvm.Settings.UseColor
helpMsg()
return
// Settings
default:
log.Fatalf("invalid argument %q. Please run `zvm help`.\n", arg)
}
}
}
func helpMsg() {
helpTmpl, err := template.New("help").Parse(helpTxt)
if err != nil {
fmt.Printf("Sorry! There was a rendering error (%q). The version is %s\n", err, meta.VERSION)
fmt.Println(helpTxt)
return
}
if err := helpTmpl.Execute(os.Stdout, map[string]string{"Version": meta.VERSION}); err != nil {
fmt.Printf("Sorry! There was a rendering error (%q). The version is %s\n", err, meta.VERSION)
fmt.Println(helpTxt)
return
}
}