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

package main
import (
"context"
"encoding/json"
"fmt"
"io/fs"
"os"
"github.com/estebangarcia21/subprocess"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
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()
}
// 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)
}
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()
}
// StartServer - запускает сервер на выполнение
func (a *App) StartServer(s string) string {
runtime.LogInfo(a.ctx, s)
var server Server
if err := json.Unmarshal([]byte(s), &server); err != nil {
runtime.LogError(a.ctx, err.Error())
}
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"
}
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("команда выполнена успешно"))
}
func getCountPics(dir string) int {
pics, err := os.ReadDir("d:\\pics")
if err != nil {
return -1
}
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)
}