You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.9 KiB
Go

9 months ago
package main
import (
"context"
9 months ago
"encoding/json"
9 months ago
"fmt"
9 months ago
"io/fs"
9 months ago
"os"
"github.com/estebangarcia21/subprocess"
"github.com/wailsapp/wails/v2/pkg/runtime"
9 months ago
)
9 months ago
type Server struct {
Name string `json:"name"`
Dir string `json:"dir"`
Cmd string `json:"cmd"`
Url string `json:"url"`
Port string `json:"port"`
Run string `json:"run"`
}
func (s *Server) Start() {
os.Chdir(s.Dir)
//s := subprocess.New(".\\"+name+".exe")
p := subprocess.New(s.Cmd)
p.ExecAsync()
}
9 months ago
// 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
// runtime.WindowFullscreen(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)
}
9 months ago
func startLocalServer(name, dir, cmd string) {
//os.Chdir("d:\\projects\\" + name);
os.Chdir(dir)
//s := subprocess.New(".\\"+name+".exe")
s := subprocess.New(cmd)
s.ExecAsync()
}
9 months ago
// StartServer - запускает сервер на выполнение
9 months ago
func (a *App) StartServer(s string) string {
runtime.LogInfo(a.ctx, s)
var server Server
if err := json.Unmarshal([]byte(s), &server); err != nil {
9 months ago
runtime.LogError(a.ctx, err.Error())
9 months ago
}
runtime.LogInfo(a.ctx, fmt.Sprintf("server: %v", server))
server.Url = fmt.Sprintf("http://localhost:%s", server.Port)
server.Start()
runtime.BrowserOpenURL(a.ctx, server.Url)
return "starting server"
}
9 months ago
func (a *App) Weed() {
os.Chdir("d:\\projects\\sw4-nano\\util")
//s := subprocess.New(".\\"+name+".exe")
cmd := "py scanpics.py"
s := subprocess.New(cmd)
err := s.Exec()
if err != nil {
runtime.LogError(a.ctx, err.Error())
}
runtime.LogInfo(a.ctx, fmt.Sprintf("команда выполнена успешно"))
}
9 months ago
func getCountPics(dir string) int {
9 months ago
pics, err := os.ReadDir("d:\\pics")
if err != nil {
9 months ago
return -1
9 months ago
}
9 months ago
return len(pics)
}
// The `getCountReady` function is counting the number of files (excluding directories) in a
// specified directory. It uses the `fs.WalkDir` function to walk through the file tree starting
// from the given directory within a file system. For each file encountered during the walk, it
// increments a counter if the file is not a directory. Finally, it returns the total count of
// files found in the directory.
func getCountReady(ctx context.Context, dir string) int {
root := dir
fileSystem := os.DirFS(root)
cnt := 0
fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
runtime.LogError(ctx, err.Error())
}
if !d.IsDir() {
cnt += 1
}
return nil
})
return cnt
}
func (a *App) GetStatus() string {
pics := getCountPics("d:\\pics")
ready := getCountReady(a.ctx, "d:\\pics-move")
return fmt.Sprintf("%d/%d", pics, ready)
9 months ago
}