refactor: extract internal/config and eliminate global config var

Move Config struct to internal/config with exported fields. Replace
global var config with package-level cfg pointer. Add OpenLogFile to
config package. Add type alias Config = appconfig.Config in main
package for backward compatibility.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
JiXieShi
2026-05-23 21:45:08 +08:00
parent 2ce672cdde
commit 31dd9da490
15 changed files with 198 additions and 189 deletions
+42
View File
@@ -0,0 +1,42 @@
// Package config holds the application configuration.
package config
import (
"fmt"
"os"
"time"
)
// Config holds all application settings.
type Config struct {
PortName string
BaudRate int
DataBits int
StopBits int
ParityBit int
OutputCode string
InputCode string
EndStr string
EnableLog bool
LogFilePath string
ForWard []int
FrameSize int
TimesTamp bool
TimesFmt string
Address []string
EnableGUI bool
HotkeyMod string
}
// OpenLogFile opens the configured log file for writing, or returns nil if logging is disabled.
func OpenLogFile(cfg *Config) (*os.File, error) {
if cfg.EnableLog {
path := fmt.Sprintf(cfg.LogFilePath, cfg.PortName, time.Now().Format("2006_01_02T150405"))
f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return nil, err
}
return f, nil
}
return nil, nil
}