-
Notifications
You must be signed in to change notification settings - Fork 8
/
list.go
131 lines (118 loc) · 2.82 KB
/
list.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func listRocks(luadir string) {
if err := os.Chdir(luadir); err != nil {
eprintf(err.Error())
return
}
var verUsed string
if dir, err := readSymlink("./lua_modules"); err != nil {
if errors.Is(err, ErrNotSymlink) {
eprintf("%s is not symlink.\nplease remove it yourself", "lua_modules")
}
eprintf("failed to read symlink: %v", err)
return
} else if dir != "" {
if !strings.HasPrefix(dir, "luarocks/") {
eprintf("./lua_modules -> %s is not relative symlink to ./luarocks directory.\nplease remove it yourself", dir)
return
} else if ok, err := isDir(dir); err != nil {
eprintf("failed to check %q type: %v", dir, err)
return
} else if !ok {
eprintf("%s -> %s is not symlink to directory.\nplease remove it yourself", dir, dir)
return
}
verUsed = filepath.Base(filepath.Dir(dir))
}
infos, err := ioutil.ReadDir("./luarocks")
if err != nil {
if !os.IsNotExist(err) {
eprintf("failed to readdir: %v", err)
}
return
}
vers := []string{}
maxlen := 0
for _, info := range infos {
ver := info.Name()
if IsSemVer(ver) {
vers = append(vers, ver)
if len(ver) > maxlen {
maxlen = len(ver)
}
}
}
SortVersions(vers)
if len(vers) > 0 {
format := fmt.Sprintf(" %%s %%-%ds", maxlen)
printf(" └ luarocks")
tail := len(vers) - 1
for i, v := range vers {
c := "├"
if i == tail {
c = "└"
}
if v == verUsed {
printf(format+" (used)", c, v)
} else {
printf(format, c, v)
}
}
}
}
func CmdList() {
for _, cfg := range []*TargetConfig{
LuaCfg, LuaJitCfg,
} {
printf("list installed %s and luarocks versions:", cfg.Name)
infos, err := ioutil.ReadDir(cfg.RootDir)
if err != nil {
eprintf("failed to readdir: %v", err)
continue
}
vers, err := NewVersionsFromFile(cfg.VersionFile)
if err != nil {
eprintf("failed to read version file %q: %v", cfg.VersionFile, err)
continue
}
inst_vers := []string{}
inst_dirs := []string{}
maxlen := 0
for _, info := range infos {
ver := info.Name()
dir := filepath.Join(cfg.RootDir, ver)
// verify version
if item := vers.GetItem(ver); item == nil {
// ignore unknown file/directory
eprintf("ignore unknown version: %s (%q)", ver, dir)
continue
} else if !info.IsDir() {
eprintf("found %s %s (%q) but it is not a directory.\nplease remove it yourself.", cfg.Name, ver, dir)
continue
} else if len(ver) > maxlen {
maxlen = len(ver)
}
inst_dirs = append(inst_dirs, dir)
inst_vers = append(inst_vers, ver)
}
format := fmt.Sprintf("%%-%ds (%%s)", maxlen)
for i, v := range inst_vers {
dir := inst_dirs[i]
if dir == CurrentUsed {
printf(format+" (used)", v, dir)
} else {
printf(format, v, dir)
}
listRocks(dir)
}
printf("")
}
}