This commit is contained in:
leokun
2026-06-30 10:38:52 +08:00
commit c083be5ec2
312 changed files with 146628 additions and 0 deletions
+83
View File
@@ -0,0 +1,83 @@
package appdata
import (
"fmt"
"io"
"os"
"path/filepath"
)
func ensureAssistantHome() error {
migrateLegacyAssistantHome()
if err := os.MkdirAll(RootDir(), 0o755); err != nil {
return fmt.Errorf("create assistant home: %w", err)
}
if err := os.MkdirAll(DataRootPath(), 0o755); err != nil {
return fmt.Errorf("create data root: %w", err)
}
if err := os.MkdirAll(HistoryRootPath(), 0o755); err != nil {
return fmt.Errorf("create history root: %w", err)
}
if err := os.MkdirAll(RulesRootPath(), 0o755); err != nil {
return fmt.Errorf("create rules root: %w", err)
}
if err := os.MkdirAll(LogsRootPath(), 0o755); err != nil {
return fmt.Errorf("create logs root: %w", err)
}
return nil
}
func EnsureAssistantHome() error {
return ensureAssistantHome()
}
func migrateLegacyAssistantHome() {
legacyRoot := legacyRootDir()
copyLegacyFile(filepath.Join(legacyRoot, "config.yaml"), filepath.Join(RootDir(), "config.yaml"))
copyLegacyRules(filepath.Join(legacyRoot, "rules"), RulesRootPath())
_ = os.RemoveAll(legacyRoot)
}
func copyLegacyRules(sourceRoot string, targetRoot string) {
_ = filepath.Walk(sourceRoot, func(path string, info os.FileInfo, err error) error {
if err != nil || info == nil {
return nil
}
rel, err := filepath.Rel(sourceRoot, path)
if err != nil {
return nil
}
targetPath := filepath.Join(targetRoot, rel)
if info.IsDir() {
_ = os.MkdirAll(targetPath, info.Mode().Perm())
return nil
}
if !info.Mode().IsRegular() {
return nil
}
copyLegacyFile(path, targetPath)
return nil
})
}
func copyLegacyFile(sourcePath string, targetPath string) {
sourceFile, err := os.Open(sourcePath)
if err != nil {
return
}
defer sourceFile.Close()
info, err := sourceFile.Stat()
if err != nil || !info.Mode().IsRegular() {
return
}
if err := os.MkdirAll(filepath.Dir(targetPath), 0o755); err != nil {
return
}
targetFile, err := os.OpenFile(targetPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode().Perm())
if err != nil {
return
}
defer targetFile.Close()
_, _ = io.Copy(targetFile, sourceFile)
}
+72
View File
@@ -0,0 +1,72 @@
package appdata
import (
"os"
"path/filepath"
"strings"
)
const (
appDirName = ".cursor-local-assistant-v2"
legacyAppDirName = ".cursor-local-assistant"
)
// RootDir 返回应用配置根目录。
func RootDir() string {
return appRootDir(appDirName)
}
func legacyRootDir() string {
return appRootDir(legacyAppDirName)
}
func appRootDir(dirName string) string {
homeDir, err := os.UserHomeDir()
if err != nil || strings.TrimSpace(homeDir) == "" {
return dirName
}
return filepath.Join(homeDir, dirName)
}
// ConfigFilePath 返回统一用户配置文件路径。
func ConfigFilePath() string {
return filepath.Join(RootDir(), "config.yaml")
}
func DataRootPath() string {
return filepath.Join(RootDir(), "data")
}
func HistoryRootPath() string {
return filepath.Join(RootDir(), "history")
}
func UsageFilePath() string {
return filepath.Join(HistoryRootPath(), "usage.json")
}
func AdsRootPath() string {
return filepath.Join(DataRootPath(), "ads")
}
func CodebaseIndexRootPath() string {
return filepath.Join(DataRootPath(), "codebase-index")
}
func DocsIndexRootPath() string {
return filepath.Join(DataRootPath(), "docs-index")
}
func RulesRootPath() string {
return filepath.Join(RootDir(), "rules")
}
// LogsRootPath 返回统一日志根目录路径。
func LogsRootPath() string {
return filepath.Join(RootDir(), "logs")
}
// CACertFilePath 返回注入给宿主的 CA 文件路径。
func CACertFilePath() string {
return filepath.Join(DataRootPath(), "ca.crt")
}