Skip to content

Commit

Permalink
refactor: 代码调整
Browse files Browse the repository at this point in the history
  • Loading branch information
junlongzzz committed Nov 7, 2024
1 parent bcfe7ca commit 5fa6f70
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
31 changes: 16 additions & 15 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ type CoreConfig struct {
OfficialUiAddr string // 官方ui地址
YACDUiAddr string // Yet Another Clash Dashboard ui地址
HttpProxyPort int // http代理端口
ConfigPath string // 配置文件路径
RunConfigPath string // 实际运行配置文件路径
}

var (
coreDir string
coreName string
corePath string
coreDir string // core工作目录
coreName string // core程序名称
corePath string // core程序路径
coreConfigPath string // core配置文件路径
coreRunConfigPath string // core实际运行配置文件路径

coreConfig = &CoreConfig{
// 配置文件中不存在时需要赋予默认值的选项
Expand All @@ -47,13 +47,13 @@ var (

// 加载配置文件
func loadCoreConfig() error {
if coreConfig.ConfigPath == "" {
coreConfig.ConfigPath = filepath.Join(coreDir, "config.yaml")
if coreConfigPath == "" {
coreConfigPath = filepath.Join(coreDir, "config.yaml")
}
if !isFileExist(coreConfig.ConfigPath) {
return fmt.Errorf("config file not found: %s", coreConfig.ConfigPath)
if !isFileExist(coreConfigPath) {
return fmt.Errorf("config file not found: %s", coreConfigPath)
}
configBytes, err := os.ReadFile(coreConfig.ConfigPath)
configBytes, err := os.ReadFile(coreConfigPath)
if err != nil {
return fmt.Errorf("failed to read config file: %v", err)
}
Expand Down Expand Up @@ -126,14 +126,15 @@ func loadCoreConfig() error {
if out, err = yaml.Marshal(&configMap); err != nil {
return fmt.Errorf("failed to marshal config file: %v", err)
}
if coreConfig.RunConfigPath == "" {
coreConfig.RunConfigPath = filepath.Join(coreDir, "config.auto-gen")
if coreRunConfigPath == "" {
coreRunConfigPath = filepath.Join(coreDir, "config.auto-gen")
}
// 保存到运行配置文件
if err = os.WriteFile(coreConfig.RunConfigPath, out, 0644); err != nil {
return fmt.Errorf("failed to write config file: %v", err)
if err = os.WriteFile(coreRunConfigPath, out, 0644); err != nil {
return fmt.Errorf("failed to write run config file: %v", err)
}

log.Println("Core config loaded:", coreConfigPath)
return nil
}

Expand All @@ -148,7 +149,7 @@ func startCore() bool {
}

// 启动core程序
cmd := execCommand(corePath, "-d", coreDir, "-f", coreConfig.RunConfigPath)
cmd := execCommand(corePath, "-d", coreDir, "-f", coreRunConfigPath)
// 重定向输出到log
cmd.Stdout = log.Writer()
cmd.Stderr = log.Writer()
Expand Down
25 changes: 14 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
"strings"
Expand Down Expand Up @@ -149,7 +150,7 @@ func onReady() {

systray.AddMenuItem("Edit Config", "Edit Config").Click(func() {
// 打开配置文件
_ = openBrowser(coreConfig.ConfigPath)
_ = openBrowser(coreConfigPath)
})

dashboardItem := systray.AddMenuItem("Core Dashboard", "Core Dashboard")
Expand Down Expand Up @@ -207,16 +208,18 @@ func onReady() {
systray.AddSeparator()

systray.AddMenuItem("About", "About").Click(func() {
about := fmt.Sprintf("Name: %s\n"+
"Description: %s\n"+
"Build Hash: %s\n"+
"---\n"+
"Work Directory: %s\n"+
"Log Directory: %s\n"+
"Core Directory: %s\n"+
"Core Path: %s\n"+
"Core Version: %s",
AppName, "Wrapper for Mihomo written in Golang.", build, workDir, logDir, coreDir, corePath, getCoreVersion())
about := fmt.Sprintf(`Name: %s
Description: %s
Build Hash: %s
Go Version: %s
---
Work Directory: %s
Log Directory: %s
Core Directory: %s
Core Path: %s
Core Version: %s
Config Path: %s`,
AppName, "Wrapper for Mihomo written in Golang.", build, runtime.Version(), workDir, logDir, coreDir, corePath, getCoreVersion(), coreConfigPath)
messageBoxAlert(AppName, about)
})

Expand Down

0 comments on commit 5fa6f70

Please sign in to comment.