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

@@ -2,25 +2,25 @@ package service
import "mfd-monitor-agent/internal/config"
// InstallOptions carries platform-specific service installation inputs.
// InstallOptions 携带平台相关的服务安装参数。
type InstallOptions struct {
// ConfigPath is preserved in the service command line when a config file was supplied.
// ConfigPath 在提供了配置文件时,会写入服务命令行中的 -config 参数。
ConfigPath string
}
// Manager controls operating system service installation and lifecycle commands.
// Manager 控制操作系统服务的安装与生命周期命令。
type Manager interface {
// Install registers the agent executable as an operating system service.
// Install 将 agent 可执行文件注册为操作系统服务。
Install(executable string, cfg config.Config, opts InstallOptions) error
// Uninstall removes the operating system service registration.
// Uninstall 移除操作系统服务注册。
Uninstall(cfg config.Config) error
// Start asks the operating system service manager to start the agent service.
// Start 请求操作系统服务管理器启动 agent 服务。
Start(cfg config.Config) error
// Stop asks the operating system service manager to stop the agent service.
// Stop 请求操作系统服务管理器停止 agent 服务。
Stop(cfg config.Config) error
}
// NewManager returns the platform-specific service manager.
// NewManager 返回平台相关的服务管理器。
func NewManager() Manager {
return newManager()
}

View File

