Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the install command behavior #19

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -37,9 +37,9 @@ Usage:
lenv fetch Fetch remote versions
lenv vers List available versions
lenv ls List installed versions
lenv install <version> <opt...> Install a version <version> of lua
lenv install-lj <version> <opt...> Install a version <version> of luajit
lenv install-rocks <version> Install a version <version> of lurocks in
lenv install <version> <opt...> Install and use a <version> of lua
lenv install-lj <version> <opt...> Install and use a <version> of luajit
lenv install-rocks <version> Install and use a <version> of lurocks in
current lua environment
lenv uninstall <version> Uninstall a version <version> of lua
lenv uninstall-lj <version> Uninstall a version <version> of luajit
@@ -66,8 +66,6 @@ the following example are installing the Lua 5.1.5.
```sh
$ lenv install 5.1.5 macosx
...snip...
$ lenv use 5.1.5
use lua version 5.1.5 ("lua/5.1.5")
$ lua -v
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
```
@@ -76,7 +74,7 @@ the following example are installing the LuaJIT 2.0.4.

```sh
$ lenv install-lj 2.0.4
$ lenv use-lj 2.0.4
...snip...
$ lua -v
LuaJIT 2.0.4 -- Copyright (C) 2005-2015 Mike Pall. http://luajit.org/
```
@@ -88,8 +86,6 @@ $ lenv use 5.1.5
use lua version 5.1.5 ("lua/5.1.5")
$ lenv install-rocks 3.5.0
...snip...
$ lenv use-rocks 3.5.0
use luarocks version 3.5.0 ("luarocks/3.5.0/lua_modules")
$ luarocks version
/Users/mah/.lenv/current/lua_modules/bin/luarocks 3.5.0
LuaRocks main command-line interface
2 changes: 1 addition & 1 deletion fetch.go
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ func parseLuaVers(body []byte) *Versions {
return vers
}

