docs(agent): 统一中文注释并补充部署文档

将代码与测试注释改为中文以符合仓库规范;新增中文部署指南、
示例配置,并完善构建脚本对 VERSION 注入的说明。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
codex
2026-05-19 02:30:49 +00:00
parent 89d5e27874
commit 877e4cf41d
28 changed files with 608 additions and 226 deletions

View File

@@ -8,7 +8,7 @@ import (
"time"
)
// Config contains all runtime, reporting, collection, and service settings.
// Config 包含运行时、上报、采集与服务注册相关的全部配置项。
type Config struct {
HostID string `json:"host_id"`
HTTP HTTPConfig `json:"http"`
@@ -17,18 +17,18 @@ type Config struct {
Service ServiceConfig `json:"service"`
}
// HTTPConfig controls the local HTTP API listener.
// HTTPConfig 控制本地 HTTP API 的监听地址。
type HTTPConfig struct {
ListenAddress string `json:"listen_address"`
}
// CollectConfig controls periodic host status collection.
// CollectConfig 控制周期性主机状态采集。
type CollectConfig struct {
Interval Duration `json:"interval"`
Timeout Duration `json:"timeout"`
}
// ReportConfig controls optional periodic status POST reporting.
// ReportConfig 控制可选的周期性状态 POST 上报。
type ReportConfig struct {
Enabled bool `json:"enabled"`
Endpoint string `json:"endpoint"`
@@ -36,27 +36,27 @@ type ReportConfig struct {
Timeout Duration `json:"timeout"`
}
// ServiceConfig controls operating system service registration metadata.
// ServiceConfig 控制操作系统服务注册时使用的元数据。
type ServiceConfig struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
}
// Duration wraps time.Duration so JSON config can use Go duration strings.
// Duration 封装 time.Duration,使 JSON 配置可使用 Go 时长字符串(如 "30s")。
type Duration time.Duration
// Duration returns the standard library duration value.
// Duration 返回标准库的 time.Duration 值。
func (d Duration) Duration() time.Duration {
return time.Duration(d)
}
// MarshalJSON writes the duration as a Go duration string such as "30s".
// MarshalJSON 将时长序列化为 Go 时长字符串,例如 "30s"。
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}
// UnmarshalJSON parses a Go duration string from JSON.
// UnmarshalJSON 从 JSON 解析 Go 时长字符串。
func (d *Duration) UnmarshalJSON(data []byte) error {
var raw string
if err := json.Unmarshal(data, &raw); err != nil {
@@ -70,7 +70,7 @@ func (d *Duration) UnmarshalJSON(data []byte) error {
return nil
}
// Default returns safe local defaults for running the agent.
// Default 返回适合本地运行的安全默认配置。
func Default() Config {
return Config{
HTTP: HTTPConfig{ListenAddress: "127.0.0.1:8080"},
@@ -91,7 +91,7 @@ func Default() Config {
}
}
// Load reads optional JSON config, applies environment overrides, and validates required settings.
// Load 读取可选的 JSON 配置文件,应用环境变量覆盖,并校验必填项。
func Load(path string) (Config, error) {
cfg := Default()
if path != "" {
@@ -110,7 +110,7 @@ func Load(path string) (Config, error) {
return cfg, nil
}
// applyEnv overlays supported MFD_AGENT_* environment variables onto cfg.
// applyEnv 将支持的 MFD_AGENT_* 环境变量覆盖到 cfg 上。
func applyEnv(cfg *Config) {
if value := os.Getenv("MFD_AGENT_HOST_ID"); value != "" {
cfg.HostID = value

View File

@@ -6,7 +6,7 @@ import (
"time"
)
// TestDefaultConfigUsesLocalhostAndSaneIntervals verifies safe local defaults.
// TestDefaultConfigUsesLocalhostAndSaneIntervals 验证默认配置使用本地监听地址与合理的采集间隔。
func TestDefaultConfigUsesLocalhostAndSaneIntervals(t *testing.T) {
cfg := Default()
@@ -21,7 +21,7 @@ func TestDefaultConfigUsesLocalhostAndSaneIntervals(t *testing.T) {
}
}
// TestLoadAppliesEnvironmentOverrides verifies environment variables override defaults.
// TestLoadAppliesEnvironmentOverrides 验证环境变量可覆盖默认配置。
func TestLoadAppliesEnvironmentOverrides(t *testing.T) {
t.Setenv("MFD_AGENT_LISTEN", "127.0.0.1:9090")
t.Setenv("MFD_AGENT_REPORT_ENABLED", "true")
@@ -48,7 +48,7 @@ func TestLoadAppliesEnvironmentOverrides(t *testing.T) {
}
}
// TestLoadJSONConfigFile verifies JSON config files are parsed into Config.
// TestLoadJSONConfigFile 验证可从 JSON 配置文件解析 Config。
func TestLoadJSONConfigFile(t *testing.T) {
file, err := os.CreateTemp(t.TempDir(), "config-*.json")
if err != nil {