@@ -13,15 +13,15 @@ import (
var currentUID = os.Geteuid
// linuxManager manages a systemd service for the agent.
// linuxManager 通过 systemd 管理 agent 服务。
type linuxManager struct{}
// newManager returns the Linux systemd-backed service manager.
// newManager 返回基于 Linux systemd 的服务管理器。
func newManager() Manager {
return linuxManager{}
}
// Install writes a systemd unit, reloads systemd, and enables the configured service.
// Install 写入 systemd unit、重载 systemd 并 enable 配置的服务。
func (linuxManager) Install(executable string, cfg config.Config, opts InstallOptions) error {
if currentUID() != 0 {
return fmt.Errorf("install requires root")
@@ -40,7 +40,7 @@ func (linuxManager) Install(executable string, cfg config.Config, opts InstallOp
return nil
}
// renderSystemdUnit builds the Linux systemd unit used by Install.
// renderSystemdUnit 生成 Install 使用的 Linux systemd unit 内容。
func renderSystemdUnit(executable string, configPath string, cfg config.Config) string {
args := []string{executable, "run"}
if configPath != "" {
@@ -62,7 +62,7 @@ WantedBy=multi-user.target
`, cfg.Service.Description, strings.Join(args, " "))
}
// Uninstall disables the service, removes its unit file, and reloads systemd.
// Uninstall 禁用服务、删除 unit 文件并重载 systemd。
func (linuxManager) Uninstall(cfg config.Config) error {
_ = exec.Command("systemctl", "disable", "--now", cfg.Service.Name).Run()
if err := os.Remove("/etc/systemd/system/" + cfg.Service.Name + ".service"); err != nil && !os.IsNotExist(err) {
@@ -71,12 +71,12 @@ func (linuxManager) Uninstall(cfg config.Config) error {
return exec.Command("systemctl", "daemon-reload").Run()
}
// Start asks systemd to start the configured service.
// Start 请求 systemd 启动配置的服务。
func (linuxManager) Start(cfg config.Config) error {
return exec.Command("systemctl", "start", cfg.Service.Name).Run()
}
// Stop asks systemd to stop the configured service.
// Stop 请求 systemd 停止配置的服务。
func (linuxManager) Stop(cfg config.Config) error {
return exec.Command("systemctl", "stop", cfg.Service.Name).Run()
}

View File

@@ -9,7 +9,7 @@ import (
"mfd-monitor-agent/internal/config"
)
// TestRenderSystemdUnitIncludesConfigPath verifies install preserves the config flag.
// TestRenderSystemdUnitIncludesConfigPath 验证安装时保留 -config 参数。
func TestRenderSystemdUnitIncludesConfigPath(t *testing.T) {
cfg := config.Default()
cfg.Service.Description = "MFD Agent"
@@ -21,7 +21,7 @@ func TestRenderSystemdUnitIncludesConfigPath(t *testing.T) {
}
}
// TestLinuxInstallRequiresRoot verifies Linux install rejects non-root callers.
// TestLinuxInstallRequiresRoot 验证 Linux 安装拒绝非 root 调用。
func TestLinuxInstallRequiresRoot(t *testing.T) {
oldUID := currentUID
t.Cleanup(func() { currentUID = oldUID })

View File

@@ -9,30 +9,30 @@ import (
"mfd-monitor-agent/internal/config"
)
// unsupportedManager reports service lifecycle operations as unsupported on this platform.
// unsupportedManager 在本平台将服务生命周期操作报告为不支持。
type unsupportedManager struct{}
// newManager returns a manager that fails all service operations on unsupported platforms.
// newManager 返回在不支持平台上对所有服务操作均失败的管理器。
func newManager() Manager {
return unsupportedManager{}
}
// Install reports that service installation is unsupported on this platform.
// Install 报告本平台不支持服务安装。
func (unsupportedManager) Install(string, config.Config, InstallOptions) error {
return fmt.Errorf("service install is not implemented for %s", runtime.GOOS)
}
// Uninstall reports that service removal is unsupported on this platform.
// Uninstall 报告本平台不支持服务卸载。
func (unsupportedManager) Uninstall(config.Config) error {
return fmt.Errorf("service uninstall is not implemented for %s", runtime.GOOS)
}
// Start reports that service start is unsupported on this platform.
// Start 报告本平台不支持服务启动。
func (unsupportedManager) Start(config.Config) error {
return fmt.Errorf("service start is not implemented for %s", runtime.GOOS)
}
// Stop reports that service stop is unsupported on this platform.
// Stop 报告本平台不支持服务停止。
func (unsupportedManager) Stop(config.Config) error {
return fmt.Errorf("service stop is not implemented for %s", runtime.GOOS)
}

View File

@@ -9,15 +9,15 @@ import (
"mfd-monitor-agent/internal/config"
)
// windowsManager manages the agent through the Windows Service Control Manager.
// windowsManager 通过 Windows 服务控制管理器(SCM)管理 agent。
type windowsManager struct{}
// newManager returns the Windows service manager.
// newManager 返回 Windows 服务管理器。
func newManager() Manager {
return windowsManager{}
}
// Install registers the agent executable as an auto-start Windows Service.
// Install 将 agent 可执行文件注册为自动启动的 Windows 服务。
func (windowsManager) Install(executable string, cfg config.Config, opts InstallOptions) error {
args := []string{`"` + executable + `"`, "run"}
if opts.ConfigPath != "" {
@@ -26,18 +26,18 @@ func (windowsManager) Install(executable string, cfg config.Config, opts Install
return exec.Command("sc.exe", "create", cfg.Service.Name, "binPath=", strings.Join(args, " "), "DisplayName=", cfg.Service.DisplayName, "start=", "auto").Run()
}
// Uninstall stops and removes the configured Windows Service.
// Uninstall 停止并删除已配置的 Windows 服务。
func (windowsManager) Uninstall(cfg config.Config) error {
_ = exec.Command("sc.exe", "stop", cfg.Service.Name).Run()
return exec.Command("sc.exe", "delete", cfg.Service.Name).Run()
}
// Start asks the Windows Service Control Manager to start the service.
// Start 请求 SCM 启动服务。
func (windowsManager) Start(cfg config.Config) error {
return exec.Command("sc.exe", "start", cfg.Service.Name).Run()
}
// Stop asks the Windows Service Control Manager to stop the service.
// Stop 请求 SCM 停止服务。
func (windowsManager) Stop(cfg config.Config) error {
return exec.Command("sc.exe", "stop", cfg.Service.Name).Run()
}