Skip to content

Commit f5fd56f

Browse files
committed
Add optional argument to path command
1 parent 665b8f5 commit f5fd56f

File tree

5 files changed

+79
-35
lines changed

5 files changed

+79
-35
lines changed

README.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Lua Version Manager.
2828

2929
please run a `help` command to show the help message.
3030

31-
```sh
31+
```
3232
$ lenv help
3333

3434
lenv - lua version manager
@@ -42,7 +42,18 @@ Options:
4242
Commands:
4343
help Show this message
4444
setup Set up required files and directories
45-
path Show the configured paths
45+
path [<target>] Show the configured paths
46+
47+
Note:
48+
The <target> specifier of the above commands can be specified as follows;
49+
50+
lenv path bin ; show the PATH of the current lua environment
51+
lenv path lualib ; show the LUA_PATH of the current lua environment
52+
lenv path luaclib ; show the LUA_CPATH of the current lua environment
53+
54+
if <target> is not specified, all the above paths of the current lua
55+
environment will be shown.
56+
4657
fetch Fetch remote versions
4758
vers List available versions
4859
ls List installed versions
@@ -71,7 +82,6 @@ Commands:
7182
uninstall <version> Uninstall a <version> of lua
7283
uninstall-lj <version> Uninstall a <version> of luajit
7384
uninstall-rocks <version> Uninstall a <version> of luarocks
74-
7585
```
7686
7787
**NOTE: you must run a `fetch` command at the first. that command will crawling the version files of `Lua`, `LuaJIT` and `LuaRocks` immediately.**

help.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,18 @@ Options:
2828
Commands:
2929
help Show this message
3030
setup Set up required files and directories
31-
path Show the configured paths
31+
path <target> Show the configured paths
32+
33+
Note:
34+
The <target> specifier of the above commands can be specified as follows;
35+
36+
lenv path bin ; show the PATH of the current lua environment
37+
lenv path lualib ; show the LUA_PATH of the current lua environment
38+
lenv path luaclib ; show the LUA_CPATH of the current lua environment
39+
40+
if <target> is not specified, all the above paths of the current lua
41+
environment will be shown.
42+
3243
fetch Fetch remote versions
3344
vers List available versions
3445
ls List installed versions

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func start() {
284284
CmdSetup()
285285

286286
case "path":
287-
CmdPath()
287+
CmdPath(argv[1:])
288288

289289
case "fetch":
290290
CmdFetch()

path.go

+52-29
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,72 @@ import (
66
"strings"
77
)
88

9-
func CmdPath() {
9+
func get_luacpath() string {
10+
prefix := filepath.Clean(CurrentDir+"/lua_modules/luaclib") + "/"
11+
paths := []string{}
12+
for i := 0; i <= 10; i++ {
13+
dir := prefix + strconv.Itoa(i)
14+
paths = append(paths, dir+"/?.so")
15+
}
16+
return strings.Join(paths, ";") + ";;"
17+
}
18+
19+
func get_luapath() string {
20+
prefix := filepath.Clean(CurrentDir+"/lua_modules/lualib") + "/"
21+
paths := []string{}
22+
for i := 0; i <= 10; i++ {
23+
dir := prefix + strconv.Itoa(i)
24+
paths = append(paths, dir+"/?.lua", dir+"/?/init.lua")
25+
}
26+
return strings.Join(paths, ";") + ";;"
27+
}
28+
29+
func get_path() string {
30+
return strings.Join([]string{
31+
filepath.Clean(CurrentDir + "/bin"),
32+
filepath.Clean(CurrentDir + "/lua_modules/bin"),
33+
}, ":")
34+
}
35+
36+
func printAll() {
1037
printf(`
1138
#
1239
# please add the following lenv settings to your environment
1340
#
1441
`)
1542

1643
for _, name := range []string{"PATH", "LUA_PATH", "LUA_CPATH"} {
17-
var (
18-
format string
19-
value string
20-
)
21-
44+
var value string
2245
switch name {
2346
case "PATH":
24-
format = `export %s="%s"`
25-
value = strings.Join([]string{
26-
filepath.Clean(CurrentDir + "/bin"),
27-
filepath.Clean(CurrentDir + "/lua_modules/bin"),
28-
"$PATH",
29-
}, ":")
47+
value = get_path() + ":$PATH"
3048

3149
case "LUA_PATH":
32-
format = `export %s="%s;"`
33-
prefix := filepath.Clean(CurrentDir+"/lua_modules/lualib") + "/"
34-
paths := []string{}
35-
for i := 0; i <= 10; i++ {
36-
dir := prefix + strconv.Itoa(i)
37-
paths = append(paths, dir+"/?.lua", dir+"/?/init.lua")
38-
}
39-
value = strings.Join(paths, ";") + ";"
50+
value = get_luapath()
4051

4152
case "LUA_CPATH":
42-
format = `export %s="%s;"`
43-
prefix := filepath.Clean(CurrentDir+"/lua_modules/luaclib") + "/"
44-
paths := []string{}
45-
for i := 0; i <= 10; i++ {
46-
dir := prefix + strconv.Itoa(i)
47-
paths = append(paths, dir+"/?.so")
48-
}
49-
value = strings.Join(paths, ";") + ";"
53+
value = get_luacpath()
5054
}
51-
printf(format, name, value)
55+
printf(`export %s="%s"`, name, value)
5256
}
5357
printf("")
5458
}
59+
60+
func CmdPath(opts []string) {
61+
if len(opts) > 0 {
62+
switch opts[0] {
63+
case "bin":
64+
printf(get_path())
65+
return
66+
67+
case "lualib":
68+
printf(get_luapath())
69+
return
70+
71+
case "luaclib":
72+
printf(get_luacpath())
73+
return
74+
}
75+
}
76+
printAll()
77+
}

path_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func Test_cmdPath(t *testing.T) {
1212
defer stdout.CloseAll()
1313

1414
// test that cmdPath outputs PATH, LUA_PATH and LUA_CPATH to stdout
15-
CmdPath()
15+
CmdPath(nil)
1616
var b bytes.Buffer
1717
if _, err := b.ReadFrom(stdout.Close()); err != nil {
1818
t.Fatalf("failed to read from pipe: %v", err)

0 commit comments

Comments
 (0)