-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.go
389 lines (362 loc) · 10.4 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/baneeishaque/adoptium_jdk_go"
"github.com/codegangsta/cli"
"github.com/tucnak/store"
"github.com/ystyle/jvms/utils/file"
"github.com/ystyle/jvms/utils/jdk"
"github.com/ystyle/jvms/utils/web"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
var version = "2.1.0"
const (
defaultOriginalpath = "https://raw.githubusercontent.com/ystyle/jvms/new/jdkdlindex.json"
)
type Config struct {
JavaHome string `json:"java_home"`
CurrentJDKVersion string `json:"current_jdk_version"`
Originalpath string `json:"original_path"`
Proxy string `json:"proxy"`
store string
download string
}
var config Config
type JdkVersion struct {
Version string `json:"version"`
Url string `json:"url"`
}
func main() {
app := cli.NewApp()
app.Name = "jvms"
app.Usage = `JDK Version Manager (JVMS) for Windows`
app.Version = version
app.CommandNotFound = func(c *cli.Context, command string) {
log.Fatal("Command Not Found")
}
app.Commands = commands()
app.Before = startup
app.After = shutdown
if err := app.Run(os.Args); err != nil {
log.Fatal(err.Error())
os.Exit(1)
}
}
func commands() []cli.Command {
return []cli.Command{
{
Name: "init",
Usage: "Initialize config file",
Description: `before init you should clear JAVA_HOME, PATH Environment variable。`,
Flags: []cli.Flag{
cli.StringFlag{
Name: "java_home",
Usage: "the JAVA_HOME location",
Value: filepath.Join(os.Getenv("ProgramFiles"), "jdk"),
},
cli.StringFlag{
Name: "originalpath",
Usage: "the jdk download index file url.",
Value: defaultOriginalpath,
},
},
Action: func(c *cli.Context) error {
if c.IsSet("java_home") || config.JavaHome == "" {
config.JavaHome = c.String("java_home")
}
cmd := exec.Command("cmd", "/C", "setx", "JAVA_HOME", config.JavaHome, "/M")
err := cmd.Run()
if err != nil {
return errors.New("set Environment variable `JAVA_HOME` failure: Please run as admin user")
}
fmt.Println("set `JAVA_HOME` Environment variable to ", config.JavaHome)
if c.IsSet("originalpath") || config.Originalpath == "" {
config.Originalpath = c.String("originalpath")
}
path := fmt.Sprintf(`%s/bin;%s;%s`, config.JavaHome, os.Getenv("PATH"), file.GetCurrentPath())
cmd = exec.Command("cmd", "/C", "setx", "path", path, "/m")
err = cmd.Run()
if err != nil {
return errors.New("set Environment variable `PATH` failure: Please run as admin user")
}
fmt.Println("add jvms.exe to `path` Environment variable")
return nil
},
},
{
Name: "list",
ShortName: "ls",
Usage: "List current JDK installations.",
Action: func(c *cli.Context) error {
fmt.Println("Installed jdk (* marks in use):")
v := jdk.GetInstalled(config.store)
for i, version := range v {
str := ""
if config.CurrentJDKVersion == version {
str = fmt.Sprintf("%s * %d) %s", str, i+1, version)
} else {
str = fmt.Sprintf("%s %d) %s", str, i+1, version)
}
fmt.Printf(str + "\n")
}
if len(v) == 0 {
fmt.Println("No installations recognized.")
}
return nil
},
},
{
Name: "install",
ShortName: "i",
Usage: "Install available remote jdk",
Action: func(c *cli.Context) error {
if config.Proxy != "" {
web.SetProxy(config.Proxy)
}
v := c.Args().Get(0)
if v == "" {
return errors.New("invalid version., Type \"jvms rls\" to see what is available for install")
}
if jdk.IsVersionInstalled(config.store, v) {
fmt.Println("Version " + v + " is already installed.")
return nil
}
versions, err := getJdkVersions()
if err != nil {
return err
}
if !file.Exists(config.download) {
os.MkdirAll(config.download, 0777)
}
if !file.Exists(config.store) {
os.MkdirAll(config.store, 0777)
}
for _, version := range versions {
if version.Version == v {
dlzipfile, success := web.GetJDK(config.download, v, version.Url)
if success {
fmt.Printf("Installing JDK %s ...\n", v)
// Extract jdk to the temp directory
jdktempfile := filepath.Join(config.download, fmt.Sprintf("%s_temp", v))
if file.Exists(jdktempfile) {
err := os.RemoveAll(jdktempfile)
if err != nil {
panic(err)
}
}
err := file.Unzip(dlzipfile, jdktempfile)
if err != nil {
return fmt.Errorf("unzip failed: %w", err)
}
// Copy the jdk files to the installation directory
temJavaHome := getJavaHome(jdktempfile)
err = os.Rename(temJavaHome, filepath.Join(config.store, v))
if err != nil {
return fmt.Errorf("unzip failed: %w", err)
}
// Remove the temp directory
// may consider keep the temp files here
os.RemoveAll(jdktempfile)
fmt.Println("Installation complete. If you want to use this version, type\n\njvms switch", v)
} else {
fmt.Println("Could not download JDK " + v + " executable.")
}
return nil
}
}
return errors.New("invalid version., Type \"jvms rls\" to see what is available for install")
},
},
{
Name: "switch",
ShortName: "s",
Usage: "Switch to use the specified version.",
Action: func(c *cli.Context) error {
v := c.Args().Get(0)
if v == "" {
return errors.New("you should input a version, Type \"jvms list\" to see what is installed")
}
if !jdk.IsVersionInstalled(config.store, v) {
fmt.Printf("jdk %s is not installed. ", v)
return nil
}
// Create or update the symlink
if file.Exists(config.JavaHome) {
err := os.Remove(config.JavaHome)
if err != nil {
return errors.New("Switch jdk failed, please manually remove " + config.JavaHome)
}
}
cmd := exec.Command("cmd", "/C", "setx", "JAVA_HOME", config.JavaHome, "/M")
err := cmd.Run()
if err != nil {
return errors.New("set Environment variable `JAVA_HOME` failure: Please run as admin user")
}
err = os.Symlink(filepath.Join(config.store, v), config.JavaHome)
if err != nil {
return errors.New("Switch jdk failed, " + err.Error())
}
fmt.Println("Switch success.\nNow using JDK " + v)
config.CurrentJDKVersion = v
return nil
},
},
{
Name: "remove",
ShortName: "rm",
Usage: "Remove a specific version.",
Action: func(c *cli.Context) error {
v := c.Args().Get(0)
if v == "" {
return errors.New("you should input a version, Type \"jvms list\" to see what is installed")
}
if jdk.IsVersionInstalled(config.store, v) {
fmt.Printf("Remove JDK %s ...\n", v)
if config.CurrentJDKVersion == v {
os.Remove(config.JavaHome)
}
dir := filepath.Join(config.store, v)
e := os.RemoveAll(dir)
if e != nil {
fmt.Println("Error removing jdk " + v)
fmt.Println("Manually remove " + dir + ".")
} else {
fmt.Printf(" done")
}
} else {
fmt.Println("jdk " + v + " is not installed. Type \"jvms list\" to see what is installed.")
}
return nil
},
},
{
Name: "rls",
Usage: "Show a list of versions available for download. ",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "a",
Usage: "list all the version",
},
},
Action: func(c *cli.Context) error {
if config.Proxy != "" {
web.SetProxy(config.Proxy)
}
versions, err := getJdkVersions()
if err != nil {
return err
}
for i, version := range versions {
fmt.Printf(" %d) %s\n", i+1, version.Version)
if !c.Bool("a") && i >= 9 {
fmt.Println("\nuse \"jvm rls -a\" show all the versions ")
break
}
}
if len(versions) == 0 {
fmt.Println("No availabled jdk veriosn for download.")
}
fmt.Printf("\nFor a complete list, visit %s\n", config.Originalpath)
return nil
},
},
{
Name: "proxy",
Usage: "Set a proxy to use for downloads.",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "show",
Usage: "show proxy.",
},
cli.StringFlag{
Name: "set",
Usage: "set proxy.",
},
},
Action: func(c *cli.Context) error {
if c.Bool("show") {
fmt.Printf("Current proxy: %s\n", config.Proxy)
return nil
}
if c.IsSet("set") {
config.Proxy = c.String("set")
}
return nil
},
},
}
}
func getJavaHome(jdkTempFile string) string {
var javaHome string
fs.WalkDir(os.DirFS(jdkTempFile), ".", func(path string, d fs.DirEntry, err error) error {
if filepath.Base(path) == "javac.exe" {
temPath := strings.Replace(path, "bin/javac.exe", "", -1)
javaHome = filepath.Join(jdkTempFile, temPath)
return fs.SkipDir
}
return nil
})
return javaHome
}
func getJdkVersions() ([]JdkVersion, error) {
jsonContent, err := web.GetRemoteTextFile(config.Originalpath)
if err != nil {
return nil, err
}
var versions []JdkVersion
err = json.Unmarshal([]byte(jsonContent), &versions)
if err != nil {
return nil, err
}
//fmt.Println(versions)
adoptiumJdks := strings.Split(adoptium_jdk_go.ApiListReleases(), "\n")
for _, adoptiumJdkUrl := range adoptiumJdks {
fileSeparatorIndex := strings.LastIndex(adoptiumJdkUrl, "/")
fileName := adoptiumJdkUrl[fileSeparatorIndex+1:]
fileVersion := strings.TrimSuffix(fileName, ".zip")
//fmt.Println(fileVersion)
versions = append(versions, JdkVersion{Version: fileVersion, Url: adoptiumJdkUrl})
}
//Azul JDKs
azulJdks := jdk.AzulJDKs()
for _, azulJdk := range azulJdks {
versions = append(versions, JdkVersion{Version: azulJdk.ShortName, Url: azulJdk.DownloadURL})
}
//fmt.Println(versions)
return versions, nil
}
func startup(c *cli.Context) error {
store.Register(
"json",
func(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
},
json.Unmarshal)
store.Init("jvms")
if err := store.Load("jvms.json", &config); err != nil {
return errors.New("failed to load the config:" + err.Error())
}
s := file.GetCurrentPath()
config.store = filepath.Join(s, "store")
config.download = filepath.Join(s, "download")
if config.Originalpath == "" {
config.Originalpath = defaultOriginalpath
}
if config.Proxy != "" {
web.SetProxy(config.Proxy)
}
return nil
}
func shutdown(c *cli.Context) error {
if err := store.Save("jvms.json", &config); err != nil {
return errors.New("failed to save the config:" + err.Error())
}
return nil
}