forked from version-fox/vfox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make plugin more easier to maintain (version-fox#83)
* 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
1 parent
83f2381
commit 72114aa
Showing
14 changed files
with
1,395 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
} |
Oops, something went wrong.