FemaInstaller/pkg/config/config.go

62 lines
1.7 KiB
Go

package config
import (
"fmt"
"os"
"strings"
)
// Config represents the application configuration
type Config struct {
DefaultSettings string
}
// SSHConfig holds SSH connection and installation parameters
type SSHConfig struct {
IP string
Port string
Login string
Password string
Serial string
TailNumber string
DefaultHost string
ArchivePath string
}
// NewSSHConfig creates a new SSH configuration with the provided parameters
func NewSSHConfig(ip, port, login, password, serial, tailNumber, defaultHost, archivePath string) *SSHConfig {
return &SSHConfig{
IP: ip,
Port: port,
Login: login,
Password: password,
Serial: serial,
TailNumber: tailNumber,
DefaultHost: defaultHost,
ArchivePath: archivePath,
}
}
// UpdateSettingsJSON updates the default settings JSON with user-provided values
func UpdateSettingsJSON(defaultSettings string, config *SSHConfig) (string, error) {
// Replace registration number
updated := strings.Replace(defaultSettings, `"REG_NUMBER" : "",`, fmt.Sprintf(`"REG_NUMBER" : "%s",`, config.TailNumber), 1)
// Split the default host into domain and port
hostParts := strings.Split(config.DefaultHost, ":")
if len(hostParts) != 2 {
return "", fmt.Errorf("invalid default host format, expected domain:port")
}
// Replace domain and port
updated = strings.Replace(updated, `"DOMAIN" : "",`, fmt.Sprintf(`"DOMAIN" : "%s",`, hostParts[0]), 1)
updated = strings.Replace(updated, `"PORT" : 0`, fmt.Sprintf(`"PORT" : %s`, hostParts[1]), 1)
return updated, nil
}
// SaveSettingsJSON saves the updated settings JSON to a file
func SaveSettingsJSON(content string, filePath string) error {
return os.WriteFile(filePath, []byte(content), 0644)
}