Skip to content

Commit

Permalink
refactor: make plugin more easier to maintain (version-fox#83)
Browse files Browse the repository at this point in the history
* refactor: add lua vm struct

* feat: support struct marshal

* feat: add more ctx

* fix: encoding tag map error

* feat: add logger

* feat: add debug flag

* fix: fix decode in mixed struct

* fix: unmarshal to interface is not work

* feat: ctx use marshal

* feat: unmarshal hook result

* ci: update test cases

* test: streamline encoding unit test

* chore: remove print

* test: add more testcases

* chore: revert hook name enum

* chore: add license

* chore: update log message

* refactor: add plugin module

* refactor: add a vm file

* chore: update logger

* chore: add license

* feat: support marshal nil

* fix: preinstall should work

* fix: lua checksum

* chore: update code

* refactor: unmarshal plugin info

* chore: some modifications

* mod: remove debug log in encoding func

* mod: Optimize function calls

* bugfix

* mod

---------

Co-authored-by: lihan <lihan@apache.org>
  • Loading branch information
2 people authored and ShizheChang committed Mar 14, 2024
1 parent 83f2381 commit 72114aa
Show file tree
Hide file tree
Showing 14 changed files with 1,395 additions and 275 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ jobs:
run: |
go build .
- name: Test with the Go CLI
# we cannot use `go test ./...` currently, because many test cases are failed
run: |
go test ./internal
go test ./internal/luai
17 changes: 16 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package cmd

import (
"fmt"
"os"

"github.com/urfave/cli/v2"
"github.com/version-fox/vfox/cmd/commands"
"github.com/version-fox/vfox/internal"
"os"
"github.com/version-fox/vfox/internal/logger"
)

func Execute(args []string) {
Expand Down Expand Up @@ -66,6 +68,19 @@ func newCmd() *cmd {
_, _ = fmt.Fprintln(ctx.App.Writer, command.Name)
}
}

debugFlags := &cli.BoolFlag{
Name: "debug",
Usage: "show debug information",
Action: func(ctx *cli.Context, b bool) error {
logger.SetLevel(logger.DebugLevel)
return nil
},
}

app.Flags = []cli.Flag{
debugFlags,
}
app.Commands = []*cli.Command{
commands.Info,
commands.Install,
Expand Down
169 changes: 169 additions & 0 deletions internal/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* Copyright 2024 Han Li and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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"`
}

type AvailableHookResultItem struct {
Version string `luai:"version"`
Note string `luai:"note"`

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

type AvailableHookResult = []*AvailableHookResultItem

type PreInstallHookCtx struct {
Version string `luai:"version"`
RuntimeVersion string `luai:"runtimeVersion"`
}

type PreInstallHookResultAdditionItem struct {
Name string `luai:"name"`
Url string `luai:"url"`

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: sum.Checksum(),
}
}

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

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

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

func (i *PreInstallHookResult) Info() (*Info, error) {
if i.Version == "" {
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: sum.Checksum(),
}, nil
}

type PreUseHookCtx struct {
RuntimeVersion string `luai:"runtimeVersion"`
Cwd string `luai:"cwd"`
Scope string `luai:"scope"`
Version string `luai:"version"`
PreviousVersion string `luai:"previousVersion"`
InstalledSdks map[string]*Info `luai:"installedSdks"`
}

type PreUseHookResult struct {
Version string `luai:"version"`
}

type PostInstallHookCtx struct {
RuntimeVersion string `luai:"runtimeVersion"`
RootPath string `luai:"rootPath"`
SdkInfo map[string]*Info `luai:"sdkInfo"`
}

type EnvKeysHookCtx struct {
RuntimeVersion string `luai:"runtimeVersion"`
Main *Info `luai:"main"`
// TODO Will be deprecated in future versions
Path string `luai:"path"`
SdkInfo map[string]*Info `luai:"sdkInfo"`
}

type EnvKeysHookResultItem struct {
Key string `luai:"key"`
Value string `luai:"value"`
}

type LuaPluginInfo struct {
Name string `luai:"name"`
Author string `luai:"author"`
Version string `luai:"version"`
Description string `luai:"description"`
UpdateUrl string `luai:"updateUrl"`
MinRuntimeVersion string `luai:"minRuntimeVersion"`
}
70 changes: 70 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2024 Han Li and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package logger

import "fmt"

type LoggerLevel int

// the smaller the level, the more logs.
const (
DebugLevel LoggerLevel = iota
InfoLevel
ErrorLevel
)

var currentLevel = InfoLevel

func SetLevel(_level LoggerLevel) {
currentLevel = _level
}

func Log(level LoggerLevel, args ...interface{}) {
if currentLevel <= level {
fmt.Println(args...)
}
}

func Logf(level LoggerLevel, message string, args ...interface{}) {
if currentLevel <= level {
fmt.Printf(message, args...)
}
}

func Error(message ...interface{}) {
Log(ErrorLevel, message...)
}

func Errorf(message string, args ...interface{}) {
Logf(ErrorLevel, message, args...)
}

func Info(message ...interface{}) {
Log(InfoLevel, message...)
}

func Infof(message string, args ...interface{}) {
Logf(InfoLevel, message, args...)
}

func Debug(args ...interface{}) {
Log(DebugLevel, args...)
}

func Debugf(message string, args ...interface{}) {
Logf(DebugLevel, message, args...)
}
Loading

0 comments on commit 72114aa

Please sign in to comment.