ok-1
This commit is contained in:
47
internal/system/motd.go
Normal file
47
internal/system/motd.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user