1
0
Files
Skill/manager/internal/config/paths.go

78 lines
1.6 KiB
Go

package config
import (
"os"
"path/filepath"
)
const (
ConfigDir = ".skillmgr"
RepositoryFile = "repository.json"
InstallFile = "install.json"
CacheDir = "cache"
)
// GetConfigRoot 获取配置根目录
// 支持通过环境变量 SKILLMGR_TEST_ROOT 覆盖(用于测试隔离)
func GetConfigRoot() (string, error) {
// 测试模式:使用环境变量指定的临时目录
if testRoot := os.Getenv("SKILLMGR_TEST_ROOT"); testRoot != "" {
return testRoot, nil
}
// 生产模式:使用用户主目录
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ConfigDir), nil
}
// GetRepositoryConfigPath 获取 repository.json 路径
func GetRepositoryConfigPath() (string, error) {
root, err := GetConfigRoot()
if err != nil {
return "", err
}
return filepath.Join(root, RepositoryFile), nil
}
// GetInstallConfigPath 获取 install.json 路径
func GetInstallConfigPath() (string, error) {
root, err := GetConfigRoot()
if err != nil {
return "", err
}
return filepath.Join(root, InstallFile), nil
}
// GetCachePath 获取缓存目录路径
func GetCachePath() (string, error) {
root, err := GetConfigRoot()
if err != nil {
return "", err
}
return filepath.Join(root, CacheDir), nil
}
// EnsureConfigDirs 确保配置目录存在
func EnsureConfigDirs() error {
root, err := GetConfigRoot()
if err != nil {
return err
}
dirs := []string{
root,
filepath.Join(root, CacheDir),
}
for _, dir := range dirs {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
}
return nil
}