Initial commit
This commit is contained in:
61
pkg/config/config.go
Normal file
61
pkg/config/config.go
Normal file
@ -0,0 +1,61 @@
|
||||
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)
|
||||
}
|
73
pkg/config/updater_config.go
Normal file
73
pkg/config/updater_config.go
Normal file
@ -0,0 +1,73 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// UpdaterConfig holds the configuration for the updater application
|
||||
type UpdaterConfig struct {
|
||||
IP string `json:"ip"`
|
||||
Port string `json:"port"`
|
||||
Login string `json:"login"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
// NewUpdaterConfig creates a new updater configuration with default values
|
||||
func NewUpdaterConfig() *UpdaterConfig {
|
||||
return &UpdaterConfig{
|
||||
IP: "127.0.0.1",
|
||||
Port: "22",
|
||||
Login: "root",
|
||||
Password: "",
|
||||
}
|
||||
}
|
||||
|
||||
// LoadUpdaterConfig loads the updater configuration from a file
|
||||
func LoadUpdaterConfig(filePath string) (*UpdaterConfig, error) {
|
||||
// Check if file exists
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
// Create default config if file doesn't exist
|
||||
config := NewUpdaterConfig()
|
||||
if err := SaveUpdaterConfig(config, filePath); err != nil {
|
||||
return nil, fmt.Errorf("не удалось создать конфигурационный файл: %w", err)
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// Read file
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("не удалось прочитать конфигурационный файл: %w", err)
|
||||
}
|
||||
|
||||
// Parse JSON
|
||||
var config UpdaterConfig
|
||||
if err := json.Unmarshal(data, &config); err != nil {
|
||||
return nil, fmt.Errorf("не удалось разобрать конфигурационный файл: %w", err)
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// SaveUpdaterConfig saves the updater configuration to a file
|
||||
func SaveUpdaterConfig(config *UpdaterConfig, filePath string) error {
|
||||
// Marshal JSON
|
||||
data, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("не удалось сериализовать конфигурацию: %w", err)
|
||||
}
|
||||
|
||||
// Write file
|
||||
if err := os.WriteFile(filePath, data, 0644); err != nil {
|
||||
return fmt.Errorf("не удалось записать конфигурационный файл: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// String returns a string representation of the updater configuration
|
||||
func (c *UpdaterConfig) String() string {
|
||||
return fmt.Sprintf("IP: %s\nПорт: %s\nЛогин: %s\nПароль: %s", c.IP, c.Port, c.Login, c.Password)
|
||||
}
|
33
pkg/fileutils/fileutils.go
Normal file
33
pkg/fileutils/fileutils.go
Normal file
@ -0,0 +1,33 @@
|
||||
package fileutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/sftp"
|
||||
)
|
||||
|
||||
// UploadFile uploads a local file to a remote server using SFTP
|
||||
func UploadFile(client *sftp.Client, localPath, remotePath string) error {
|
||||
// Open the local file
|
||||
localFile, err := os.Open(localPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open local file: %w", err)
|
||||
}
|
||||
defer localFile.Close()
|
||||
|
||||
// Create the remote file
|
||||
remoteFile, err := client.Create(remotePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create remote file: %w", err)
|
||||
}
|
||||
defer remoteFile.Close()
|
||||
|
||||
// Copy from the local file to the remote file
|
||||
if _, err = io.Copy(remoteFile, localFile); err != nil {
|
||||
return fmt.Errorf("failed to upload file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user