mirror of
https://github.com/wahyd4/jabba.git
synced 2026-08-09 04:46:17 +10:00
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package command
|
|
|
|
import (
|
|
"github.com/shyiko/jabba/cfg"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"runtime"
|
|
)
|
|
|
|
func Use(selector string) ([]string, error) {
|
|
aliasValue := GetAlias(selector)
|
|
if aliasValue != "" {
|
|
selector = aliasValue
|
|
}
|
|
ver, err := LsBestMatch(selector)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return usePath(filepath.Join(cfg.Dir(), "jdk", ver))
|
|
}
|
|
|
|
func usePath(path string) ([]string, error) {
|
|
path, err := filepath.Abs(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pth, _ := os.LookupEnv("PATH")
|
|
rgxp := regexp.MustCompile(regexp.QuoteMeta(filepath.Join(cfg.Dir(), "jdk")) + "[^:]+[:]")
|
|
// strip references to ~/.jabba/jdk/*, otherwise leave unchanged
|
|
pth = rgxp.ReplaceAllString(pth, "")
|
|
if runtime.GOOS == "darwin" {
|
|
path = filepath.Join(path, "Contents", "Home")
|
|
}
|
|
systemJavaHome, overrideWasSet := os.LookupEnv("JAVA_HOME_BEFORE_JABBA")
|
|
if !overrideWasSet {
|
|
systemJavaHome, _ = os.LookupEnv("JAVA_HOME")
|
|
}
|
|
return []string{
|
|
"export PATH=\"" + filepath.Join(path, "bin") + string(os.PathListSeparator) + pth + "\"",
|
|
"export JAVA_HOME=\"" + path + "\"",
|
|
"export JAVA_HOME_BEFORE_JABBA=\"" + systemJavaHome + "\"",
|
|
}, nil
|
|
}
|