Added "--home" flag ("which" command)

This commit is contained in:
Stanley Shyiko
2017-05-12 21:50:28 -07:00
parent 5dbbf4eb20
commit 5b4e78f756
2 changed files with 31 additions and 22 deletions
+7 -2
View File
@@ -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
}
+24 -20
View File
@@ -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", "", "")