From 3330006c3f5676e249cd68847f17f78ec9b9e9ac Mon Sep 17 00:00:00 2001 From: "v.mashkin" Date: Mon, 28 Apr 2025 00:49:22 +0300 Subject: [PATCH] add config --- internal/ui/main_window.go | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/internal/ui/main_window.go b/internal/ui/main_window.go index a2074ce..d76a724 100644 --- a/internal/ui/main_window.go +++ b/internal/ui/main_window.go @@ -1,6 +1,10 @@ package ui import ( + "encoding/json" + "os" + "sync" + "fyne.io/fyne/v2" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/dialog" @@ -9,6 +13,8 @@ import ( "gitea.unprism.ru/KRBL/FemaInstaller/pkg/config" ) +var mu = sync.Mutex{} + // MainWindow represents the main application window type MainWindow struct { Window fyne.Window @@ -24,6 +30,26 @@ type MainWindow struct { InstallButton *widget.Button } +func loadConfig() *config.SSHConfig { + fileName := "config.json" + if _, err := os.Stat(fileName); os.IsNotExist(err) { + return nil + } + + data, err := os.ReadFile(fileName) + if err != nil { + return nil + } + + var cfg config.SSHConfig + err = json.Unmarshal(data, &cfg) + if err != nil { + return nil + } + + return &cfg +} + // NewMainWindow creates a new main window for the application func NewMainWindow(app fyne.App, installHandler func(*config.SSHConfig) error) *MainWindow { window := app.NewWindow("Fema Installer") @@ -42,6 +68,19 @@ func NewMainWindow(app fyne.App, installHandler func(*config.SSHConfig) error) * StatusLabel: widget.NewLabel(""), } + cfg := loadConfig() + + if cfg != nil { + mainWindow.IPEntry.SetText(cfg.IP) + mainWindow.PortEntry.SetText(cfg.Port) + mainWindow.LoginEntry.SetText(cfg.Login) + mainWindow.PasswordEntry.SetText(cfg.Password) + mainWindow.SerialEntry.SetText(cfg.Serial) + mainWindow.TailNumberEntry.SetText(cfg.TailNumber) + mainWindow.DefaultHostEntry.SetText(cfg.DefaultHost) + mainWindow.ArchivePathEntry.SetText(cfg.ArchivePath) + } + // Disable archive path entry (will be set by file selector) mainWindow.ArchivePathEntry.Disable() @@ -73,7 +112,13 @@ func NewMainWindow(app fyne.App, installHandler func(*config.SSHConfig) error) * dialog.ShowInformation("Error", "Serial number must be 16 characters", window) return } + mu.Lock() + jsonData, _ := json.MarshalIndent(config, "", " ") + // Записываем JSON в файл + fileName := "config.json" + os.WriteFile(fileName, jsonData, 0644) + mu.Unlock() // Perform installation err := installHandler(config) if err != nil {