@@ -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}
0 commit comments