Skip to content

Commit

Permalink
adapt for linux and macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
lerte committed Apr 12, 2024
1 parent 441f035 commit d39f9ac
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 1,526 deletions.
129 changes: 129 additions & 0 deletions app_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
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()
}

129 changes: 129 additions & 0 deletions app_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
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()
}

File renamed without changes.
2 changes: 1 addition & 1 deletion build/windows/installer/project.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ SectionEnd

Section "Zrok"
SetOutPath $INSTDIR\resources
File /r ".\..\..\..\resources\*.*"
File /r ".\..\..\..\resources\zroke.exe"
SectionEnd

Section "uninstall"
Expand Down
Loading

0 comments on commit d39f9ac

Please sign in to comment.