Files
sunhpc-go/internal/system/motd.go
2026-02-14 05:36:00 +08:00

48 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}