add tui command
This commit is contained in:
192
pkg/wizard/config.go
Normal file
192
pkg/wizard/config.go
Normal file
@@ -0,0 +1,192 @@
|
||||
package wizard
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"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.Hostname = m.textInputs[0].Value()
|
||||
m.config.Country = m.textInputs[1].Value()
|
||||
m.config.Region = m.textInputs[2].Value()
|
||||
m.config.Timezone = m.textInputs[3].Value()
|
||||
m.config.HomePage = m.textInputs[4].Value()
|
||||
m.config.DBAddress = m.textInputs[5].Value()
|
||||
m.config.DBName = m.textInputs[6].Value()
|
||||
m.config.DataAddress = m.textInputs[7].Value()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *model) savePublicNetworkPage() {
|
||||
if len(m.textInputs) >= 4 {
|
||||
m.config.PublicInterface = m.textInputs[0].Value()
|
||||
m.config.IPAddress = m.textInputs[1].Value()
|
||||
m.config.Netmask = m.textInputs[2].Value()
|
||||
m.config.Gateway = m.textInputs[3].Value()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *model) saveInternalNetworkPage() {
|
||||
if len(m.textInputs) >= 3 {
|
||||
m.config.InternalInterface = m.textInputs[0].Value()
|
||||
m.config.InternalIP = m.textInputs[1].Value()
|
||||
m.config.InternalMask = 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 }{
|
||||
{"主机名:", m.config.Hostname},
|
||||
{"国家:", m.config.Country},
|
||||
{"地区:", m.config.Region},
|
||||
{"时区:", m.config.Timezone},
|
||||
{"主页:", m.config.HomePage},
|
||||
{"数据库地址:", m.config.DBAddress},
|
||||
{"数据库名称:", m.config.DBName},
|
||||
{"Data 地址:", m.config.DataAddress},
|
||||
}
|
||||
for _, f := range fields {
|
||||
ti := textinput.New()
|
||||
ti.Placeholder = ""
|
||||
ti.Placeholder = f.label
|
||||
//ti.Placeholder = "请输入" + f.label[:len(f.label)-1]
|
||||
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{"Hostname", "Country", "Region", "Timezone", "Homepage", "DBPath", "DBName", "Software"}
|
||||
|
||||
case PagePublicNetwork:
|
||||
fields := []struct{ label, value string }{
|
||||
{"公网接口:", m.config.PublicInterface},
|
||||
{"IP 地址:", m.config.IPAddress},
|
||||
{"子网掩码:", m.config.Netmask},
|
||||
{"网关:", m.config.Gateway},
|
||||
}
|
||||
for _, f := range fields {
|
||||
ti := textinput.New()
|
||||
ti.Placeholder = ""
|
||||
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{"Wan iface", "IPAddress", "Netmask", "Gateway"}
|
||||
|
||||
case PageInternalNetwork:
|
||||
fields := []struct{ label, value string }{
|
||||
{"内网接口:", m.config.InternalInterface},
|
||||
{"内网 IP:", m.config.InternalIP},
|
||||
{"内网掩码:", m.config.InternalMask},
|
||||
}
|
||||
for _, f := range fields {
|
||||
ti := textinput.New()
|
||||
ti.Placeholder = ""
|
||||
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{"Lan iface", "IPAddress", "Netmask"}
|
||||
|
||||
case PageDNS:
|
||||
fields := []struct{ label, value string }{
|
||||
{"主 DNS:", m.config.DNSPrimary},
|
||||
{"备 DNS:", m.config.DNSSecondary},
|
||||
}
|
||||
for _, f := range fields {
|
||||
ti := textinput.New()
|
||||
ti.Placeholder = ""
|
||||
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{"DNSPrimary", "DNSSecondary"}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user