add config

This commit is contained in:
v.mashkin 2025-04-28 00:49:22 +03:00
parent 88a1f0f50f
commit 3330006c3f

View File

@ -1,6 +1,10 @@
package ui package ui
import ( import (
"encoding/json"
"os"
"sync"
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
"fyne.io/fyne/v2/container" "fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog" "fyne.io/fyne/v2/dialog"
@ -9,6 +13,8 @@ import (
"gitea.unprism.ru/KRBL/FemaInstaller/pkg/config" "gitea.unprism.ru/KRBL/FemaInstaller/pkg/config"
) )
var mu = sync.Mutex{}
// MainWindow represents the main application window // MainWindow represents the main application window
type MainWindow struct { type MainWindow struct {
Window fyne.Window Window fyne.Window
@ -24,6 +30,26 @@ type MainWindow struct {
InstallButton *widget.Button 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 // NewMainWindow creates a new main window for the application
func NewMainWindow(app fyne.App, installHandler func(*config.SSHConfig) error) *MainWindow { func NewMainWindow(app fyne.App, installHandler func(*config.SSHConfig) error) *MainWindow {
window := app.NewWindow("Fema Installer") window := app.NewWindow("Fema Installer")
@ -42,6 +68,19 @@ func NewMainWindow(app fyne.App, installHandler func(*config.SSHConfig) error) *
StatusLabel: widget.NewLabel(""), 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) // Disable archive path entry (will be set by file selector)
mainWindow.ArchivePathEntry.Disable() 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) dialog.ShowInformation("Error", "Serial number must be 16 characters", window)
return return
} }
mu.Lock()
jsonData, _ := json.MarshalIndent(config, "", " ")
// Записываем JSON в файл
fileName := "config.json"
os.WriteFile(fileName, jsonData, 0644)
mu.Unlock()
// Perform installation // Perform installation
err := installHandler(config) err := installHandler(config)
if err != nil { if err != nil {