-
Notifications
You must be signed in to change notification settings - Fork 8
/
use.go
60 lines (52 loc) · 1.39 KB
/
use.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"os"
"path/filepath"
"strings"
)
func UseInstalledVersion(cfg *TargetConfig, ver string) {
// change workdir
wd := LenvDir
dst := CurrentDir
suffix := ""
if cfg.Name == "luarocks" {
wd = CurrentUsed
dst = filepath.Join(wd, "lua_modules")
suffix = "lua_modules"
}
if err := os.Chdir(wd); err != nil {
fatalf("failed to chdir: %v", err)
}
// change rootdir from absolute to relative
rootdir := strings.TrimPrefix(cfg.RootDir, wd)
if rootdir != cfg.RootDir {
rootdir = filepath.Clean("./" + rootdir)
}
infos, err := os.ReadDir(rootdir)
if err != nil {
fatalf("failed to readdir: %v", err)
}
for _, info := range infos {
if info.Name() == ver {
src := filepath.Join(rootdir, ver, suffix)
if !info.IsDir() {
fatalf("found %s %s (%q) but it is not a directory.\nplease remove it yourself.", cfg.Name, ver, src)
} else if err := createSymlink(src, dst); err != nil {
fatalf("failed to create symlink: %v", err)
}
printf("use %s version %s (%q)", cfg.Name, ver, src)
return
}
}
fatalf("%s version %q is not installed", cfg.Name, ver)
}
func CmdUse(opts []string) {
target := PickTargetVersion(opts[0], false)
if target.Lua != nil {
UseInstalledVersion(target.Lua.Config, target.Lua.Version.Ver)
}
if target.LuaRocks != nil {
CheckLuaRocksRootDir()
UseInstalledVersion(target.LuaRocks.Config, target.LuaRocks.Version.Ver)
}
}