Skip to content

Commit 5a9be1f

Browse files
committed
fix: systemInfo
1 parent 6938f05 commit 5a9be1f

File tree

9 files changed

+71
-64
lines changed

9 files changed

+71
-64
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
# Yui
2+
OPQBot 的 golang 版本的插件实现
3+
4+
[文档站](https://yui.opqbot.com/)

config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
httpproxy: http://127.0.0.1:7890 # 插件 HTTP 服务时会使用的 Proxy
1+
#httpproxy: http://127.0.0.1:7890 # 插件 HTTP 服务时会使用的 Proxy
22
opqurl: http://127.0.0.1:8086 # OPQ 的地址
33
admin:
44
- 2435932516 # 管理员

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ require (
1212
github.com/mailru/easyjson v0.7.7
1313
github.com/manifoldco/promptui v0.9.0
1414
github.com/mcoo/OPQBot v0.2.2-0.20230415115049-02b00e2cf9e1
15-
github.com/opq-osc/OPQBot/v2 v2.0.0-20230427091225-f73faded2e3b
15+
github.com/opq-osc/OPQBot/v2 v2.0.0-20230501145629-6fc4f36b6a66
1616
github.com/robfig/cron/v3 v3.0.1
17-
github.com/shirou/gopsutil/v3 v3.23.3
17+
github.com/shirou/gopsutil/v3 v3.23.4
1818
github.com/spf13/cast v1.5.0
1919
github.com/spf13/cobra v1.7.0
2020
github.com/spf13/viper v1.15.0
@@ -63,7 +63,7 @@ require (
6363
github.com/quic-go/quic-go v0.32.0 // indirect
6464
github.com/rivo/uniseg v0.2.0 // indirect
6565
github.com/rotisserie/eris v0.5.4 // indirect
66-
github.com/shoenig/go-m1cpu v0.1.4 // indirect
66+
github.com/shoenig/go-m1cpu v0.1.5 // indirect
6767
github.com/spf13/afero v1.9.3 // indirect
6868
github.com/spf13/jwalterweatherman v1.1.0 // indirect
6969
github.com/spf13/pflag v1.0.5 // indirect

plugin/builder/cmd/new.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
44
package cmd
55

66
import (
7+
"bytes"
78
"encoding/json"
9+
"fmt"
810
"github.com/manifoldco/promptui"
9-
"github.com/opq-osc/Yui/plugin/meta"
1011
"github.com/spf13/cobra"
1112
"os"
1213
"os/exec"
1314
"path/filepath"
15+
"regexp"
1416
"strings"
1517
)
1618

@@ -21,25 +23,27 @@ var newCmd = &cobra.Command{
2123
Long: `创建一个新的插件`,
2224
RunE: func(cmd *cobra.Command, args []string) error {
2325
prompt := promptui.Prompt{
24-
Label: "plugin name",
26+
Label: "plugin name",
27+
HideEntered: true,
28+
Validate: ValidAllAlpha,
2529
}
2630
var err error
27-
pluginInfo := &meta.PluginMeta{}
31+
pluginInfo := &BuildMetaInfo{}
2832
pluginInfo.PluginName, err = prompt.Run()
2933
if err != nil {
3034
return err
3135
}
32-
prompt = promptui.Prompt{Label: "plugin description"}
36+
prompt = promptui.Prompt{Label: "plugin description", HideEntered: true}
3337
pluginInfo.Description, err = prompt.Run()
3438
if err != nil {
3539
return err
3640
}
37-
prompt = promptui.Prompt{Label: "author"}
41+
prompt = promptui.Prompt{Label: "author", HideEntered: true}
3842
pluginInfo.Author, err = prompt.Run()
3943
if err != nil {
4044
return err
4145
}
42-
prompt = promptui.Prompt{Label: "author url"}
46+
prompt = promptui.Prompt{Label: "author url", HideEntered: true}
4347
pluginInfo.Url, err = prompt.Run()
4448
if err != nil {
4549
return err
@@ -71,7 +75,12 @@ var newCmd = &cobra.Command{
7175
if err != nil {
7276
return err
7377
}
74-
err = os.WriteFile(filepath.Join(pluginInfo.PluginName, "meta.json"), []byte(metaInfo), 0777)
78+
var buf bytes.Buffer
79+
err = json.Indent(&buf, metaInfo, "", "\t")
80+
if err != nil {
81+
return err
82+
}
83+
err = os.WriteFile(filepath.Join(pluginInfo.PluginName, "meta.json"), buf.Bytes(), 0777)
7584
if err != nil {
7685
return err
7786
}
@@ -93,12 +102,18 @@ func init() {
93102
// newCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
94103
}
95104

105+
func ValidAllAlpha(s string) error {
106+
if match, _ := regexp.MatchString(`^[A-Za-z0-9]+$`, s); match {
107+
return nil
108+
}
109+
return fmt.Errorf("err")
110+
}
111+
96112
var goFile = `//go:build tinygo.wasm
97113
98114
package main
99115
100116
import (
101-
"github.com/opq-osc/Yui/plugin/meta"
102117
"github.com/opq-osc/Yui/proto"
103118
"context"
104119
"github.com/knqyf263/go-plugin/types/known/emptypb"

plugin/builder/main.go.bak

Lines changed: 0 additions & 49 deletions
This file was deleted.

proto/library/systemInfo/systemInfo.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func (s SystemInfo) CpuInfo(ctx context.Context, empty *emptypb.Empty) (*export.
3232
}
3333
return &export.SystemInfoReply{Data: data}, nil
3434
}
35+
3536
func (s SystemInfo) MemInfo(ctx context.Context, empty *emptypb.Empty) (*export.SystemInfoReply, error) {
3637
if s.PluginInfo.Permissions&meta.SystemInfoPermission == 0 {
3738
return nil, fmt.Errorf("缺少获取系统信息权限")
@@ -44,6 +45,25 @@ func (s SystemInfo) MemInfo(ctx context.Context, empty *emptypb.Empty) (*export.
4445
if err != nil {
4546
return nil, err
4647
}
48+
devicesMem, err := mem.SwapDevices()
49+
if err != nil {
50+
return nil, err
51+
}
52+
var devices []*struct {
53+
Name string `json:"name"`
54+
UsedBytes uint64 `json:"usedBytes"`
55+
FreeBytes uint64 `json:"freeBytes"`
56+
}
57+
for _, v := range devicesMem {
58+
devices = append(devices, &struct {
59+
Name string `json:"name"`
60+
UsedBytes uint64 `json:"usedBytes"`
61+
FreeBytes uint64 `json:"freeBytes"`
62+
}{Name: v.Name, UsedBytes: v.UsedBytes, FreeBytes: v.FreeBytes})
63+
}
64+
if err != nil {
65+
return nil, err
66+
}
4767
data, err := (&export.MemInfo{VirtualMemory: (*struct {
4868
Total uint64 `json:"total"`
4969
Available uint64 `json:"available"`
@@ -93,7 +113,7 @@ func (s SystemInfo) MemInfo(ctx context.Context, empty *emptypb.Empty) (*export.
93113
PgOut uint64 `json:"pgOut"`
94114
PgFault uint64 `json:"pgFault"`
95115
PgMajFault uint64 `json:"pgMajFault"`
96-
})(swapMemory)}).MarshalJSON()
116+
})(swapMemory), SwapDevices: devices}).MarshalJSON()
97117
if err != nil {
98118
return nil, err
99119
}

web/docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default defineConfig({
77
markdown: {
88
lineNumbers: true
99
},
10+
lang: 'zh-CN',
1011
themeConfig: {
1112
nav: [
1213
{ text: 'OPQBot', link: 'https://opqbot.com/' },

web/docs/插件编写/环境配置.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,15 @@ sudo npm i wasm-opt -g
3131
- [knqyf263/go-plugin Github](https://github.com/knqyf263/go-plugin#installation)
3232
:::
3333

34+
## 创建插件项目
35+
```bash
36+
Yui-cli new
37+
```
38+
这样会进入交互式创建插件项目,按照提示输入信息即可
39+
40+
3441
## 编译
3542
```bash
36-
yui-cli build ./plugins/signPlugin/signPlugin.go
43+
Yui-cli build signPlugin/signPlugin.go
3744
```
38-
编译完成后会在plugins目录下生成 `signPlugin.opq` 文件
45+
编译完成后会在signPlugin目录下生成 `signPlugin.opq` 文件
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 编译报错
2+
3+
## not implemented: build constraints in #cgo line
4+
```bash
5+
# os/user
6+
G:\Program\go\src\os\user\cgo_lookup_unix.go:18:6: not implemented: build constraints in #cgo line
7+
G:\Program\go\src\os\user\cgo_lookup_unix.go:21:10: fatal: 'pwd.h' file not found
8+
G:\Program\go\src\os\user\getgrouplist_unix.go:12:10: fatal: 'grp.h' file not found
9+
Error: exit status 1
10+
```

0 commit comments

Comments
 (0)