Skip to content

Commit e0a9d62

Browse files
committed
moved commands
1 parent f28bf66 commit e0a9d62

File tree

12 files changed

+491
-344
lines changed

12 files changed

+491
-344
lines changed

internal/commandCli/cmds.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package commandCli
2+
3+
import (
4+
"github.com/codegangsta/cli"
5+
"github.com/ystyle/jvms/internal/entity"
6+
)
7+
8+
type CommandParams struct {
9+
DefaultOriginalPath string
10+
Config *entity.Config
11+
}
12+
13+
func Commands(cp *CommandParams) []cli.Command {
14+
cmds := []cli.Command{
15+
*init_(cp.DefaultOriginalPath, cp.Config),
16+
*list(cp.Config),
17+
*install(cp.Config),
18+
*switch_(cp.Config),
19+
*remove(cp.Config),
20+
*rls(cp.Config),
21+
*proxy(cp.Config),
22+
}
23+
return cmds
24+
}

internal/commandCli/init.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package commandCli
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
10+
"github.com/codegangsta/cli"
11+
"github.com/ystyle/jvms/internal/entity"
12+
"github.com/ystyle/jvms/utils/file"
13+
)
14+
15+
func init_(defaultOriginalpath string, cfx *entity.Config) *cli.Command {
16+
return &cli.Command{
17+
Name: "init",
18+
Usage: "Initialize config file",
19+
Description: `before init you should clear JAVA_HOME, PATH Environment variable。`,
20+
Flags: []cli.Flag{
21+
cli.StringFlag{
22+
Name: "java_home",
23+
Usage: "the JAVA_HOME location",
24+
Value: filepath.Join(os.Getenv("ProgramFiles"), "jdk"),
25+
},
26+
cli.StringFlag{
27+
Name: "originalpath",
28+
Usage: "the jdk download index file url.",
29+
Value: defaultOriginalpath,
30+
},
31+
},
32+
Action: func(c *cli.Context) error {
33+
if c.IsSet("java_home") || cfx.JavaHome == "" {
34+
cfx.JavaHome = c.String("java_home")
35+
}
36+
cmd := exec.Command("cmd", "/C", "setx", "JAVA_HOME", cfx.JavaHome, "/M")
37+
err := cmd.Run()
38+
if err != nil {
39+
return errors.New("set Environment variable `JAVA_HOME` failure: Please run as admin user")
40+
}
41+
fmt.Println("set `JAVA_HOME` Environment variable to ", cfx.JavaHome)
42+
43+
if c.IsSet("originalpath") || cfx.Originalpath == "" {
44+
cfx.Originalpath = c.String("originalpath")
45+
}
46+
path := fmt.Sprintf(`%s/bin;%s;%s`, cfx.JavaHome, os.Getenv("PATH"), file.GetCurrentPath())
47+
cmd = exec.Command("cmd", "/C", "setx", "path", path, "/m")
48+
err = cmd.Run()
49+
if err != nil {
50+
return errors.New("set Environment variable `PATH` failure: Please run as admin user")
51+
}
52+
fmt.Println("add jvms.exe to `path` Environment variable")
53+
return nil
54+
},
55+
}
56+
57+
}

internal/commandCli/install.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package commandCli
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/codegangsta/cli"
10+
"github.com/ystyle/jvms/internal/entity"
11+
"github.com/ystyle/jvms/utils/file"
12+
"github.com/ystyle/jvms/utils/jdk"
13+
"github.com/ystyle/jvms/utils/web"
14+
)
15+
16+
func install(cfx *entity.Config) *cli.Command {
17+
cmd := &cli.Command{
18+
Name: "install",
19+
ShortName: "i",
20+
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+
}
29+
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+
}
38+
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+
}
45+
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)
51+
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+
}
64+
65+
// Copy the jdk files to the installation directory
66+
temJavaHome := getJavaHome(jdktempfile)
67+
err = os.Rename(temJavaHome, filepath.Join(cfx.Store, v))
68+
if err != nil {
69+
return fmt.Errorf("unzip failed: %w", err)
70+
}
71+
72+
// Remove the temp directory
73+
// may consider keep the temp files here
74+
os.RemoveAll(jdktempfile)
75+
76+
fmt.Println("Installation complete. If you want to use this version, type\n\njvms switch", v)
77+
} else {
78+
fmt.Println("Could not download JDK " + v + " executable.")
79+
}
80+
return nil
81+
}
82+
}
83+
return errors.New("invalid version., Type \"jvms rls\" to see what is available for install")
84+
},
85+
}
86+
return cmd
87+
}

