From 16064e92d242d477320881fdc0cc77f410801def Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=A2?= =?UTF-8?q?=D1=83=D1=85=D1=82=D0=B0=D1=80=D0=BE=D0=B2?= Date: Fri, 5 Apr 2024 12:16:40 +0500 Subject: [PATCH] cursor --- app.go | 38 +++++++++++++++++++++++++++++++++----- frontend/src/App.svelte | 10 +++++++--- frontend/src/servers.js | 14 +++++++------- main.go | 12 ++++++++++++ 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/app.go b/app.go index 142df3d..8c77334 100644 --- a/app.go +++ b/app.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "io/fs" "os" "github.com/estebangarcia21/subprocess" @@ -56,7 +57,7 @@ func startLocalServer(name, dir, cmd string) { s.ExecAsync() } -// StartServer - запускает сервер на выполение +// StartServer - запускает сервер на выполнение func (a *App) StartServer(s string) string { runtime.LogInfo(a.ctx, s) var server Server @@ -84,11 +85,38 @@ func (a *App) Weed() { runtime.LogInfo(a.ctx, fmt.Sprintf("команда выполнена успешно")) } -func (a *App) GetStatus() string { +func getCountPics(dir string) int { pics, err := os.ReadDir("d:\\pics") if err != nil { - runtime.LogError(a.ctx, err.Error()) - return "error: " + err.Error() + return -1 } - return fmt.Sprint(len(pics)) + 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) } diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index a81cde0..c63a40a 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -33,6 +33,11 @@ GetStatus().then((result) => { console.log("result:", result) cnt_pic = result; + // сразу сбросим признак работающего сервера + local = local.map ( server => { + server.run = "idle" + return server + }) }); } @@ -72,7 +77,7 @@ class="text-teal-600 hover:text-teal-200 hover:font-normal hover:underline hover:underline-offset-2 cursor-pointer" on:click={weed} > - Загрузка в weed + Weed (python)
@@ -82,7 +87,7 @@
- + @@ -110,6 +115,5 @@ - diff --git a/frontend/src/servers.js b/frontend/src/servers.js index 3f2f823..76703f4 100644 --- a/frontend/src/servers.js +++ b/frontend/src/servers.js @@ -1,41 +1,41 @@ const servers = [ { - name: "books-web", + name: "books-web:3000", dir: "d:\\projects\\books-web", cmd: ".\\books-web.exe", port: "3000", run: "idle" }, { - name: "nano-tw", + name: "nano-tw:5321", dir: "d:\\projects\\nano-tw", cmd: ".\\nano-tw.exe", - port: "4321", + port: "5321", run: "idle" }, { - name: "trans-web", + name: "trans-web:4322", dir: "d:\\projects\\trans-web", cmd: ".\\trans-web.exe", port: "4322", run: "idle" }, { - name: "stem", + name: "stem:4323", dir: "d:\\projects\\stem", cmd: ".\\stem.exe", port: "4323", run: "idle" }, { - name: "sw4-nano", + name: "sw4-nano:4321", dir: "d:\\projects\\sw4-nano", cmd: ".\\sw4-nano.exe", port: "4321", run: "idle" }, { - name: "pionier", + name: "pionier:4327", dir: "d:\\projects\\pionier-daisy", cmd: ".\\pionier-daisy.exe", port: "4327", diff --git a/main.go b/main.go index 15e6a39..2f726fd 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,8 @@ package main import ( "embed" + "fmt" + "os" "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/options" @@ -40,3 +42,13 @@ func main() { println("Error:", err.Error()) } } + +// ============================ разные утилиты ========================== + +// Проверяет наличие ошибки +func checkError(src string, err error) { + if err != nil { + fmt.Printf("(%s): %s\n", src, err) + os.Exit(1) + } +}