Skip to content

Commit 590dd12

Browse files
committed
moved commands, added use
1 parent e0a9d62 commit 590dd12

File tree

12 files changed

+108
-83
lines changed

12 files changed

+108
-83
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package commandCli
1+
package cmdCLi
22

33
import (
44
"github.com/codegangsta/cli"
@@ -16,6 +16,7 @@ func Commands(cp *CommandParams) []cli.Command {
1616
*list(cp.Config),
1717
*install(cp.Config),
1818
*switch_(cp.Config),
19+
*use(cp.Config),
1920
*remove(cp.Config),
2021
*rls(cp.Config),
2122
*proxy(cp.Config),
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package commandCli
1+
package cmdCLi
22

33
import (
44
"errors"
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package commandCli
1+
package cmdCLi
22

33
import (
44
"errors"
@@ -72,8 +72,7 @@ func install(cfx *entity.Config) *cli.Command {
7272
// Remove the temp directory
7373
// may consider keep the temp files here
7474
os.RemoveAll(jdktempfile)
75-
76-
fmt.Println("Installation complete. If you want to use this version, type\n\njvms switch", v)
75+
fmt.Printf("Installation completedly succesfully. Use: jvms switch %v, if you'd like to use this version", v)
7776
} else {
7877
fmt.Println("Could not download JDK " + v + " executable.")
7978
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package commandCli
1+
package cmdCLi
22

33
import (
44
"fmt"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package commandCli
1+
package cmdCLi
22

33
import (
44
"fmt"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package commandCli
1+
package cmdCLi
22

33
import (
44
"fmt"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package commandCli
1+
package cmdCLi
22

33
import (
44
"errors"

internal/cmdCLi/switch.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cmdCLi
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"strconv"
10+
11+
"github.com/codegangsta/cli"
12+
"github.com/ystyle/jvms/internal/entity"
13+
"github.com/ystyle/jvms/utils/file"
14+
"github.com/ystyle/jvms/utils/jdk"
15+
)
16+
17+
func switch_(cfx *entity.Config) *cli.Command {
18+
cmd := &cli.Command{
19+
Name: "switch",
20+
ShortName: "s",
21+
Usage: "Switch to use the specified version or index number.",
22+
Action: switchFunc(*cfx),
23+
}
24+
return cmd
25+
}
26+
27+
func switchFunc(cfx entity.Config) func(*cli.Context) error {
28+
return func(c *cli.Context) error {
29+
v := c.Args().Get(0)
30+
if v == "" {
31+
return errors.New("you should input a version or index number, Type \"jvms list\" to see what is installed")
32+
}
33+
34+
// Check if input is a number (index)
35+
index, err := strconv.Atoi(v)
36+
if err == nil && index > 0 {
37+
// Input is a valid number, get the list of installed JDKs
38+
installedJDKs := jdk.GetInstalled(cfx.Store)
39+
if len(installedJDKs) == 0 {
40+
return errors.New("no JDK installations found")
41+
}
42+
43+
if index > len(installedJDKs) {
44+
return fmt.Errorf("invalid index: %d, should be between 1 and %d", index, len(installedJDKs))
45+
}
46+
47+
v = installedJDKs[index-1]
48+
fmt.Printf("Using index %d to select JDK %s\n", index, v)
49+
}
50+
51+
if !jdk.IsVersionInstalled(cfx.Store, v) {
52+
fmt.Printf("jdk %s is not installed. ", v)
53+
return nil
54+
}
55+
// Create or update the symlink
56+
if file.Exists(cfx.JavaHome) {
57+
err := os.Remove(cfx.JavaHome)
58+
if err != nil {
59+
return errors.New("Switch jdk failed, please manually remove " + cfx.JavaHome)
60+
}
61+
}
62+
cmd := exec.Command("cmd", "/C", "setx", "JAVA_HOME", cfx.JavaHome, "/M")
63+
err = cmd.Run()
64+
if err != nil {
65+
return errors.New("set Environment variable `JAVA_HOME` failure: Please run as admin user")
66+
}
67+
err = os.Symlink(filepath.Join(cfx.Store, v), cfx.JavaHome)
68+
if err != nil {
69+
return errors.New("Switch jdk failed, " + err.Error())
70+
}
71+
fmt.Println("Switch success.\nNow using JDK " + v)
72+
cfx.CurrentJDKVersion = v
73+
return nil
74+
}
75+
}

internal/cmdCLi/use.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cmdCLi
2+
3+
import (
4+
"github.com/codegangsta/cli"
5+
"github.com/ystyle/jvms/internal/entity"
6+
)
7+
8+
func use(cfx *entity.Config) *cli.Command {
9+
cmd := &cli.Command{
10+
Name: "use",
11+
ShortName: "u",
12+
Usage: "Switch to use the specified version or index number.",
13+
Action: switchFunc(*cfx),
14+
}
15+
return cmd
16+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package commandCli
1+
package cmdCLi
22

33
import (
44
"encoding/json"
@@ -13,6 +13,11 @@ import (
1313
"github.com/ystyle/jvms/utils/web"
1414
)
1515

16+
// Uses a
17+
func getSimilarAvailableVersions(version string) {
18+
19+
}
20+
1621
func getJavaHome(jdkTempFile string) string {
1722
var javaHome string
1823
fs.WalkDir(os.DirFS(jdkTempFile), ".", func(path string, d fs.DirEntry, err error) error {

0 commit comments

Comments
 (0)