mirror of
https://github.com/wahyd4/jabba.git
synced 2026-08-09 04:46:17 +10:00
Added "--home" flag ("which" command)
This commit is contained in:
+7
-2
@@ -3,9 +3,10 @@ package command
|
||||
import (
|
||||
"github.com/shyiko/jabba/cfg"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func Which(selector string) (string, error) {
|
||||
func Which(selector string, home bool) (string, error) {
|
||||
aliasValue := GetAlias(selector)
|
||||
if aliasValue != "" {
|
||||
selector = aliasValue
|
||||
@@ -14,5 +15,9 @@ func Which(selector string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(cfg.Dir(), "jdk", ver), nil
|
||||
path := filepath.Join(cfg.Dir(), "jdk", ver)
|
||||
if home && runtime.GOOS == "darwin" {
|
||||
path = filepath.Join(path, "Contents", "Home")
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
@@ -64,6 +64,29 @@ func main() {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
var whichHome bool
|
||||
whichCmd := &cobra.Command{
|
||||
Use: "which [version]",
|
||||
Short: "Display path to installed JDK",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var ver string
|
||||
if len(args) == 0 {
|
||||
ver = rc().JDK
|
||||
if ver == "" {
|
||||
return pflag.ErrHelp
|
||||
}
|
||||
} else {
|
||||
ver = args[0]
|
||||
}
|
||||
dir, _ := command.Which(ver, whichHome)
|
||||
if dir != "" {
|
||||
fmt.Println(dir)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
whichCmd.Flags().BoolVarP(&whichHome, "home", "", false,
|
||||
"Account for platform differences so that value could be used as JAVA_HOME (e.g. append \"/Contents/Home\" on macOS)")
|
||||
rootCmd.AddCommand(
|
||||
&cobra.Command{
|
||||
Use: "install [version to install]",
|
||||
@@ -247,26 +270,7 @@ func main() {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
&cobra.Command{
|
||||
Use: "which [version]",
|
||||
Short: "Display path to installed JDK",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var ver string
|
||||
if len(args) == 0 {
|
||||
ver = rc().JDK
|
||||
if ver == "" {
|
||||
return pflag.ErrHelp
|
||||
}
|
||||
} else {
|
||||
ver = args[0]
|
||||
}
|
||||
dir, _ := command.Which(ver)
|
||||
if dir != "" {
|
||||
fmt.Println(dir)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
whichCmd,
|
||||
)
|
||||
rootCmd.Flags().Bool("version", false, "version of jabba")
|
||||
rootCmd.PersistentFlags().String("fd3", "", "")
|
||||
|
||||
Reference in New Issue
Block a user