diff --git a/README.md b/README.md index c983fbb..f1fa31a 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ # Yui +OPQBot 的 golang 版本的插件实现 + +[文档站](https://yui.opqbot.com/) \ No newline at end of file diff --git a/config.yaml b/config.yaml index 4c76b27..645f9ef 100644 --- a/config.yaml +++ b/config.yaml @@ -1,4 +1,4 @@ -httpproxy: http://127.0.0.1:7890 # 插件 HTTP 服务时会使用的 Proxy +#httpproxy: http://127.0.0.1:7890 # 插件 HTTP 服务时会使用的 Proxy opqurl: http://127.0.0.1:8086 # OPQ 的地址 admin: - 2435932516 # 管理员 diff --git a/go.mod b/go.mod index 7524506..49f6f63 100644 --- a/go.mod +++ b/go.mod @@ -12,9 +12,9 @@ require ( github.com/mailru/easyjson v0.7.7 github.com/manifoldco/promptui v0.9.0 github.com/mcoo/OPQBot v0.2.2-0.20230415115049-02b00e2cf9e1 - github.com/opq-osc/OPQBot/v2 v2.0.0-20230427091225-f73faded2e3b + github.com/opq-osc/OPQBot/v2 v2.0.0-20230501145629-6fc4f36b6a66 github.com/robfig/cron/v3 v3.0.1 - github.com/shirou/gopsutil/v3 v3.23.3 + github.com/shirou/gopsutil/v3 v3.23.4 github.com/spf13/cast v1.5.0 github.com/spf13/cobra v1.7.0 github.com/spf13/viper v1.15.0 @@ -63,7 +63,7 @@ require ( github.com/quic-go/quic-go v0.32.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/rotisserie/eris v0.5.4 // indirect - github.com/shoenig/go-m1cpu v0.1.4 // indirect + github.com/shoenig/go-m1cpu v0.1.5 // indirect github.com/spf13/afero v1.9.3 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect diff --git a/plugin/builder/cmd/new.go b/plugin/builder/cmd/new.go index 3bf94c5..4cf8f0b 100644 --- a/plugin/builder/cmd/new.go +++ b/plugin/builder/cmd/new.go @@ -4,13 +4,15 @@ Copyright © 2023 NAME HERE package cmd import ( + "bytes" "encoding/json" + "fmt" "github.com/manifoldco/promptui" - "github.com/opq-osc/Yui/plugin/meta" "github.com/spf13/cobra" "os" "os/exec" "path/filepath" + "regexp" "strings" ) @@ -21,25 +23,27 @@ var newCmd = &cobra.Command{ Long: `创建一个新的插件`, RunE: func(cmd *cobra.Command, args []string) error { prompt := promptui.Prompt{ - Label: "plugin name", + Label: "plugin name", + HideEntered: true, + Validate: ValidAllAlpha, } var err error - pluginInfo := &meta.PluginMeta{} + pluginInfo := &BuildMetaInfo{} pluginInfo.PluginName, err = prompt.Run() if err != nil { return err } - prompt = promptui.Prompt{Label: "plugin description"} + prompt = promptui.Prompt{Label: "plugin description", HideEntered: true} pluginInfo.Description, err = prompt.Run() if err != nil { return err } - prompt = promptui.Prompt{Label: "author"} + prompt = promptui.Prompt{Label: "author", HideEntered: true} pluginInfo.Author, err = prompt.Run() if err != nil { return err } - prompt = promptui.Prompt{Label: "author url"} + prompt = promptui.Prompt{Label: "author url", HideEntered: true} pluginInfo.Url, err = prompt.Run() if err != nil { return err @@ -71,7 +75,12 @@ var newCmd = &cobra.Command{ if err != nil { return err } - err = os.WriteFile(filepath.Join(pluginInfo.PluginName, "meta.json"), []byte(metaInfo), 0777) + var buf bytes.Buffer + err = json.Indent(&buf, metaInfo, "", "\t") + if err != nil { + return err + } + err = os.WriteFile(filepath.Join(pluginInfo.PluginName, "meta.json"), buf.Bytes(), 0777) if err != nil { return err } @@ -93,12 +102,18 @@ func init() { // newCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } +func ValidAllAlpha(s string) error { + if match, _ := regexp.MatchString(`^[A-Za-z0-9]+$`, s); match { + return nil + } + return fmt.Errorf("err") +} + var goFile = `//go:build tinygo.wasm package main import ( - "github.com/opq-osc/Yui/plugin/meta" "github.com/opq-osc/Yui/proto" "context" "github.com/knqyf263/go-plugin/types/known/emptypb" diff --git a/plugin/builder/main.go.bak b/plugin/builder/main.go.bak deleted file mode 100644 index 12b462d..0000000 --- a/plugin/builder/main.go.bak +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import ( - "Yui/plugin/meta" - "bytes" - "crypto/sha256" - "encoding/binary" - "encoding/gob" - "encoding/hex" - "io" - "os" -) - -func main() { - b, _ := os.ReadFile("plugins/example.wasm") - s := sha256.New() - s.Write(b) - - m := meta.PluginMeta{ - PluginName: "测试插件", - Author: "enjoy", - Url: "mcenjoy.cn", - Version: 0, - Permissions: meta.SendMsgPermission | meta.HTTPRequestPermission | meta.ReceiveAllMsgPermission | meta.UploadPermission, - Sha256: hex.EncodeToString(s.Sum(nil)), - } - var headerBuf bytes.Buffer - e := gob.NewEncoder(&headerBuf) - if err := e.Encode(m); err != nil { - panic(err) - } - var buf bytes.Buffer - buf.Write([]byte("OPQ")) - if err := binary.Write(&buf, binary.LittleEndian, int32(headerBuf.Len())); err != nil { - panic(err) - } - buf.Write(headerBuf.Bytes()) - buf.Write(b) - f, err := os.OpenFile("plugins/example.opq", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0777) - if err != nil { - panic(err) - } - defer f.Close() - - _, err = io.Copy(f, &buf) - if err != nil { - panic(err) - } -} diff --git a/proto/library/systemInfo/systemInfo.go b/proto/library/systemInfo/systemInfo.go index 88b8c98..ae74bc2 100644 --- a/proto/library/systemInfo/systemInfo.go +++ b/proto/library/systemInfo/systemInfo.go @@ -32,6 +32,7 @@ func (s SystemInfo) CpuInfo(ctx context.Context, empty *emptypb.Empty) (*export. } return &export.SystemInfoReply{Data: data}, nil } + func (s SystemInfo) MemInfo(ctx context.Context, empty *emptypb.Empty) (*export.SystemInfoReply, error) { if s.PluginInfo.Permissions&meta.SystemInfoPermission == 0 { return nil, fmt.Errorf("缺少获取系统信息权限") @@ -44,6 +45,25 @@ func (s SystemInfo) MemInfo(ctx context.Context, empty *emptypb.Empty) (*export. if err != nil { return nil, err } + devicesMem, err := mem.SwapDevices() + if err != nil { + return nil, err + } + var devices []*struct { + Name string `json:"name"` + UsedBytes uint64 `json:"usedBytes"` + FreeBytes uint64 `json:"freeBytes"` + } + for _, v := range devicesMem { + devices = append(devices, &struct { + Name string `json:"name"` + UsedBytes uint64 `json:"usedBytes"` + FreeBytes uint64 `json:"freeBytes"` + }{Name: v.Name, UsedBytes: v.UsedBytes, FreeBytes: v.FreeBytes}) + } + if err != nil { + return nil, err + } data, err := (&export.MemInfo{VirtualMemory: (*struct { Total uint64 `json:"total"` Available uint64 `json:"available"` @@ -93,7 +113,7 @@ func (s SystemInfo) MemInfo(ctx context.Context, empty *emptypb.Empty) (*export. PgOut uint64 `json:"pgOut"` PgFault uint64 `json:"pgFault"` PgMajFault uint64 `json:"pgMajFault"` - })(swapMemory)}).MarshalJSON() + })(swapMemory), SwapDevices: devices}).MarshalJSON() if err != nil { return nil, err } diff --git a/web/docs/.vitepress/config.ts b/web/docs/.vitepress/config.ts index d8639ba..e565b07 100644 --- a/web/docs/.vitepress/config.ts +++ b/web/docs/.vitepress/config.ts @@ -7,6 +7,7 @@ export default defineConfig({ markdown: { lineNumbers: true }, + lang: 'zh-CN', themeConfig: { nav: [ { text: 'OPQBot', link: 'https://opqbot.com/' }, diff --git "a/web/docs/\346\217\222\344\273\266\347\274\226\345\206\231/\347\216\257\345\242\203\351\205\215\347\275\256.md" "b/web/docs/\346\217\222\344\273\266\347\274\226\345\206\231/\347\216\257\345\242\203\351\205\215\347\275\256.md" index 62e0e1a..20c7b35 100644 --- "a/web/docs/\346\217\222\344\273\266\347\274\226\345\206\231/\347\216\257\345\242\203\351\205\215\347\275\256.md" +++ "b/web/docs/\346\217\222\344\273\266\347\274\226\345\206\231/\347\216\257\345\242\203\351\205\215\347\275\256.md" @@ -31,8 +31,15 @@ sudo npm i wasm-opt -g - [knqyf263/go-plugin Github](https://github.com/knqyf263/go-plugin#installation) ::: +## 创建插件项目 +```bash +Yui-cli new +``` +这样会进入交互式创建插件项目,按照提示输入信息即可 + + ## 编译 ```bash -yui-cli build ./plugins/signPlugin/signPlugin.go +Yui-cli build signPlugin/signPlugin.go ``` -编译完成后会在plugins目录下生成 `signPlugin.opq` 文件 \ No newline at end of file +编译完成后会在signPlugin目录下生成 `signPlugin.opq` 文件 \ No newline at end of file diff --git "a/web/docs/\346\217\222\344\273\266\347\274\226\345\206\231/\347\274\226\350\257\221\346\212\245\351\224\231.md" "b/web/docs/\346\217\222\344\273\266\347\274\226\345\206\231/\347\274\226\350\257\221\346\212\245\351\224\231.md" new file mode 100644 index 0000000..feed416 --- /dev/null +++ "b/web/docs/\346\217\222\344\273\266\347\274\226\345\206\231/\347\274\226\350\257\221\346\212\245\351\224\231.md" @@ -0,0 +1,10 @@ +# 编译报错 + +## not implemented: build constraints in #cgo line +```bash +# os/user +G:\Program\go\src\os\user\cgo_lookup_unix.go:18:6: not implemented: build constraints in #cgo line +G:\Program\go\src\os\user\cgo_lookup_unix.go:21:10: fatal: 'pwd.h' file not found +G:\Program\go\src\os\user\getgrouplist_unix.go:12:10: fatal: 'grp.h' file not found +Error: exit status 1 +``` \ No newline at end of file