Skip to content

Commit

Permalink
Merge pull request #30 from mah0x211/add-optional-args-to-path-cmd
Browse files Browse the repository at this point in the history
Add optional argument to path command
  • Loading branch information
mah0x211 committed Dec 21, 2023
2 parents 058d106 + c76256d commit 70068c8
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 36 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
name: Test

on:
push
push:
branches:
- '**'
paths-ignore:
- '**.md'
- 'LICENSE'

jobs:
test:
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Lua Version Manager.

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

```sh
```
$ lenv help

lenv - lua version manager
Expand All @@ -42,7 +42,18 @@ Options:
Commands:
help Show this message
setup Set up required files and directories
path Show the configured paths
path [<target>] Show the configured paths

Note:
The <target> specifier of the above commands can be specified as follows;

lenv path bin ; show the PATH of the current lua environment
lenv path lualib ; show the LUA_PATH of the current lua environment
lenv path luaclib ; show the LUA_CPATH of the current lua environment

if <target> is not specified, all the above paths of the current lua
environment will be shown.

fetch Fetch remote versions
vers List available versions
ls List installed versions
Expand Down Expand Up @@ -71,7 +82,6 @@ Commands:
uninstall <version> Uninstall a <version> of lua
uninstall-lj <version> Uninstall a <version> of luajit
uninstall-rocks <version> Uninstall a <version> of luarocks
```
**NOTE: you must run a `fetch` command at the first. that command will crawling the version files of `Lua`, `LuaJIT` and `LuaRocks` immediately.**
Expand Down
13 changes: 12 additions & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ Options:
Commands:
help Show this message
setup Set up required files and directories
path Show the configured paths
path <target> Show the configured paths
Note:
The <target> specifier of the above commands can be specified as follows;
lenv path bin ; show the PATH of the current lua environment
lenv path lualib ; show the LUA_PATH of the current lua environment
lenv path luaclib ; show the LUA_CPATH of the current lua environment
if <target> is not specified, all the above paths of the current lua
environment will be shown.
fetch Fetch remote versions
vers List available versions
ls List installed versions
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func start() {
CmdSetup()

case "path":
CmdPath()
CmdPath(argv[1:])

case "fetch":
CmdFetch()
Expand Down
81 changes: 52 additions & 29 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,72 @@ import (
"strings"
)

func CmdPath() {
func get_luacpath() string {
prefix := filepath.Clean(CurrentDir+"/lua_modules/luaclib") + "/"
paths := []string{}
for i := 0; i <= 10; i++ {
dir := prefix + strconv.Itoa(i)
paths = append(paths, dir+"/?.so")
}
return strings.Join(paths, ";") + ";;"
}

func get_luapath() string {
prefix := filepath.Clean(CurrentDir+"/lua_modules/lualib") + "/"
paths := []string{}
for i := 0; i <= 10; i++ {
dir := prefix + strconv.Itoa(i)
paths = append(paths, dir+"/?.lua", dir+"/?/init.lua")
}
return strings.Join(paths, ";") + ";;"
}

func get_path() string {
return strings.Join([]string{
filepath.Clean(CurrentDir + "/bin"),
filepath.Clean(CurrentDir + "/lua_modules/bin"),
}, ":")
}

func printAll() {
printf(`
#
# please add the following lenv settings to your environment
#
`)

for _, name := range []string{"PATH", "LUA_PATH", "LUA_CPATH"} {
var (
format string
value string
)

var value string
switch name {
case "PATH":
format = `export %s="%s"`
value = strings.Join([]string{
filepath.Clean(CurrentDir + "/bin"),
filepath.Clean(CurrentDir + "/lua_modules/bin"),
"$PATH",
}, ":")
value = get_path() + ":$PATH"

case "LUA_PATH":
format = `export %s="%s;"`
prefix := filepath.Clean(CurrentDir+"/lua_modules/lualib") + "/"
paths := []string{}
for i := 0; i <= 10; i++ {
dir := prefix + strconv.Itoa(i)
paths = append(paths, dir+"/?.lua", dir+"/?/init.lua")
}
value = strings.Join(paths, ";") + ";"
value = get_luapath()

case "LUA_CPATH":
format = `export %s="%s;"`
prefix := filepath.Clean(CurrentDir+"/lua_modules/luaclib") + "/"
paths := []string{}
for i := 0; i <= 10; i++ {
dir := prefix + strconv.Itoa(i)
paths = append(paths, dir+"/?.so")
}
value = strings.Join(paths, ";") + ";"
value = get_luacpath()
}
printf(format, name, value)
printf(`export %s="%s"`, name, value)
}
printf("")
}

func CmdPath(opts []string) {
if len(opts) > 0 {
switch opts[0] {
case "bin":
printf(get_path())
return

case "lualib":
printf(get_luapath())
return

case "luaclib":
printf(get_luacpath())
return
}
}
printAll()
}
2 changes: 1 addition & 1 deletion path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Test_cmdPath(t *testing.T) {
defer stdout.CloseAll()

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

0 comments on commit 70068c8

Please sign in to comment.