Skip to content

Commit d537546

Browse files
authored
Merge pull request #65 from renniemaharaj/new
Switch command 'as_path' flag
2 parents 1bcc6b5 + 2cc5ad2 commit d537546

File tree

10 files changed

+160
-113
lines changed

10 files changed

+160
-113
lines changed

internal/cmdCli/init.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/ystyle/jvms/utils/file"
1313
)
1414

15-
func init_(defaultOriginalpath string, cfx *entity.Config) *cli.Command {
15+
func init_(defaultOriginalpath string, config *entity.Config) *cli.Command {
1616
return &cli.Command{
1717
Name: "init",
1818
Usage: "Initialize config file",
@@ -30,20 +30,20 @@ func init_(defaultOriginalpath string, cfx *entity.Config) *cli.Command {
3030
},
3131
},
3232
Action: func(c *cli.Context) error {
33-
if c.IsSet("java_home") || cfx.JavaHome == "" {
34-
cfx.JavaHome = c.String("java_home")
33+
if c.IsSet("java_home") || config.JavaHome == "" {
34+
config.JavaHome = c.String("java_home")
3535
}
36-
cmd := exec.Command("cmd", "/C", "setx", "JAVA_HOME", cfx.JavaHome, "/M")
36+
cmd := exec.Command("cmd", "/C", "setx", "JAVA_HOME", config.JavaHome, "/M")
3737
err := cmd.Run()
3838
if err != nil {
3939
return errors.New("set Environment variable `JAVA_HOME` failure: Please run as admin user")
4040
}
41-
fmt.Println("set `JAVA_HOME` Environment variable to ", cfx.JavaHome)
41+
fmt.Println("set `JAVA_HOME` Environment variable to ", config.JavaHome)
4242

43-
if c.IsSet("originalpath") || cfx.Originalpath == "" {
44-
cfx.Originalpath = c.String("originalpath")
43+
if c.IsSet("originalpath") || config.Originalpath == "" {
44+
config.Originalpath = c.String("originalpath")
4545
}
46-
path := fmt.Sprintf(`%s/bin;%s;%s`, cfx.JavaHome, os.Getenv("PATH"), file.GetCurrentPath())
46+
path := fmt.Sprintf(`%s/bin;%s;%s`, config.JavaHome, os.Getenv("PATH"), file.GetCurrentPath())
4747
cmd = exec.Command("cmd", "/C", "setx", "path", path, "/m")
4848
err = cmd.Run()
4949
if err != nil {

internal/cmdCli/install.go

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,78 @@ import (
1313
"github.com/ystyle/jvms/utils/web"
1414
)
1515

16-
func install(cfx *entity.Config) *cli.Command {
16+
func install(config *entity.Config) *cli.Command {
1717
cmd := &cli.Command{
1818
Name: "install",
1919
ShortName: "i",
2020
Usage: "Install available remote jdk",
21-
Action: func(c *cli.Context) error {
22-
if cfx.Proxy != "" {
23-
web.SetProxy(cfx.Proxy)
24-
}
25-
v := c.Args().Get(0)
26-
if v == "" {
27-
return errors.New("invalid version., Type \"jvms rls\" to see what is available for install")
28-
}
21+
Action: installFunc(config),
22+
}
23+
return cmd
24+
}
2925

30-
if jdk.IsVersionInstalled(cfx.Store, v) {
31-
fmt.Println("Version " + v + " is already installed.")
32-
return nil
33-
}
34-
versions, err := getJdkVersions(cfx)
35-
if err != nil {
36-
return err
37-
}
26+
func installFunc(config *entity.Config) func(*cli.Context) error {
27+
return func(c *cli.Context) error {
28+
if config.Proxy != "" {
29+
web.SetProxy(config.Proxy)
30+
}
31+
v := c.Args().Get(0)
32+
if v == "" {
33+
return errors.New("invalid version., Type \"jvms rls\" to see what is available for install")
34+
}
3835

39-
if !file.Exists(cfx.Download) {
40-
os.MkdirAll(cfx.Download, 0777)
41-
}
42-
if !file.Exists(cfx.Store) {
43-
os.MkdirAll(cfx.Store, 0777)
44-
}
36+
if jdk.IsVersionInstalled(config.Store, v) {
37+
fmt.Println("Version " + v + " is already installed.")
38+
return nil
39+
}
40+
versions, err := getJdkVersions(config)
41+
if err != nil {
42+
return err
43+
}
4544

46-
for _, version := range versions {
47-
if version.Version == v {
48-
dlzipfile, success := web.GetJDK(cfx.Download, v, version.Url)
49-
if success {
50-
fmt.Printf("Installing JDK %s ...\n", v)
45+
if !file.Exists(config.Download) {
46+
os.MkdirAll(config.Download, 0777)
47+
}
48+
if !file.Exists(config.Store) {
49+
os.MkdirAll(config.Store, 0777)
50+
}
5151

52-
// Extract jdk to the temp directory
53-
jdktempfile := filepath.Join(cfx.Download, fmt.Sprintf("%s_temp", v))
54-
if file.Exists(jdktempfile) {
55-
err := os.RemoveAll(jdktempfile)
56-
if err != nil {
57-
panic(err)
58-
}
59-
}
60-
err := file.Unzip(dlzipfile, jdktempfile)
61-
if err != nil {
62-
return fmt.Errorf("unzip failed: %w", err)
63-
}
52+
for _, version := range versions {
53+
if version.Version == v {
54+
dlzipfile, success := web.GetJDK(config.Download, v, version.Url)
55+
if success {
56+
fmt.Printf("Installing JDK %s ...\n", v)
6457

65-
// Copy the jdk files to the installation directory
66-
temJavaHome := getJavaHome(jdktempfile)
67-
err = os.Rename(temJavaHome, filepath.Join(cfx.Store, v))
58+
// Extract jdk to the temp directory
59+
jdktempfile := filepath.Join(config.Download, fmt.Sprintf("%s_temp", v))
60+
if file.Exists(jdktempfile) {
61+
err := os.RemoveAll(jdktempfile)
6862
if err != nil {
69-
return fmt.Errorf("unzip failed: %w", err)
63+
panic(err)
7064
}
65+
}
66+
err := file.Unzip(dlzipfile, jdktempfile)
67+
if err != nil {
68+
return fmt.Errorf("unzip failed: %w", err)
69+
}
7170

72-
// Remove the temp directory
73-
// may consider keep the temp files here
74-
os.RemoveAll(jdktempfile)
75-
fmt.Printf("Installation completedly succesfully. Use: jvms switch %v, if you'd like to use this version", v)
76-
} else {
77-
fmt.Println("Could not download JDK " + v + " executable.")
71+
// Copy the jdk files to the installation directory
72+
temJavaHome := getJavaHome(jdktempfile)
73+
err = os.Rename(temJavaHome, filepath.Join(config.Store, v))
74+
if err != nil {
75+
return fmt.Errorf("unzip failed: %w", err)
7876
}
79-
return nil
77+
78+
// Remove the temp directory
79+
// may consider keep the temp files here
80+
os.RemoveAll(jdktempfile)
81+
fmt.Printf("Installation completedly succesfully. Use: jvms switch %v, if you'd like to use this version", v)
82+
} else {
83+
fmt.Println("Could not download JDK " + v + " executable.")
8084
}
85+
return nil
8186
}
82-
return errors.New("invalid version., Type \"jvms rls\" to see what is available for install")
83-
},
87+
}
88+
return errors.New("invalid version., Type \"jvms rls\" to see what is available for install")
8489
}
85-
return cmd
8690
}

internal/cmdCli/list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import (
88
"github.com/ystyle/jvms/utils/jdk"
99
)
1010

11-
func list(cfx *entity.Config) *cli.Command {
11+
func list(config *entity.Config) *cli.Command {
1212
cmd := &cli.Command{
1313
Name: "list",
1414
ShortName: "ls",
1515
Usage: "List current JDK installations.",
1616
Action: func(c *cli.Context) error {
1717
fmt.Println("Installed jdk (* marks in use):")
18-
v := jdk.GetInstalled(cfx.Store)
18+
v := jdk.GetInstalled(config.Store)
1919
for i, version := range v {
2020
str := ""
21-
if cfx.CurrentJDKVersion == version {
21+
if config.CurrentJDKVersion == version {
2222
str = fmt.Sprintf("%s * %d) %s", str, i+1, version)
2323
} else {
2424
str = fmt.Sprintf("%s %d) %s", str, i+1, version)

internal/cmdCli/proxy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/ystyle/jvms/internal/entity"
88
)
99

10-
func proxy(cfx *entity.Config) *cli.Command {
10+
func proxy(config *entity.Config) *cli.Command {
1111
cmd := &cli.Command{
1212
Name: "proxy",
1313
Usage: "Set a proxy to use for downloads.",
@@ -23,11 +23,11 @@ func proxy(cfx *entity.Config) *cli.Command {
2323
},
2424
Action: func(c *cli.Context) error {
2525
if c.Bool("show") {
26-
fmt.Printf("Current proxy: %s\n", cfx.Proxy)
26+
fmt.Printf("Current proxy: %s\n", config.Proxy)
2727
return nil
2828
}
2929
if c.IsSet("set") {
30-
cfx.Proxy = c.String("set")
30+
config.Proxy = c.String("set")
3131
}
3232
return nil
3333
},

internal/cmdCli/rels.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/ystyle/jvms/utils/web"
99
)
1010

11-
func rls(cfx *entity.Config) *cli.Command {
11+
func rls(config *entity.Config) *cli.Command {
1212
cmd := &cli.Command{
1313
Name: "rls",
1414
Usage: "Show a list of versions available for download. ",
@@ -19,10 +19,10 @@ func rls(cfx *entity.Config) *cli.Command {
1919
},
2020
},
2121
Action: func(c *cli.Context) error {
22-
if cfx.Proxy != "" {
23-
web.SetProxy(cfx.Proxy)
22+
if config.Proxy != "" {
23+
web.SetProxy(config.Proxy)
2424
}
25-
versions, err := getJdkVersions(cfx)
25+
versions, err := getJdkVersions(config)
2626
if err != nil {
2727
return err
2828
}
@@ -37,7 +37,7 @@ func rls(cfx *entity.Config) *cli.Command {
3737
fmt.Println("No availabled jdk veriosn for download.")
3838
}
3939

40-
fmt.Printf("\nFor a complete list, visit %s\n", cfx.Originalpath)
40+
fmt.Printf("\nFor a complete list, visit %s\n", config.Originalpath)
4141
return nil
4242
},
4343
}

internal/cmdCli/remove.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/ystyle/jvms/utils/jdk"
1212
)
1313

14-
func remove(cfx *entity.Config) *cli.Command {
14+
func remove(config *entity.Config) *cli.Command {
1515
cmd := &cli.Command{
1616
Name: "remove",
1717
ShortName: "rm",
@@ -21,12 +21,12 @@ func remove(cfx *entity.Config) *cli.Command {
2121
if v == "" {
2222
return errors.New("you should input a version, Type \"jvms list\" to see what is installed")
2323
}
24-
if jdk.IsVersionInstalled(cfx.Store, v) {
24+
if jdk.IsVersionInstalled(config.Store, v) {
2525
fmt.Printf("Remove JDK %s ...\n", v)
26-
if cfx.CurrentJDKVersion == v {
27-
os.Remove(cfx.JavaHome)
26+
if config.CurrentJDKVersion == v {
27+
os.Remove(config.JavaHome)
2828
}
29-
dir := filepath.Join(cfx.Store, v)
29+
dir := filepath.Join(config.Store, v)
3030
e := os.RemoveAll(dir)
3131
if e != nil {
3232
fmt.Println("Error removing jdk " + v)

internal/cmdCli/switch.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,33 @@ import (
1515
"github.com/ystyle/jvms/utils/jdk"
1616
)
1717

18-
func switch_(cfx *entity.Config) *cli.Command {
18+
var asPathUsage = "Interpret the argument as a direct path rather than a version or index number."
19+
20+
// Shared flags between switch and use commands
21+
var switchFlags = []cli.Flag{
22+
cli.BoolFlag{
23+
Name: "as_path",
24+
Usage: asPathUsage,
25+
},
26+
cli.BoolFlag{
27+
Name: "p",
28+
Usage: asPathUsage,
29+
},
30+
}
31+
32+
func switch_(config *entity.Config) *cli.Command {
1933
cmd := &cli.Command{
2034
Name: "switch",
2135
ShortName: "s",
2236
Usage: "Switch to use the specified version or index number.",
23-
Action: switchFunc(cfx),
37+
Flags: switchFlags,
38+
Action: switchFunc(config),
2439
}
2540
return cmd
2641
}
2742

2843
// SwitchFunc is used by both switch and use commands
29-
func switchFunc(cfx *entity.Config) func(*cli.Context) error {
44+
func switchFunc(config *entity.Config) func(*cli.Context) error {
3045
return func(c *cli.Context) error {
3146
v := strings.TrimSpace(c.Args().Get(0))
3247
if v == "" {
@@ -36,42 +51,46 @@ func switchFunc(cfx *entity.Config) func(*cli.Context) error {
3651
// Check if input is a number (index)
3752
index, err := strconv.Atoi(v)
3853
if err == nil && index > 0 {
39-
// Input is a valid number, get the list of installed JDKs
40-
installedJDKs := jdk.GetInstalled(cfx.Store)
41-
if len(installedJDKs) == 0 {
42-
return errors.New("no JDK installations found")
54+
asPath := c.Bool("as_path")
55+
if !asPath {
56+
asPath = c.Bool("p")
4357
}
4458

45-
if index > len(installedJDKs) {
46-
return fmt.Errorf("invalid index: %d, should be between 1 and %d", index, len(installedJDKs))
59+
// If not as_path, try index expansion
60+
if !asPath {
61+
installed := jdk.GetInstalled(config.Store)
62+
if len(installed) == 0 {
63+
return errors.New("no JDK installations found")
64+
}
65+
if index > len(installed) {
66+
return fmt.Errorf("invalid index: %d, should be between 1 and %d", index, len(installed))
67+
}
68+
v = installed[index-1]
69+
fmt.Printf("Using index %d to select JDK %s\n", index, v)
4770
}
48-
49-
v = installedJDKs[index-1]
50-
fmt.Printf("Using index %d to select JDK %s\n", index, v)
5171
}
52-
53-
if !jdk.IsVersionInstalled(cfx.Store, v) {
72+
if !jdk.IsVersionInstalled(config.Store, v) {
5473
fmt.Printf("jdk %s is not installed. ", v)
5574
return nil
5675
}
5776
// Create or update the symlink
58-
if file.Exists(cfx.JavaHome) {
59-
err := os.Remove(cfx.JavaHome)
77+
if file.Exists(config.JavaHome) {
78+
err := os.Remove(config.JavaHome)
6079
if err != nil {
61-
return errors.New("Switch jdk failed, please manually remove " + cfx.JavaHome)
80+
return errors.New("Switch jdk failed, please manually remove " + config.JavaHome)
6281
}
6382
}
64-
cmd := exec.Command("cmd", "/C", "setx", "JAVA_HOME", cfx.JavaHome, "/M")
83+
cmd := exec.Command("cmd", "/C", "setx", "JAVA_HOME", config.JavaHome, "/M")
6584
err = cmd.Run()
6685
if err != nil {
6786
return errors.New("set Environment variable `JAVA_HOME` failure: Please run as admin user")
6887
}
69-
err = os.Symlink(filepath.Join(cfx.Store, v), cfx.JavaHome)
88+
err = os.Symlink(filepath.Join(config.Store, v), config.JavaHome)
7089
if err != nil {
7190
return errors.New("Switch jdk failed, " + err.Error())
7291
}
7392
fmt.Println("Switch success.\nNow using JDK " + v)
74-
cfx.CurrentJDKVersion = v
93+
config.CurrentJDKVersion = v
7594
return nil
7695
}
7796
}

0 commit comments

Comments
 (0)