Skip to content

Commit

Permalink
Save conf file to path
Browse files Browse the repository at this point in the history
  • Loading branch information
cyp0633 committed Mar 10, 2023
1 parent e3c620d commit a93db56
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
21 changes: 21 additions & 0 deletions internal/dhcp/auto/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func Auto() {
// Mechanism part
util.Logger.Info("Analyzing auth mechanism...")
guess(ver)

util.Logger.Info("One last step: input conf output path.")
path := inputConfPath()
util.Conf.SaveConf(path)
}

// selectVersion 选择 Dr.com 客户端版本
Expand Down Expand Up @@ -95,3 +99,20 @@ func inputPassword() string {
}
return result
}

func inputConfPath() (result string) {
for err := error(nil); ; {
prompt := promptui.Prompt{
Label: "Configuration path",
Validate: func(input string) error { return nil },
Default: "drcom-go.conf",
}
result, err = prompt.Run()
if err != nil {
util.Logger.Error("Input configuration path failed", zap.Error(err))
} else {
break
}
}
return
}
30 changes: 29 additions & 1 deletion internal/util/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package util

import (
"encoding/hex"
"strconv"

"go.uber.org/zap"
"gopkg.in/ini.v1"
)

// 对应 drcom-generic 配置
var Conf struct {
type BaseConf struct {
Server string
Username string
Password string
Expand All @@ -27,6 +28,8 @@ var Conf struct {
RorVersion bool
}

var Conf BaseConf

// drcom-go 专有扩展配置
var ExtConf struct {
// 检查连接(204)使用的 URL
Expand Down Expand Up @@ -113,3 +116,28 @@ func parseBytes(s string) []byte {
}
return b
}

// 生成配置文件
func (c *BaseConf) SaveConf(path string) {
cfg := ini.Empty()
section := cfg.Section("")
section.Key("server").SetValue(c.Server)
section.Key("username").SetValue(c.Username)
section.Key("password").SetValue(c.Password)
section.Key("CONTROLCHECKSTATUS").SetValue(string(c.ControlCheckStatus))
section.Key("ADAPTERNUM").SetValue(string(c.AdapterNum))
section.Key("host_ip").SetValue(c.HostIP)
section.Key("IPDOG").SetValue(string(c.IpDog))
section.Key("host_name").SetValue(c.Hostname)
section.Key("PRIMARY_DNS").SetValue(c.PrimaryDns)
section.Key("dhcp_server").SetValue(c.DhcpServer)
section.Key("AUTH_VERSION").SetValue(string(c.AuthVersion[0]) + string(c.AuthVersion[1]))
section.Key("mac").SetValue(c.Mac)
section.Key("host_os").SetValue(c.HostOs)
section.Key("KEEP_ALIVE_VERSION").SetValue(string(c.KeepAliveVersion[0]) + string(c.KeepAliveVersion[1]))
section.Key("ror_version").SetValue(strconv.FormatBool(c.RorVersion))
err := cfg.SaveTo(path)
if err != nil {
Logger.Panic("Saving configuration failed", zap.Error(err), zap.String("path", path))
}
}

0 comments on commit a93db56

Please sign in to comment.