func cmdFetch() {
func CmdFetch() {
for _, target := range []struct {
cfg *TargetConfig
parse ParseFunc
8 changes: 4 additions & 4 deletions help.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import (

var osExit = os.Exit

func cmdHelp(rc int, msgs ...interface{}) {
func CmdHelp(rc int, msgs ...interface{}) {
// print passed messages
if len(msgs) > 0 {
fmtstr, ok := msgs[0].(string)
@@ -26,9 +26,9 @@ Usage:
lenv fetch Fetch remote versions
lenv vers List available versions
lenv ls List installed versions
lenv install <version> <opt...> Install a version <version> of lua
lenv install-lj <version> <opt...> Install a version <version> of luajit
lenv install-rocks <version> Install a version <version> of lurocks in
lenv install <version> <opt...> Install and use a <version> of lua
lenv install-lj <version> <opt...> Install and use a <version> of luajit
lenv install-rocks <version> Install and use a <version> of lurocks in
current lua environment
lenv uninstall <version> Uninstall a version <version> of lua
lenv uninstall-lj <version> Uninstall a version <version> of luajit
2 changes: 1 addition & 1 deletion help_test.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ func Test_cmdHelp(t *testing.T) {
defer stdout.CloseAll()

// test that cmdHelp output to stdout and exit with rc
cmdHelp(123, "message")
CmdHelp(123, "message")
stdout.Close()
exit.Close()
assert.Equalf(t, 123, exit.Code, "not equal")
7 changes: 5 additions & 2 deletions install.go
Original file line number Diff line number Diff line change
@@ -318,10 +318,10 @@ func installLua(instdir string, opts []string) error {
return doExec("make", "install", "INSTALL_TOP="+instdir)
}

func cmdInstall(cfg *TargetConfig, opts []string) {
func CmdInstall(cfg *TargetConfig, opts []string) {
// check target version
if len(opts) == 0 {
cmdHelp(1, "no version specified")
CmdHelp(1, "no version specified")
}

vers, err := NewVersionsFromFile(cfg.VersionFile)
@@ -382,4 +382,7 @@ func cmdInstall(cfg *TargetConfig, opts []string) {

printf("")
printf("%s version %s (%q) has been installed.", cfg.Name, ver, instdir)

// automatically use the installed version
UseInstalledVersion(cfg, ver)
}
2 changes: 1 addition & 1 deletion list.go
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ func listRocks(luadir string) {
}
}

func cmdList() {
func CmdList() {
for _, cfg := range []*TargetConfig{
LuaCfg, LuaJitCfg,
} {
36 changes: 18 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
@@ -236,7 +236,7 @@ func checkInitialized() {
ERROR: the required directory does not exists.
please run 'lenv setup' before use.
`)
cmdHelp(1)
CmdHelp(1)
}
}

@@ -250,66 +250,66 @@ func start() {
argv := os.Args[1:]
if len(argv) == 0 {
checkInitialized()
cmdHelp(0)
CmdHelp(0)
} else if argv[0] != "setup" {
checkInitialized()
}

switch argv[0] {
case "help":
cmdHelp(0)
CmdHelp(0)

case "setup":
cmdSetup()
CmdSetup()

case "path":
cmdPath()
CmdPath()

case "fetch":
cmdFetch()
CmdFetch()

case "vers":
cmdVers()
CmdVers()

case "ls":
cmdList()
CmdList()

case "install":
cmdInstall(LuaCfg, argv[1:])
CmdInstall(LuaCfg, argv[1:])

case "install-lj":
argv = argv[1:]
if runtime.GOOS == "darwin" {
argv = append(argv, "MACOSX_DEPLOYMENT_TARGET=10.6")
}
cmdInstall(LuaJitCfg, argv)
CmdInstall(LuaJitCfg, argv)

case "install-rocks":
checkLuaRocksRootDir()
cmdInstall(LuaRocksCfg, argv[1:])
CmdInstall(LuaRocksCfg, argv[1:])

case "uninstall":
cmdUninstall(LuaCfg, argv[1:])
CmdUninstall(LuaCfg, argv[1:])

case "uninstall-lj":
cmdUninstall(LuaJitCfg, argv[1:])
CmdUninstall(LuaJitCfg, argv[1:])

case "uninstall-rocks":
checkLuaRocksRootDir()
cmdUninstall(LuaRocksCfg, argv[1:])
CmdUninstall(LuaRocksCfg, argv[1:])

case "use":
cmdUse(LuaCfg, argv[1:])
CmdUse(LuaCfg, argv[1:])

case "use-lj":
cmdUse(LuaJitCfg, argv[1:])
CmdUse(LuaJitCfg, argv[1:])

case "use-rocks":
checkLuaRocksRootDir()
cmdUse(LuaRocksCfg, argv[1:])
CmdUse(LuaRocksCfg, argv[1:])

default:
cmdHelp(1, "unknown command %q", argv[0])
CmdHelp(1, "unknown command %q", argv[0])
}
}

2 changes: 1 addition & 1 deletion path.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import (
"strings"
)

func cmdPath() {
func CmdPath() {
printf(`
#
# please add the following lenv settings to your environment
2 changes: 1 addition & 1 deletion path_test.go
Original file line number Diff line number Diff line change
@@ -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()
var b bytes.Buffer
if _, err := b.ReadFrom(stdout.Close()); err != nil {
t.Fatalf("failed to read from pipe: %v", err)
2 changes: 1 addition & 1 deletion setup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

func cmdSetup() {
func CmdSetup() {
printf("creating the required directories...")
// create required directories
for _, dir := range []string{
4 changes: 2 additions & 2 deletions uninstall.go
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@ import (
"path/filepath"
)

func cmdUninstall(cfg *TargetConfig, opts []string) {
func CmdUninstall(cfg *TargetConfig, opts []string) {
// check target version
if len(opts) == 0 {
cmdHelp(1, "no version specified")
CmdHelp(1, "no version specified")
}

vers, err := NewVersionsFromFile(cfg.VersionFile)
40 changes: 22 additions & 18 deletions use.go
Original file line number Diff line number Diff line change
@@ -7,23 +7,7 @@ import (
"strings"
)

func cmdUse(cfg *TargetConfig, opts []string) {
// check target version
if len(opts) == 0 {
cmdHelp(1, "no version specified")
}

vers, err := NewVersionsFromFile(cfg.VersionFile)
if err != nil {
fatalf("failed to read version file %q: %v", cfg.VersionFile, err)
}

ver := opts[0]
item := vers.GetItem(ver)
if item == nil {
fatalf("%s version %q does not defined in %q", cfg.Name, ver, cfg.VersionFile)
}

func UseInstalledVersion(cfg *TargetConfig, ver string) {
// change workdir
wd := LenvDir
dst := CurrentDir
@@ -33,7 +17,8 @@ func cmdUse(cfg *TargetConfig, opts []string) {
dst = filepath.Join(wd, "lua_modules")
suffix = "lua_modules"
}
if err = os.Chdir(wd); err != nil {

if err := os.Chdir(wd); err != nil {
fatalf("failed to chdir: %v", err)
}

@@ -63,3 +48,22 @@ func cmdUse(cfg *TargetConfig, opts []string) {

fatalf("%s version %q is not installed", cfg.Name, ver)
}

func CmdUse(cfg *TargetConfig, opts []string) {
// check target version
if len(opts) == 0 {
CmdHelp(1, "no version specified")
}

vers, err := NewVersionsFromFile(cfg.VersionFile)
if err != nil {
fatalf("failed to read version file %q: %v", cfg.VersionFile, err)
}

ver := opts[0]
if vers.GetItem(ver) == nil {
fatalf("%s version %q does not defined in %q", cfg.Name, ver, cfg.VersionFile)
}

UseInstalledVersion(cfg, ver)
}
2 changes: 1 addition & 1 deletion vers.go
Original file line number Diff line number Diff line change
@@ -238,7 +238,7 @@ func (vers *Versions) GetList() (VerItems, int) {
return vitems, maxlen
}

func cmdVers() {
func CmdVers() {
for _, cfg := range []*TargetConfig{
LuaCfg, LuaJitCfg, LuaRocksCfg,
} {