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

refactor: make plugin more easier to maintain #83

Merged
merged 33 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
09b7e1c
refactor: add lua vm struct
bytemain Mar 6, 2024
6f462ae
feat: support struct marshal
bytemain Mar 6, 2024
9250116
feat: add more ctx
bytemain Mar 6, 2024
5732fb5
fix: encoding tag map error
bytemain Mar 6, 2024
620e1c0
feat: add logger
bytemain Mar 6, 2024
bad1f1e
feat: add debug flag
bytemain Mar 6, 2024
a9f4f03
fix: fix decode in mixed struct
bytemain Mar 6, 2024
882bb9a
fix: unmarshal to interface is not work
bytemain Mar 6, 2024
0edeb1d
feat: ctx use marshal
bytemain Mar 6, 2024
f99b172
feat: unmarshal hook result
bytemain Mar 6, 2024
4dc97fe
ci: update test cases
bytemain Mar 6, 2024
d76a19c
test: streamline encoding unit test
bytemain Mar 6, 2024
f4b0631
Merge remote-tracking branch 'origin/main' into refactor/make-plugin-…
bytemain Mar 7, 2024
4f7b5d3
chore: remove print
bytemain Mar 7, 2024
1a98f94
test: add more testcases
bytemain Mar 7, 2024
95afe00
chore: revert hook name enum
bytemain Mar 7, 2024
a0feebe
chore: add license
bytemain Mar 7, 2024
92e7071
Merge remote-tracking branch 'origin/main' into refactor/make-plugin-…
bytemain Mar 7, 2024
82d0e17
chore: update log message
bytemain Mar 7, 2024
6617df1
refactor: add plugin module
bytemain Mar 8, 2024
f107888
refactor: add a vm file
bytemain Mar 8, 2024
cb64f1a
chore: update logger
bytemain Mar 8, 2024
2cae92c
chore: add license
bytemain Mar 8, 2024
29560d5
feat: support marshal nil
bytemain Mar 8, 2024
7061f28
fix: preinstall should work
bytemain Mar 8, 2024
4984ff0
fix: lua checksum
bytemain Mar 8, 2024
37a961c
chore: update code
bytemain Mar 8, 2024
f43ddb9
refactor: unmarshal plugin info
bytemain Mar 10, 2024
ad6f9e8
chore: some modifications
aooohan Mar 11, 2024
3729e40
mod: remove debug log in encoding func
aooohan Mar 11, 2024
275bcdb
mod: Optimize function calls
aooohan Mar 11, 2024
2d33ebf
bugfix
aooohan Mar 11, 2024
c44f873
mod
aooohan Mar 11, 2024
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
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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only worked when this flag before any subcommand,

related: #88

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
Loading