RDM-Wails/app.go

134 lines
3.0 KiB
Go

package main
import (
"context"
"fmt"
"log"
"os"
"github.com/duke-git/lancet/v2/fileutil"
"github.com/gookit/goutil/jsonutil"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (a *App) CreateConnect(connect RedisConnect) Result {
dir, err := os.UserHomeDir()
connectFilePath := "/RDMWails/connect.json"
savePath := dir + connectFilePath
if err != nil {
println(err.Error())
return Result{Msg: "保存设置失败," + err.Error()}
}
// marshal, err := jsonutil.Encode(connect)
// if err != nil {
// println(err.Error())
// return Result{Msg: "保存设置失败," + err.Error()}
// }
if !fileutil.IsExist(savePath) {
created := fileutil.CreateFile(savePath)
if !created {
println("创建文件失败")
return Result{Msg: "保存设置失败,创建文件失败"}
}
}
str, err := fileutil.ReadFileToString(savePath)
if err != nil {
return Result{Msg: "保存设置失败,读取配置文件失败"}
}
settingPtr := &SettingStore{}
err = jsonutil.Decode([]byte(str), settingPtr)
if err != nil {
println(err.Error())
return Result{Msg: "保存设置失败,解析配置文件失败"}
}
settingPtr.RedisConnect = append(settingPtr.RedisConnect, connect)
marshal, err := jsonutil.EncodePretty(*settingPtr)
if err != nil {
println(err.Error())
return Result{Msg: "保存设置失败,编码Json失败"}
}
err = fileutil.WriteStringToFile(savePath, string(marshal), false)
if err != nil {
println(err.Error())
return Result{Msg: "保存设置失败," + err.Error()}
}
return Result{Msg: "保存成功"}
}
func (a *App) DeleteConnect(id string) Result {
return Result{}
}
func (a *App) UpdateConnect(connect RedisConnect) Result {
return Result{}
}
func (a *App) GetConnectList() Result {
dir, err := os.UserHomeDir()
result := Result{Msg: "读取配置文件失败"}
if err != nil {
return result
}
connectFilePath := "/RDMWails/connect.json"
targetPath := dir + connectFilePath
exist := fileutil.IsExist(targetPath)
if !exist {
return result
}
config, err := fileutil.ReadFileToString(targetPath)
if err != nil {
return result
}
settingPtr := &SettingStore{}
err = jsonutil.Decode([]byte(config), settingPtr)
if err != nil {
return result
}
return Result{Msg: "读取列表完成", Data: settingPtr.RedisConnect}
}
func (a *App) ConnectRedis(connect RedisConnect) bool {
cli, err := GetRedisClient(connect.Address, connect.Port, connect.Auth, 0, connect.SSHConfig)
if err != nil {
log.Println("连接失败", err.Error())
}
ctx := context.Background()
ping, err := cli.Ping(ctx).Result()
if err != nil || ping != "pong" {
return false
}
return true
}