internal/commandCli/list.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package commandCli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/codegangsta/cli"
7+
"github.com/ystyle/jvms/internal/entity"
8+
"github.com/ystyle/jvms/utils/jdk"
9+
)
10+
11+
func list(cfx *entity.Config) *cli.Command {
12+
cmd := &cli.Command{
13+
Name: "list",
14+
ShortName: "ls",
15+
Usage: "List current JDK installations.",
16+
Action: func(c *cli.Context) error {
17+
fmt.Println("Installed jdk (* marks in use):")
18+
v := jdk.GetInstalled(cfx.Store)
19+
for i, version := range v {
20+
str := ""
21+
if cfx.CurrentJDKVersion == version {
22+
str = fmt.Sprintf("%s * %d) %s", str, i+1, version)
23+
} else {
24+
str = fmt.Sprintf("%s %d) %s", str, i+1, version)
25+
}
26+
fmt.Printf(str + "\n")
27+
}
28+
if len(v) == 0 {
29+
fmt.Println("No installations recognized.")
30+
}
31+
return nil
32+
},
33+
}
34+
return cmd
35+
}

internal/commandCli/proxy.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package commandCli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/codegangsta/cli"
7+
"github.com/ystyle/jvms/internal/entity"
8+
)
9+
10+
func proxy(cfx *entity.Config) *cli.Command {
11+
cmd := &cli.Command{
12+
Name: "proxy",
13+
Usage: "Set a proxy to use for downloads.",
14+
Flags: []cli.Flag{
15+
cli.BoolFlag{
16+
Name: "show",
17+
Usage: "show proxy.",
18+
},
19+
cli.StringFlag{
20+
Name: "set",
21+
Usage: "set proxy.",
22+
},
23+
},
24+
Action: func(c *cli.Context) error {
25+
if c.Bool("show") {
26+
fmt.Printf("Current proxy: %s\n", cfx.Proxy)
27+
return nil
28+
}
29+
if c.IsSet("set") {
30+
cfx.Proxy = c.String("set")
31+
}
32+
return nil
33+
},
34+
}
35+
return cmd
36+
}

internal/commandCli/rels.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package commandCli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/codegangsta/cli"
7+
"github.com/ystyle/jvms/internal/entity"
8+
"github.com/ystyle/jvms/utils/web"
9+
)
10+
11+
func rls(cfx *entity.Config) *cli.Command {
12+
cmd := &cli.Command{
13+
Name: "rls",
14+
Usage: "Show a list of versions available for download. ",
15+
Flags: []cli.Flag{
16+
cli.BoolFlag{
17+
Name: "a",
18+
Usage: "list all the version",
19+
},
20+
},
21+
Action: func(c *cli.Context) error {
22+
if cfx.Proxy != "" {
23+
web.SetProxy(cfx.Proxy)
24+
}
25+
versions, err := getJdkVersions(cfx)
26+
if err != nil {
27+
return err
28+
}
29+
for i, version := range versions {
30+
fmt.Printf(" %d) %s\n", i+1, version.Version)
31+
if !c.Bool("a") && i >= 9 {
32+
fmt.Println("\nuse \"jvm rls -a\" show all the versions ")
33+
break
34+
}
35+
}
36+
if len(versions) == 0 {
37+
fmt.Println("No availabled jdk veriosn for download.")
38+
}
39+
40+
fmt.Printf("\nFor a complete list, visit %s\n", cfx.Originalpath)
41+
return nil
42+
},
43+
}
44+
return cmd
45+
}

internal/commandCli/remove.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package commandCli
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/codegangsta/cli"
10+
"github.com/ystyle/jvms/internal/entity"
11+
"github.com/ystyle/jvms/utils/jdk"
12+
)
13+
14+
func remove(cfx *entity.Config) *cli.Command {
15+
cmd := &cli.Command{
16+
Name: "remove",
17+
ShortName: "rm",
18+
Usage: "Remove a specific version.",
19+
Action: func(c *cli.Context) error {
20+
v := c.Args().Get(0)
21+
if v == "" {
22+
return errors.New("you should input a version, Type \"jvms list\" to see what is installed")
23+
}
24+
if jdk.IsVersionInstalled(cfx.Store, v) {
25+
fmt.Printf("Remove JDK %s ...\n", v)
26+
if cfx.CurrentJDKVersion == v {
27+
os.Remove(cfx.JavaHome)
28+
}
29+
dir := filepath.Join(cfx.Store, v)
30+
e := os.RemoveAll(dir)
31+
if e != nil {
32+
fmt.Println("Error removing jdk " + v)
33+
fmt.Println("Manually remove " + dir + ".")
34+
} else {
35+
fmt.Printf(" done")
36+
}
37+
} else {
38+
fmt.Println("jdk " + v + " is not installed. Type \"jvms list\" to see what is installed.")
39+
}
40+
return nil
41+
},
42+
}
43+
return cmd
44+
}

0 commit comments

Comments
 (0)