diff --git a/command/which.go b/command/which.go index 9b85cc4..b3edcb5 100644 --- a/command/which.go +++ b/command/which.go @@ -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 } diff --git a/jabba.go b/jabba.go index 1edaf95..cb1ef3e 100644 --- a/jabba.go +++ b/jabba.go @@ -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", "", "")