Skip to content

Commit

Permalink
Merge pull request #23 from mah0x211/change-the-use-command-behavior
Browse files Browse the repository at this point in the history
Change the use command behavior
  • Loading branch information
mah0x211 authored Jun 5, 2023
2 parents a8d5c61 + e0d4550 commit 2846ba8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ Usage:
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 use <version> Use a <version> of lua
lenv use-lj <version> Use a <version> of luajit
lenv use-rocks <version> Use a <version> of luajit
Note:
The <version> specifier of the install, install-lj and install-rocks commands
can be specified as follows;
The <version> specifier of the above commands can be specified as follows;
lenv install latest ; that picks the latest version
lenv install 5 ; that picks the latest minor version and patch version
Expand All @@ -60,9 +62,6 @@ Usage:
lenv uninstall <version> Uninstall a <version> of lua
lenv uninstall-lj <version> Uninstall a <version> of luajit
lenv uninstall-rocks <version> Uninstall a <version> of luarocks
lenv use <version> Use a <version> of lua
lenv use-lj <version> Use a <version> of luajit
lenv use-rocks <version> Use a <version> of luajit
```

Expand Down
9 changes: 4 additions & 5 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ Usage:
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 use <version> Use a <version> of lua
lenv use-lj <version> Use a <version> of luajit
lenv use-rocks <version> Use a <version> of luajit
Note:
The <version> specifier of the install, install-lj and install-rocks commands
can be specified as follows;
The <version> specifier of the above commands can be specified as follows;
lenv install latest ; that picks the latest version
lenv install 5 ; that picks the latest minor version and patch version
Expand All @@ -49,9 +51,6 @@ Usage:
lenv uninstall <version> Uninstall a <version> of lua
lenv uninstall-lj <version> Uninstall a <version> of luajit
lenv uninstall-rocks <version> Uninstall a <version> of luarocks
lenv use <version> Use a <version> of lua
lenv use-lj <version> Use a <version> of luajit
lenv use-rocks <version> Use a <version> of luajit
`)
osExit(rc)
}
23 changes: 2 additions & 21 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,25 +399,6 @@ func doInstall(cfg *TargetConfig, item *VerItem, opts []string) {
UseInstalledVersion(cfg, item.Ver)
}

func pickTargetVersionItem(cfg *TargetConfig, ver string) *VerItem {
print("check %s version %q definition ... ", cfg.Name, ver)
vers, err := NewVersionsFromFile(cfg.VersionFile)
if err != nil {
fatalf("failed to read version file %q: %v", cfg.VersionFile, err)
}

item := vers.PickItem(ver)
if item == nil {
printf("not found")
fatalf("%s version %q does not defined in %q\n%s", cfg.Name, ver, cfg.VersionFile, ListTargetVersions(cfg))
} else if item.Ext != ".tar.gz" {
fatalf("unsupported media-type %q", item.Name)
}
printf("found %q", item.Ver)

return item
}

func CmdInstall(cfg *TargetConfig, opts []string) {
// check target version
if len(opts) == 0 || (cfg != LuaRocksCfg && opts[0] == ":") {
Expand All @@ -436,11 +417,11 @@ func CmdInstall(cfg *TargetConfig, opts []string) {

var verItem *VerItem
if len(ver) > 0 {
verItem = pickTargetVersionItem(cfg, ver)
verItem = PickTargetVersionItem(cfg, ver)
}
var rocksItem *VerItem
if len(rocksVer) > 0 {
rocksItem = pickTargetVersionItem(LuaRocksCfg, rocksVer)
rocksItem = PickTargetVersionItem(LuaRocksCfg, rocksVer)
}

if verItem != nil {
Expand Down
30 changes: 22 additions & 8 deletions use.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,33 @@ func UseInstalledVersion(cfg *TargetConfig, ver string) {

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

vers, err := NewVersionsFromFile(cfg.VersionFile)
if err != nil {
fatalf("failed to read version file %q: %v", cfg.VersionFile, err)
// check :<luarocks-version>
var rocksVer string
if cfg != LuaRocksCfg {
if delim := strings.Index(ver, ":"); delim != -1 {
rocksVer = ver[delim+1:]
ver = ver[:delim]
}
}

ver := opts[0]
if vers.GetItem(ver) == nil {
fatalf("%s version %q does not defined in %q", cfg.Name, ver, cfg.VersionFile)
var verItem *VerItem
if len(ver) > 0 {
verItem = PickTargetVersionItem(cfg, ver)
}
var rocksItem *VerItem
if len(rocksVer) > 0 {
rocksItem = PickTargetVersionItem(LuaRocksCfg, rocksVer)
}

UseInstalledVersion(cfg, ver)
if verItem != nil {
UseInstalledVersion(cfg, verItem.Ver)
}
if rocksItem != nil {
UseInstalledVersion(LuaRocksCfg, rocksItem.Ver)
}
}
19 changes: 19 additions & 0 deletions vers.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ func ListTargetVersions(cfg *TargetConfig) string {
return b.String()
}

func PickTargetVersionItem(cfg *TargetConfig, ver string) *VerItem {
print("check %s version %q definition ... ", cfg.Name, ver)
vers, err := NewVersionsFromFile(cfg.VersionFile)
if err != nil {
fatalf("failed to read version file %q: %v", cfg.VersionFile, err)
}

item := vers.PickItem(ver)
if item == nil {
printf("not found")
fatalf("%s version %q does not defined in %q\n%s", cfg.Name, ver, cfg.VersionFile, ListTargetVersions(cfg))
} else if item.Ext != ".tar.gz" {
fatalf("unsupported media-type %q", item.Name)
}
printf("found %q", item.Ver)

return item
}

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

0 comments on commit 2846ba8

Please sign in to comment.