This commit is contained in:
2026-02-14 05:36:00 +08:00
commit d7cd899983
37 changed files with 4169 additions and 0 deletions

47
internal/system/motd.go Normal file
View File

@@ -0,0 +1,47 @@
package system
import (
"os"
"time"
)
// SetMOTD 设置 /etc/motd 文件内容
// 参数: content - MOTD 文本内容
// 返回: error - 写入文件错误
func SetMOTD(content string) error {
if content == "" {
// 如果内容为空,不清除现有 MOTD避免误操作
return nil
}
// 添加时间和系统信息
finalContent := "========================================\n"
finalContent += "SunHPC 集群管理系统\n"
finalContent += "时间: " + time.Now().Format("2006-01-02 15:04:05") + "\n"
finalContent += "========================================\n\n"
finalContent += content
// 确保行尾有换行
if content[len(content)-1] != '\n' {
finalContent += "\n"
}
return os.WriteFile("/etc/motd", []byte(finalContent), 0644)
}
// ClearMOTD 清空 MOTD
func ClearMOTD() error {
return os.WriteFile("/etc/motd", []byte{}, 0644)
}
// AppendToMOTD 追加内容到 MOTD
func AppendToMOTD(additional string) error {
f, err := os.OpenFile("/etc/motd", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(additional + "\n")
return err
}