|
| 1 | +/* |
| 2 | +Copyright © 2023 NAME HERE <EMAIL ADDRESS> |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "github.com/manifoldco/promptui" |
| 9 | + "github.com/opq-osc/Yui/plugin/meta" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "os" |
| 12 | + "os/exec" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | +) |
| 16 | + |
| 17 | +// newCmd represents the new command |
| 18 | +var newCmd = &cobra.Command{ |
| 19 | + Use: "new", |
| 20 | + Short: "创建一个新的插件", |
| 21 | + Long: `创建一个新的插件`, |
| 22 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 23 | + prompt := promptui.Prompt{ |
| 24 | + Label: "plugin name", |
| 25 | + } |
| 26 | + var err error |
| 27 | + pluginInfo := &meta.PluginMeta{} |
| 28 | + pluginInfo.PluginName, err = prompt.Run() |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + prompt = promptui.Prompt{Label: "plugin description"} |
| 33 | + pluginInfo.Description, err = prompt.Run() |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + prompt = promptui.Prompt{Label: "author"} |
| 38 | + pluginInfo.Author, err = prompt.Run() |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + prompt = promptui.Prompt{Label: "author url"} |
| 43 | + pluginInfo.Url, err = prompt.Run() |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + err = os.MkdirAll(pluginInfo.PluginName, 0777) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + goFile = strings.ReplaceAll(goFile, "{{.pluginName}}", pluginInfo.PluginName) |
| 52 | + err = os.WriteFile(filepath.Join(pluginInfo.PluginName, pluginInfo.PluginName+".go"), []byte(goFile), 0777) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + GoCmd := exec.Command("go", "mod", "init", pluginInfo.PluginName) |
| 57 | + GoCmd.Dir = "./" + pluginInfo.PluginName |
| 58 | + GoCmd.Stderr = os.Stderr |
| 59 | + err = GoCmd.Run() |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + GoCmd = exec.Command("go", "mod", "tidy") |
| 64 | + GoCmd.Dir = "./" + pluginInfo.PluginName |
| 65 | + GoCmd.Stderr = os.Stderr |
| 66 | + err = GoCmd.Run() |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + metaInfo, err := json.Marshal(pluginInfo) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + err = os.WriteFile(filepath.Join(pluginInfo.PluginName, "meta.json"), []byte(metaInfo), 0777) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + return nil |
| 79 | + }, |
| 80 | +} |
| 81 | + |
| 82 | +func init() { |
| 83 | + rootCmd.AddCommand(newCmd) |
| 84 | + |
| 85 | + // Here you will define your flags and configuration settings. |
| 86 | + |
| 87 | + // Cobra supports Persistent Flags which will work for this command |
| 88 | + // and all subcommands, e.g.: |
| 89 | + // newCmd.PersistentFlags().String("foo", "", "A help for foo") |
| 90 | + |
| 91 | + // Cobra supports local flags which will only run when this command |
| 92 | + // is called directly, e.g.: |
| 93 | + // newCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") |
| 94 | +} |
| 95 | + |
| 96 | +var goFile = `//go:build tinygo.wasm |
| 97 | +
|
| 98 | +package main |
| 99 | +
|
| 100 | +import ( |
| 101 | + "github.com/opq-osc/Yui/plugin/meta" |
| 102 | + "github.com/opq-osc/Yui/proto" |
| 103 | + "context" |
| 104 | + "github.com/knqyf263/go-plugin/types/known/emptypb" |
| 105 | +) |
| 106 | +
|
| 107 | +type {{.pluginName}} struct { |
| 108 | +} |
| 109 | +
|
| 110 | +func (p {{.pluginName}}) OnRemoteCallEvent(ctx context.Context, req *proto.RemoteCallReq) (*proto.RemoteCallReply, error) { |
| 111 | + //TODO implement me |
| 112 | + panic("implement me") |
| 113 | +} |
| 114 | +
|
| 115 | +func (p {{.pluginName}}) OnCronEvent(ctx context.Context, req *proto.CronEventReq) (*emptypb.Empty, error) { |
| 116 | + return &emptypb.Empty{}, nil |
| 117 | +} |
| 118 | +
|
| 119 | +func (p {{.pluginName}}) OnFriendMsg(ctx context.Context, msg *proto.CommonMsg) (*emptypb.Empty, error) { |
| 120 | + return &emptypb.Empty{}, nil |
| 121 | +} |
| 122 | +
|
| 123 | +func (p {{.pluginName}}) OnPrivateMsg(ctx context.Context, msg *proto.CommonMsg) (*emptypb.Empty, error) { |
| 124 | + return &emptypb.Empty{}, nil |
| 125 | +} |
| 126 | +
|
| 127 | +func (p {{.pluginName}}) OnGroupMsg(ctx context.Context, msg *proto.CommonMsg) (*emptypb.Empty, error) { |
| 128 | + return &emptypb.Empty{}, nil |
| 129 | +} |
| 130 | +func (p {{.pluginName}}) Unload(ctx context.Context, _ *emptypb.Empty) (*emptypb.Empty, error) { |
| 131 | + return &emptypb.Empty{}, nil |
| 132 | +
|
| 133 | +} |
| 134 | +func (p {{.pluginName}}) Init(ctx context.Context, _ *emptypb.Empty) (*proto.InitReply, error) { |
| 135 | + return &proto.InitReply{ |
| 136 | + Ok: true, |
| 137 | + Message: "Success", |
| 138 | + }, nil |
| 139 | +} |
| 140 | +
|
| 141 | +func main() { |
| 142 | + proto.RegisterEvent({{.pluginName}}{}) |
| 143 | +} |
| 144 | +` |
0 commit comments