各包职责: - config: 分类 YAML 配置 - model: Device/IP/MAC 公共类型 - discovery: 扫描 + ARP MAC 解析 + Devices 变量 - sshclient: SSH 连接 - stress: stress-ng/stressapptest 压测 + Results 变量 - report: 80mm 热敏模板/sparkline/打印 + Printed 变量 - workflow: 永久循环调度 + Start/Stop workflow 已精简为 100 行纯编排,所有业务逻辑下沉各包
130 lines
3.0 KiB
Go
130 lines
3.0 KiB
Go
package config
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
"time"
|
||
|
||
"gopkg.in/yaml.v3"
|
||
)
|
||
|
||
// ============================
|
||
// Config — 分类配置
|
||
// ============================
|
||
|
||
// Config 全局运行配置(仅从配置文件加载)
|
||
type Config struct {
|
||
Scan ScanConfig `yaml:"scan"`
|
||
SSH SSHConfig `yaml:"ssh"`
|
||
Stress StressConfig `yaml:"stress"`
|
||
Report ReportConfig `yaml:"report"`
|
||
Workflow WorkflowConfig `yaml:"workflow"`
|
||
}
|
||
|
||
// ============================
|
||
// 分类配置项
|
||
// ============================
|
||
|
||
// ScanConfig 网段扫描
|
||
type ScanConfig struct {
|
||
CIDR string `yaml:"cidr"`
|
||
Timeout time.Duration `yaml:"timeout"`
|
||
Concurrency int `yaml:"concurrency"`
|
||
}
|
||
|
||
// SSHConfig SSH 认证
|
||
type SSHConfig struct {
|
||
User string `yaml:"user"`
|
||
Password string `yaml:"password"`
|
||
KeyFile string `yaml:"key"`
|
||
Port int `yaml:"port"`
|
||
}
|
||
|
||
// StressConfig 压力测试
|
||
type StressConfig struct {
|
||
Types string `yaml:"types"`
|
||
Duration time.Duration `yaml:"duration"`
|
||
Threads int `yaml:"threads"`
|
||
}
|
||
|
||
// ReportConfig 报告
|
||
type ReportConfig struct {
|
||
Path string `yaml:"path"` // 报告保存路径
|
||
Print PrintConfig `yaml:"print"` // 打印配置
|
||
}
|
||
|
||
// PrintConfig 打印配置
|
||
type PrintConfig struct {
|
||
Enabled bool `yaml:"enabled"` // 是否打印
|
||
Backend string `yaml:"backend"` // system / escpos / usb / file
|
||
Device string `yaml:"device"` // 目标:打印机名 / host:port / 设备路径 / 文件路径
|
||
Width int `yaml:"width"` // 行宽,80mm=42
|
||
Charset string `yaml:"charset"` // 字符集(默认 utf-8)
|
||
}
|
||
|
||
// WorkflowConfig 工作流
|
||
type WorkflowConfig struct {
|
||
Interval time.Duration `yaml:"interval"` // 轮询间隔
|
||
}
|
||
|
||
// ============================
|
||
// 加载
|
||
// ============================
|
||
|
||
// Default 内置默认配置
|
||
func Default() *Config {
|
||
return &Config{
|
||
Scan: ScanConfig{
|
||
CIDR: "192.168.1.0/24",
|
||
Timeout: 3 * time.Second,
|
||
Concurrency: 50,
|
||
},
|
||
SSH: SSHConfig{
|
||
User: "root",
|
||
Port: 22,
|
||
},
|
||
Stress: StressConfig{
|
||
Types: "cpu,memory",
|
||
Duration: 30 * time.Second,
|
||
Threads: 4,
|
||
},
|
||
Workflow: WorkflowConfig{
|
||
Interval: 10 * time.Second,
|
||
},
|
||
}
|
||
}
|
||
|
||
// Load 从文件加载配置(覆盖默认值)
|
||
// path 为空时尝试加载 ./auto-check.yaml
|
||
// 文件不存在时返回默认配置
|
||
func Load(path string) (*Config, error) {
|
||
cfg := Default()
|
||
|
||
if path == "" {
|
||
path = "auto-check.yaml"
|
||
}
|
||
|
||
data, err := os.ReadFile(path)
|
||
if err != nil {
|
||
if os.IsNotExist(err) {
|
||
return cfg, nil // 无配置文件,使用默认值
|
||
}
|
||
return nil, fmt.Errorf("读取配置文件失败: %w", err)
|
||
}
|
||
|
||
if err := yaml.Unmarshal(data, cfg); err != nil {
|
||
return nil, fmt.Errorf("解析配置文件 %s 失败: %w", path, err)
|
||
}
|
||
|
||
return cfg, nil
|
||
}
|
||
|
||
// LoadFromString 从字符串加载(用于测试)
|
||
func LoadFromString(s string) (*Config, error) {
|
||
cfg := Default()
|
||
if err := yaml.Unmarshal([]byte(s), cfg); err != nil {
|
||
return nil, fmt.Errorf("解析配置失败: %w", err)
|
||
}
|
||
return cfg, nil
|
||
}
|