feat: 修复配置路径BUG并迁移onboard到huh
All checks were successful
Release / build (push) Successful in 12m14s

- 新增路径解析工具 internal/config/path.go
- 配置查找优先级: --config > ~/.config/yoyo/config.yaml > ./configs/config.yaml
- onboard配置保存到 ~/.config/yoyo/config.yaml (符合XDG规范)
- .env文件从 ~/.config/yoyo/.env 加载
- onboard使用huh替代survey库,更现代的交互体验
- 添加Ctrl+C取消支持,打印'你已取消本次配置'
- 保存前增加确认步骤
- 版本号 v0.5.1 -> v1.1.0
This commit is contained in:
2026-04-07 23:51:33 +08:00
parent 21e4710829
commit c0156a88d6
7 changed files with 326 additions and 232 deletions

76
internal/config/path.go Normal file
View File

@@ -0,0 +1,76 @@
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
)
const (
// ConfigDirName 配置目录名称
ConfigDirName = "yoyo"
// ConfigFileName 配置文件名
ConfigFileName = "config.yaml"
// EnvFileName 环境变量文件名
EnvFileName = ".env"
)
// ResolveConfigPath 解析配置文件路径
// 优先级: 用户指定路径 > ~/.config/yoyo/config.yaml > ./configs/config.yaml
func ResolveConfigPath(userPath string) (string, error) {
// 1. 用户通过 --config 指定的路径
if userPath != "" {
return expandPath(userPath)
}
// 2. 标准用户配置目录 ~/.config/yoyo/config.yaml
userConfigPath := GetUserConfigPath()
if _, err := os.Stat(userConfigPath); err == nil {
return userConfigPath, nil
}
// 3. 项目本地配置 ./configs/config.yaml向后兼容
localConfigPath := "configs/config.yaml"
if _, err := os.Stat(localConfigPath); err == nil {
return localConfigPath, nil
}
// 4. 都不存在返回标准路径onboard 会创建)
return userConfigPath, nil
}
// GetUserConfigDir 获取用户配置目录路径
// 返回 ~/.config/yoyo
func GetUserConfigDir() string {
home, err := os.UserHomeDir()
if err != nil {
// 降级到当前目录
return ".config/" + ConfigDirName
}
return filepath.Join(home, ".config", ConfigDirName)
}
// GetUserConfigPath 获取用户配置文件路径
// 返回 ~/.config/yoyo/config.yaml
func GetUserConfigPath() string {
return filepath.Join(GetUserConfigDir(), ConfigFileName)
}
// GetUserEnvPath 获取用户环境变量文件路径
// 返回 ~/.config/yoyo/.env
func GetUserEnvPath() string {
return filepath.Join(GetUserConfigDir(), EnvFileName)
}
// expandPath 展开路径中的 ~ 符号
func expandPath(path string) (string, error) {
if strings.HasPrefix(path, "~") {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("无法获取用户主目录: %w", err)
}
path = filepath.Join(home, path[1:])
}
return path, nil
}