Skip to content

Commit

Permalink
fix: preinstall should work
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Mar 8, 2024
1 parent 29560d5 commit 7061f28
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 28 deletions.
50 changes: 49 additions & 1 deletion internal/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,37 @@

package internal

import "fmt"

type LuaCheckSum struct {
Sha256 string `luai:"sha256"`
Sha512 string `luai:"sha512"`
Sha1 string `luai:"sha1"`
Md5 string `luai:"md5"`
}

func (c *LuaCheckSum) Checksum() *Checksum {
checksum := &Checksum{}

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

return checksum
}

type AvailableHookCtx struct {
RuntimeVersion string `luai:"runtimeVersion"`
}
Expand All @@ -45,12 +69,36 @@ type PreInstallHookResultAdditionItem struct {
LuaCheckSum
}

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

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

Addition []*Info `luai:"addition"`
Addition []*PreInstallHookResultAdditionItem `luai:"addition"`
}

func (i *PreInstallHookResult) Info() (*Info, error) {
if i.Version == "" {
return nil, fmt.Errorf("no version number provided")
}

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

type PreUseHookCtx struct {
Expand Down
8 changes: 4 additions & 4 deletions internal/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ type Package struct {
}

type Info struct {
Name string
Version Version
Path string
Note string
Name string `luai:"name"`
Version Version `luai:"version"`
Path string `luai:"path"`
Note string `luai:"note"`
Checksum *Checksum
}

Expand Down
39 changes: 16 additions & 23 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,35 +182,28 @@ func (l *LuaPlugin) PreInstall(version Version) (*Package, error) {
if table == nil || table.Type() == lua.LTNil {
return nil, nil
}
mainSdk, err := l.parseInfo(table)

result := PreInstallHookResult{}

err = luai.Unmarshal(table, &result)
if err != nil {
return nil, err
}

mainSdk, err := result.Info()
if err != nil {
return nil, err
}
mainSdk.Name = l.Name

var additionalArr []*Info
additions := table.RawGetString("addition")
if tb, ok := additions.(*lua.LTable); ok && tb.Len() != 0 {
var err error
additions.(*lua.LTable).ForEach(func(key lua.LValue, value lua.LValue) {
kvTable, ok := value.(*lua.LTable)
if !ok {
err = fmt.Errorf("the return value is not a table")
return
}
info, err := l.parseInfo(kvTable)
if err != nil {
return
}
if info.Name == "" {
err = fmt.Errorf("additional file no name provided")
// todo: logger error
return
}
additionalArr = append(additionalArr, info)
})
if err != nil {
return nil, err

for i, addition := range result.Addition {
if addition.Name == "" {
return nil, fmt.Errorf("[PreInstall] additional file %d no name provided", i+1)
}

additionalArr = append(additionalArr, addition.Info())
}

return &Package{
Expand Down
38 changes: 38 additions & 0 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,44 @@ func TestPlugin(t *testing.T) {
}
})

t.Run("PreInstall", func(t *testing.T) {
manager := NewSdkManager()
plugin, err := NewLuaPlugin(pluginContent, pluginPath, manager)
if err != nil {
t.Fatal(err)
}

pkg, err := plugin.PreInstall(Version("9.0.0"))
if err != nil {
t.Fatal(err)
}

Main := pkg.Main

if Main.Path != "xxx" {
t.Errorf("expected path 'xxx', got '%s'", Main.Path)
}

// checksum should be existed
if Main.Checksum == nil {
t.Errorf("expected checksum to be set, got nil")
}

if len(pkg.Additions) != 1 {
t.Errorf("expected 1 addition, got %d", len(pkg.Additions))
}

addition := pkg.Additions[0]

if addition.Path != "xxx" {
t.Errorf("expected path 'xxx', got '%s'", addition.Path)
}

if addition.Checksum == nil {
t.Errorf("expected checksum to be set, got nil")
}
})

t.Run("EnvKeys", func(t *testing.T) {
manager := NewSdkManager()

Expand Down

0 comments on commit 7061f28

Please sign in to comment.