Files
DevPack/internal/config/paths.go
2026-03-04 17:45:33 +08:00

68 lines
1.5 KiB
Go

package config
import (
"os"
"path/filepath"
"github.com/spf13/viper"
)
const (
// AppName 应用名称
AppName = "devpack"
// DefaultConfigFileName 默认配置文件名
DefaultConfigFileName = "config.yaml"
// DefaultProfileName 默认 Profile 名
DefaultProfileName = "default"
)
// Paths DevPack 路径管理
type Paths struct {
Home string // ~/.devpack
Config string // ~/.devpack/config.yaml
Profiles string // ~/.devpack/profiles
Packs string // ~/.devpack/packs
Logs string // ~/.devpack/logs
Temp string // ~/.devpack/tmp
}
// DefaultPaths 返回默认路径配置
func DefaultPaths() (*Paths, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
devpackHome := filepath.Join(home, ".devpack")
return &Paths{
Home: devpackHome,
Config: filepath.Join(devpackHome, DefaultConfigFileName),
Profiles: filepath.Join(devpackHome, "profiles"),
Packs: filepath.Join(devpackHome, "packs"),
Logs: filepath.Join(devpackHome, "logs"),
Temp: filepath.Join(devpackHome, "tmp"),
}, nil
}
// EnsureDirs 确保所有目录存在
func (p *Paths) EnsureDirs() error {
dirs := []string{p.Home, p.Profiles, p.Packs, p.Logs, p.Temp}
for _, dir := range dirs {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
}
return nil
}
func (p *Paths) ConfigFilePath() string {
path := viper.ConfigFileUsed() // 触发 viper 加载配置文件
if path != "" {
return path
}
return p.Config
}