218 lines
5.5 KiB
Go
218 lines
5.5 KiB
Go
package wizard
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"sunhpc/pkg/utils"
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
)
|
|
|
|
// saveConfig 保存配置到文件
|
|
func (m *model) saveConfig() error {
|
|
configPath := GetConfigPath()
|
|
|
|
// 确保目录存在
|
|
dir := filepath.Dir(configPath)
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
return fmt.Errorf("创建配置目录失败:%w", err)
|
|
}
|
|
|
|
// 序列化配置
|
|
data, err := json.MarshalIndent(m.config, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("序列化配置失败:%w", err)
|
|
}
|
|
|
|
// 写入文件
|
|
if err := os.WriteFile(configPath, data, 0644); err != nil {
|
|
return fmt.Errorf("保存配置文件失败:%w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// loadConfig 从文件加载配置
|
|
func loadConfig() (*Config, error) {
|
|
configPath := GetConfigPath()
|
|
|
|
data, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("读取配置文件失败:%w", err)
|
|
}
|
|
|
|
var cfg Config
|
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
|
return nil, fmt.Errorf("解析配置文件失败:%w", err)
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|
|
|
|
// 以下是 model.go 中调用的保存方法
|
|
func (m *model) saveCurrentPage() {
|
|
switch m.currentPage {
|
|
case PageData:
|
|
m.saveDataPage()
|
|
case PagePublicNetwork:
|
|
m.savePublicNetworkPage()
|
|
case PageInternalNetwork:
|
|
m.saveInternalNetworkPage()
|
|
case PageDNS:
|
|
m.saveDNSPage()
|
|
}
|
|
}
|
|
|
|
func (m *model) saveDataPage() {
|
|
if len(m.textInputs) >= 8 {
|
|
m.config.HomePage = m.textInputs[0].Value()
|
|
m.config.Hostname = m.textInputs[1].Value()
|
|
m.config.Country = m.textInputs[2].Value()
|
|
m.config.Region = m.textInputs[3].Value()
|
|
m.config.Timezone = m.textInputs[4].Value()
|
|
m.config.DBAddress = m.textInputs[5].Value()
|
|
m.config.DataAddress = m.textInputs[6].Value()
|
|
}
|
|
}
|
|
|
|
func (m *model) savePublicNetworkPage() {
|
|
if len(m.textInputs) >= 4 {
|
|
m.config.PublicInterface = m.textInputs[0].Value()
|
|
m.config.PublicIPAddress = m.textInputs[1].Value()
|
|
m.config.PublicNetmask = m.textInputs[2].Value()
|
|
m.config.PublicGateway = m.textInputs[3].Value()
|
|
}
|
|
}
|
|
|
|
func (m *model) saveInternalNetworkPage() {
|
|
if len(m.textInputs) >= 3 {
|
|
m.config.InternalInterface = m.textInputs[0].Value()
|
|
m.config.InternalIPAddress = m.textInputs[1].Value()
|
|
m.config.InternalNetmask = m.textInputs[2].Value()
|
|
}
|
|
}
|
|
|
|
func (m *model) saveDNSPage() {
|
|
if len(m.textInputs) >= 2 {
|
|
m.config.DNSPrimary = m.textInputs[0].Value()
|
|
m.config.DNSSecondary = m.textInputs[1].Value()
|
|
}
|
|
}
|
|
|
|
// initPageInputs 初始化当前页面的输入框
|
|
func (m *model) initPageInputs() {
|
|
m.textInputs = make([]textinput.Model, 0)
|
|
|
|
switch m.currentPage {
|
|
case PageData:
|
|
fields := []struct{ label, value string }{
|
|
{"Homepage", m.config.HomePage},
|
|
{"Hostname", m.config.Hostname},
|
|
{"Country", m.config.Country},
|
|
{"Region", m.config.Region},
|
|
{"Timezone", m.config.Timezone},
|
|
{"DB Path", m.config.DBAddress},
|
|
{"Software", m.config.DataAddress},
|
|
}
|
|
for _, f := range fields {
|
|
ti := textinput.New()
|
|
ti.Placeholder = ""
|
|
ti.Placeholder = f.label
|
|
ti.SetValue(f.value)
|
|
ti.Width = 50
|
|
m.textInputs = append(m.textInputs, ti)
|
|
}
|
|
m.focusIndex = 0
|
|
if len(m.textInputs) > 0 {
|
|
m.textInputs[0].Focus()
|
|
}
|
|
m.inputLabels = []string{"Homepage", "Hostname", "Country", "Region", "Timezone", "DBPath", "Software"}
|
|
|
|
case PagePublicNetwork:
|
|
fields := []struct{ label, value string }{
|
|
{"Public Interface", m.config.PublicInterface},
|
|
{"Public IP Address", m.config.PublicIPAddress},
|
|
{"Public Netmask", m.config.PublicNetmask},
|
|
{"Public Gateway", m.config.PublicGateway},
|
|
}
|
|
for _, f := range fields {
|
|
ti := textinput.New()
|
|
ti.Placeholder = ""
|
|
ti.Placeholder = f.label
|
|
ti.SetValue(f.value)
|
|
ti.Width = 50
|
|
m.textInputs = append(m.textInputs, ti)
|
|
}
|
|
m.focusIndex = 0
|
|
if len(m.textInputs) > 0 {
|
|
m.textInputs[0].Focus()
|
|
}
|
|
m.inputLabels = []string{"Public Interface", "Public IP Address", "Public Netmask", "Public Gateway"}
|
|
|
|
case PageInternalNetwork:
|
|
fields := []struct{ label, value string }{
|
|
{"Internal Interface", m.config.InternalInterface},
|
|
{"Internal IP Address", m.config.InternalIPAddress},
|
|
{"Internal Netmask", m.config.InternalNetmask},
|
|
}
|
|
for _, f := range fields {
|
|
ti := textinput.New()
|
|
ti.Placeholder = f.label
|
|
ti.SetValue(f.value)
|
|
ti.Width = 50
|
|
m.textInputs = append(m.textInputs, ti)
|
|
}
|
|
m.focusIndex = 0
|
|
if len(m.textInputs) > 0 {
|
|
m.textInputs[0].Focus()
|
|
}
|
|
m.inputLabels = []string{"Internal Interface", "Internal IP", "Internal Mask"}
|
|
|
|
case PageDNS:
|
|
fields := []struct{ label, value string }{
|
|
{"Primary DNS", m.config.DNSPrimary},
|
|
{"Secondary DNS", m.config.DNSSecondary},
|
|
}
|
|
for _, f := range fields {
|
|
ti := textinput.New()
|
|
ti.Placeholder = f.label
|
|
ti.SetValue(f.value)
|
|
ti.Width = 50
|
|
m.textInputs = append(m.textInputs, ti)
|
|
}
|
|
m.focusIndex = 0
|
|
if len(m.textInputs) > 0 {
|
|
m.textInputs[0].Focus()
|
|
}
|
|
m.inputLabels = []string{"Pri DNS", "Sec DNS"}
|
|
}
|
|
}
|
|
|
|
// 获取系统网络接口
|
|
func getNetworkInterfaces() []string {
|
|
// 实现获取系统网络接口的逻辑
|
|
// 例如:使用 net.Interface() 函数获取系统网络接口
|
|
// 返回一个字符串切片,包含系统网络接口的名称
|
|
interfaces, err := net.Interfaces()
|
|
if err != nil {
|
|
return []string{utils.NoAvailableNetworkInterfaces}
|
|
}
|
|
|
|
var result []string
|
|
for _, iface := range interfaces {
|
|
// 跳过 loopback 接口
|
|
if iface.Flags&net.FlagLoopback != 0 {
|
|
continue
|
|
}
|
|
result = append(result, iface.Name)
|
|
}
|
|
|
|
if len(result) == 0 {
|
|
return []string{utils.NoAvailableNetworkInterfaces}
|
|
}
|
|
return result
|
|
}
|