mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-08-17 19:47:10 +08:00
v0.3.8
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user