Skip to content

Commit

Permalink
fix: lua checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Mar 8, 2024
1 parent 7061f28 commit 4984ff0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 56 deletions.
34 changes: 29 additions & 5 deletions internal/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package internal

import "fmt"
import (
"fmt"
)

type LuaCheckSum struct {
Sha256 string `luai:"sha256"`
Expand Down Expand Up @@ -66,23 +68,38 @@ type PreInstallHookCtx struct {
type PreInstallHookResultAdditionItem struct {
Name string `luai:"name"`
Url string `luai:"url"`
LuaCheckSum

Sha256 string `luai:"sha256"`
Sha512 string `luai:"sha512"`
Sha1 string `luai:"sha1"`
Md5 string `luai:"md5"`
}

func (i *PreInstallHookResultAdditionItem) Info() *Info {
sum := LuaCheckSum{
Sha256: i.Sha256,
Sha512: i.Sha512,
Sha1: i.Sha1,
Md5: i.Md5,
}

return &Info{
Name: i.Name,
Version: Version(""),
Path: i.Url,
Note: "",
Checksum: i.Checksum(),
Checksum: sum.Checksum(),
}
}

type PreInstallHookResult struct {
Version string `luai:"version"`
Url string `luai:"url"`
LuaCheckSum

Sha256 string `luai:"sha256"`
Sha512 string `luai:"sha512"`
Sha1 string `luai:"sha1"`
Md5 string `luai:"md5"`

Addition []*PreInstallHookResultAdditionItem `luai:"addition"`
}
Expand All @@ -92,12 +109,19 @@ func (i *PreInstallHookResult) Info() (*Info, error) {
return nil, fmt.Errorf("no version number provided")
}

sum := LuaCheckSum{
Sha256: i.Sha256,
Sha512: i.Sha512,
Sha1: i.Sha1,
Md5: i.Md5,
}

return &Info{
Name: "",
Version: Version(i.Version),
Path: i.Url,
Note: "",
Checksum: i.Checksum(),
Checksum: sum.Checksum(),
}, nil
}

Expand Down
50 changes: 0 additions & 50 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,35 +134,6 @@ func (l *LuaPlugin) Available() ([]*Package, error) {
return result, nil
}

func (l *LuaPlugin) Checksum(table *lua.LTable) *Checksum {
luaCheckSum := LuaCheckSum{}
err := luai.Unmarshal(table, luaCheckSum)
if err != nil {
// todo: logger error
return NoneChecksum
}

checksum := &Checksum{}

if luaCheckSum.Sha256 != "" {
checksum.Value = luaCheckSum.Sha256
checksum.Type = "sha256"
} else if luaCheckSum.Md5 != "" {
checksum.Value = luaCheckSum.Md5
checksum.Type = "md5"
} else if luaCheckSum.Sha1 != "" {
checksum.Value = luaCheckSum.Sha1
checksum.Type = "sha1"
} else if luaCheckSum.Sha512 != "" {
checksum.Value = luaCheckSum.Sha512
checksum.Type = "sha512"
} else {
return NoneChecksum
}

return checksum
}

func (l *LuaPlugin) PreInstall(version Version) (*Package, error) {
L := l.vm.Instance
ctxTable, err := luai.Marshal(L, PreInstallHookCtx{
Expand Down Expand Up @@ -212,27 +183,6 @@ func (l *LuaPlugin) PreInstall(version Version) (*Package, error) {
}, nil
}

func (l *LuaPlugin) parseInfo(table *lua.LTable) (*Info, error) {
info := &Info{}
err := luai.Unmarshal(table, info)
if err != nil {
return nil, err
}

if info.Version == "" {
return nil, fmt.Errorf("no version number provided")
}

checksum := l.Checksum(table)
return &Info{
Name: info.Name,
Version: Version(info.Version),
Path: info.Path,
Note: info.Name,
Checksum: checksum,
}, nil
}

func (l *LuaPlugin) PostInstall(rootPath string, sdks []*Info) error {
L := l.vm.Instance

Expand Down
12 changes: 12 additions & 0 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func TestPlugin(t *testing.T) {

Main := pkg.Main

if Main.Version != "version" {
t.Errorf("expected version 'version', got '%s'", Main.Version)
}

if Main.Path != "xxx" {
t.Errorf("expected path 'xxx', got '%s'", Main.Path)
}
Expand All @@ -65,6 +69,14 @@ func TestPlugin(t *testing.T) {
t.Errorf("expected checksum to be set, got nil")
}

if Main.Checksum.Type != "sha256" {
t.Errorf("expected checksum type 'sha256', got '%s'", Main.Checksum.Type)
}

if Main.Checksum.Value != "xxx" {
t.Errorf("expected checksum value 'xxx', got '%s'", Main.Checksum.Value)
}

if len(pkg.Additions) != 1 {
t.Errorf("expected 1 addition, got %d", len(pkg.Additions))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/testdata/plugins/java.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function PLUGIN:PreInstall(ctx)
local runtimeVersion = ctx.runtimeVersion
return {
--- Version number
version = "xxx",
version = "version",
--- remote URL or local file path [optional]
url = "xxx",
--- SHA256 checksum [optional]
Expand Down

0 comments on commit 4984ff0

Please sign in to comment.