@@ -4,18 +4,20 @@ import (
44 "encoding/json"
55 "errors"
66 "fmt"
7- "github.com/baneeishaque/adoptium_jdk_go"
8- "github.com/codegangsta/cli"
9- "github.com/tucnak/store"
10- "github.com/ystyle/jvms/utils/file"
11- "github.com/ystyle/jvms/utils/jdk"
12- "github.com/ystyle/jvms/utils/web"
137 "io/fs"
148 "log"
159 "os"
1610 "os/exec"
1711 "path/filepath"
12+ "strconv"
1813 "strings"
14+
15+ "github.com/baneeishaque/adoptium_jdk_go"
16+ "github.com/codegangsta/cli"
17+ "github.com/tucnak/store"
18+ "github.com/ystyle/jvms/utils/file"
19+ "github.com/ystyle/jvms/utils/jdk"
20+ "github.com/ystyle/jvms/utils/web"
1921)
2022
2123var version = "2.1.0"
@@ -194,12 +196,30 @@ func commands() []cli.Command {
194196 {
195197 Name : "switch" ,
196198 ShortName : "s" ,
197- Usage : "Switch to use the specified version." ,
199+ Usage : "Switch to use the specified version or index number ." ,
198200 Action : func (c * cli.Context ) error {
199201 v := c .Args ().Get (0 )
200202 if v == "" {
201- return errors .New ("you should input a version, Type \" jvms list\" to see what is installed" )
203+ return errors .New ("you should input a version or index number , Type \" jvms list\" to see what is installed" )
202204 }
205+
206+ // Check if input is a number (index)
207+ index , err := strconv .Atoi (v )
208+ if err == nil && index > 0 {
209+ // Input is a valid number, get the list of installed JDKs
210+ installedJDKs := jdk .GetInstalled (config .store )
211+ if len (installedJDKs ) == 0 {
212+ return errors .New ("no JDK installations found" )
213+ }
214+
215+ if index > len (installedJDKs ) {
216+ return fmt .Errorf ("invalid index: %d, should be between 1 and %d" , index , len (installedJDKs ))
217+ }
218+
219+ v = installedJDKs [index - 1 ]
220+ fmt .Printf ("Using index %d to select JDK %s\n " , index , v )
221+ }
222+
203223 if ! jdk .IsVersionInstalled (config .store , v ) {
204224 fmt .Printf ("jdk %s is not installed. " , v )
205225 return nil
@@ -212,7 +232,7 @@ func commands() []cli.Command {
212232 }
213233 }
214234 cmd := exec .Command ("cmd" , "/C" , "setx" , "JAVA_HOME" , config .JavaHome , "/M" )
215- err : = cmd .Run ()
235+ err = cmd .Run ()
216236 if err != nil {
217237 return errors .New ("set Environment variable `JAVA_HOME` failure: Please run as admin user" )
218238 }
0 commit comments