Skip to content

Commit

Permalink
fix: systemInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mcoo committed May 2, 2023
1 parent 6938f05 commit 5a9be1f
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 64 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Yui
OPQBot 的 golang 版本的插件实现

[文档站](https://yui.opqbot.com/)
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -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 # 管理员
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
31 changes: 23 additions & 8 deletions plugin/builder/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
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"
)

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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"
Expand Down
49 changes: 0 additions & 49 deletions plugin/builder/main.go.bak

This file was deleted.

22 changes: 21 additions & 1 deletion proto/library/systemInfo/systemInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("缺少获取系统信息权限")
Expand All @@ -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"`
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions web/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default defineConfig({
markdown: {
lineNumbers: true
},
lang: 'zh-CN',
themeConfig: {
nav: [
{ text: 'OPQBot', link: 'https://opqbot.com/' },
Expand Down
11 changes: 9 additions & 2 deletions web/docs/插件编写/环境配置.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 文件
编译完成后会在signPlugin目录下生成 `signPlugin.opq` 文件
10 changes: 10 additions & 0 deletions web/docs/插件编写/编译报错.md
Original file line number Diff line number Diff line change
@@ -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
```

0 comments on commit 5a9be1f

Please sign in to comment.