Files
sunhpc-go/internal/config/defaults.go
2026-02-15 07:18:14 +08:00

61 lines
1.3 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 config
import (
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
// DefaultConfig 返回 SunHPC 的默认配置结构体
func DefaultConfig() *Config {
return &Config{
DB: DBConfig{
Type: "sqlite",
Path: "/var/lib/sunhpc", // SQLite 数据库存放目录
Name: "sunhpc.db", // 数据库文件名
User: "", // SQLite 不需要
Password: "",
Host: "",
Port: 0,
Socket: "",
Verbose: false,
},
Log: LogConfig{
Level: "info",
Format: "text", // or "json"
Output: "stdout",
FilePath: "/var/log/sunhpc/sunhpc.log",
},
Cluster: ClusterConfig{
Name: "default-cluster",
AdminEmail: "admin@example.com",
TimeZone: "Asia/Shanghai",
NodePrefix: "node",
},
}
}
// WriteDefaultConfig 将默认配置写入指定路径
// 如果目录不存在,会自动创建(需有权限)
// 如果文件已存在且非空,会返回错误(除非调用方先删除)
func WriteDefaultConfig(path string) error {
// 确保目录存在
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
// 生成默认配置
cfg := DefaultConfig()
// 序列化为 YAML
data, err := yaml.Marshal(cfg)
if err != nil {
return err
}
// 写入文件0644 权限)
return os.WriteFile(path, data, 0644)
}