diff --git a/app_windows.go b/app.go similarity index 81% rename from app_windows.go rename to app.go index c6e34f2..07556d3 100644 --- a/app_windows.go +++ b/app.go @@ -6,9 +6,7 @@ import ( "fmt" "net/http" "os" - "os/exec" "strings" - "syscall" "github.com/gofiber/fiber/v2/log" "github.com/wailsapp/wails/v2/pkg/runtime" @@ -20,8 +18,6 @@ type App struct { ctx context.Context } -var zrokCommand = "./resources/zrok.exe" - // NewApp creates a new App application struct func NewApp() *App { @@ -71,9 +67,9 @@ func writeToFile(name string, data string) error { } func (a *App) Invite(email string) string { - cmd := exec.Command(zrokCommand, "status") - cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} - output, _ := cmd.Output() + status := []string{"status"} + output, _ := zrokCmd(status).Output() + xurlsStrict := xurls.Strict() find := xurlsStrict.FindAllString(string(output), -1) apiEndpoint := find[0] @@ -88,16 +84,14 @@ func (a *App) Invite(email string) string { } func (a *App) Version() string { - cmd := exec.Command(zrokCommand, "version") - cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} - output, _ := cmd.Output() + version := []string{"version"} + output, _ := zrokCmd(version).Output() return string(output) } func (a *App) Overview() string { - cmd := exec.Command(zrokCommand, "overview") - cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} - output, _ := cmd.Output() + overview := []string{"overview"} + output, _ := zrokCmd(overview).Output() return string(output) } @@ -106,8 +100,7 @@ func (a *App) Zrok(args []string) string { log.Info("Zrok", args) writeToFile("./resources/logs.txt", strings.Join(args, " ")) - cmd := exec.Command(zrokCommand, args...) - cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + cmd := zrokCmd(args) // 创建一个缓冲区来保存标准错误输出 var stdout bytes.Buffer var stderr bytes.Buffer diff --git a/app_linux.go b/app_linux.go deleted file mode 100644 index 984a0bc..0000000 --- a/app_linux.go +++ /dev/null @@ -1,129 +0,0 @@ -package main - -import ( - "bytes" - "context" - "fmt" - "net/http" - "os" - "os/exec" - "strings" - "syscall" - - "github.com/gofiber/fiber/v2/log" - "github.com/wailsapp/wails/v2/pkg/runtime" - "mvdan.cc/xurls/v2" -) - -// App struct -type App struct { - ctx context.Context -} - -var zrokCommand = "./resources/linux-x64-zrok" - - -// 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 -} - -func (a *App) Minimize() { - runtime.WindowMinimise(a.ctx) -} - -func (a *App) Maximize() { - if runtime.WindowIsMaximised(a.ctx) { - runtime.WindowUnmaximise(a.ctx) - } else{ - runtime.WindowMaximise(a.ctx) - } -} - -func (a *App) Quit() { - runtime.Quit(a.ctx) -} - -func (a *App) ChooseFolder() (string, error) { - folder,_ := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{ - Title: "Choose a folder", - }) - return folder,nil -} - -func writeToFile(name string, data string) error { - f, err := os.OpenFile(name, os.O_CREATE | os.O_APPEND | os.O_WRONLY, 0644) - if err != nil { - return err - } - defer f.Close() - _, err = f.WriteString(data+"\n") - if err != nil { - return err - } - return nil -} - -func (a *App) Invite(email string) string { - cmd := exec.Command(zrokCommand, "status") - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} - output, _ := cmd.Output() - xurlsStrict := xurls.Strict() - find := xurlsStrict.FindAllString(string(output), -1) - apiEndpoint := find[0] - - requestBody := []byte(fmt.Sprintf(`{"email": "%s"}`, email)) - resp, err := http.Post(apiEndpoint+"/api/v1/invite", "application/zrok.v1+json", bytes.NewBuffer(requestBody)) - if err != nil { - log.Error("发送请求时出错:", err) - } - defer resp.Body.Close() - return resp.Status -} - -func (a *App) Version() string { - cmd := exec.Command(zrokCommand, "version") - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} - output, _ := cmd.Output() - return string(output) -} - -func (a *App) Overview() string { - cmd := exec.Command(zrokCommand, "overview") - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} - output, _ := cmd.Output() - return string(output) -} - -// Zrok -func (a *App) Zrok(args []string) string { - log.Info("Zrok", args) - writeToFile("./resources/logs.txt", strings.Join(args, " ")) - - cmd := exec.Command(zrokCommand, args...) - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} - // 创建一个缓冲区来保存标准错误输出 - var stdout bytes.Buffer - var stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - // 执行命令 - err := cmd.Run() - if err != nil { - // 如果命令执行出错,则打印标准错误输出 - log.Error("执行命令时出错:", err) - log.Error("标准错误输出:", stderr.String()) - return stderr.String() - } - // 如果命令执行成功,则打印标准输出 - - log.Info("标准输出:", stdout.String()) - return stdout.String() -} - diff --git a/app_unix.go b/app_unix.go deleted file mode 100644 index ad8b3cc..0000000 --- a/app_unix.go +++ /dev/null @@ -1,129 +0,0 @@ -package main - -import ( - "bytes" - "context" - "fmt" - "net/http" - "os" - "os/exec" - "strings" - "syscall" - - "github.com/gofiber/fiber/v2/log" - "github.com/wailsapp/wails/v2/pkg/runtime" - "mvdan.cc/xurls/v2" -) - -// App struct -type App struct { - ctx context.Context -} - -var zrokCommand = "./resources/darwin-x64-zrok" - - -// 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 -} - -func (a *App) Minimize() { - runtime.WindowMinimise(a.ctx) -} - -func (a *App) Maximize() { - if runtime.WindowIsMaximised(a.ctx) { - runtime.WindowUnmaximise(a.ctx) - } else{ - runtime.WindowMaximise(a.ctx) - } -} - -func (a *App) Quit() { - runtime.Quit(a.ctx) -} - -func (a *App) ChooseFolder() (string, error) { - folder,_ := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{ - Title: "Choose a folder", - }) - return folder,nil -} - -func writeToFile(name string, data string) error { - f, err := os.OpenFile(name, os.O_CREATE | os.O_APPEND | os.O_WRONLY, 0644) - if err != nil { - return err - } - defer f.Close() - _, err = f.WriteString(data+"\n") - if err != nil { - return err - } - return nil -} - -func (a *App) Invite(email string) string { - cmd := exec.Command(zrokCommand, "status") - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} - output, _ := cmd.Output() - xurlsStrict := xurls.Strict() - find := xurlsStrict.FindAllString(string(output), -1) - apiEndpoint := find[0] - - requestBody := []byte(fmt.Sprintf(`{"email": "%s"}`, email)) - resp, err := http.Post(apiEndpoint+"/api/v1/invite", "application/zrok.v1+json", bytes.NewBuffer(requestBody)) - if err != nil { - log.Error("发送请求时出错:", err) - } - defer resp.Body.Close() - return resp.Status -} - -func (a *App) Version() string { - cmd := exec.Command(zrokCommand, "version") - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} - output, _ := cmd.Output() - return string(output) -} - -func (a *App) Overview() string { - cmd := exec.Command(zrokCommand, "overview") - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} - output, _ := cmd.Output() - return string(output) -} - -// Zrok -func (a *App) Zrok(args []string) string { - log.Info("Zrok", args) - writeToFile("./resources/logs.txt", strings.Join(args, " ")) - - cmd := exec.Command(zrokCommand, args...) - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} - // 创建一个缓冲区来保存标准错误输出 - var stdout bytes.Buffer - var stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - // 执行命令 - err := cmd.Run() - if err != nil { - // 如果命令执行出错,则打印标准错误输出 - log.Error("执行命令时出错:", err) - log.Error("标准错误输出:", stderr.String()) - return stderr.String() - } - // 如果命令执行成功,则打印标准输出 - - log.Info("标准输出:", stdout.String()) - return stdout.String() -} - diff --git a/zrok_darwin.go b/zrok_darwin.go new file mode 100644 index 0000000..8b32dfb --- /dev/null +++ b/zrok_darwin.go @@ -0,0 +1,13 @@ +package main + +import ( + "os/exec" + "syscall" +) + +func zrokCmd(args []string) (*exec.Cmd) { + zrokBin := "./resources/darwin-x64-zrok" + cmd := exec.Command(zrokBin, args...) + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} + return cmd +} \ No newline at end of file diff --git a/zrok_linux.go b/zrok_linux.go new file mode 100644 index 0000000..1a9e2e1 --- /dev/null +++ b/zrok_linux.go @@ -0,0 +1,13 @@ +package main + +import ( + "os/exec" + "syscall" +) + +func zrokCmd(args []string) (*exec.Cmd) { + zrokBin := "./resources/linux-x64-zrok" + cmd := exec.Command(zrokBin, args...) + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} + return cmd +} \ No newline at end of file diff --git a/zrok_windows.go b/zrok_windows.go new file mode 100644 index 0000000..98719b6 --- /dev/null +++ b/zrok_windows.go @@ -0,0 +1,13 @@ +package main + +import ( + "os/exec" + "syscall" +) + +func zrokCmd(args []string) (*exec.Cmd) { + zrokBin := "./resources/zrok.exe" + cmd := exec.Command(zrokBin, args...) + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + return cmd +} \ No newline at end of file