diff --git a/script/config.go b/script/config.go index 2a01d97..f783ca0 100644 --- a/script/config.go +++ b/script/config.go @@ -237,7 +237,25 @@ func LoadLedgerAccountsMap() error { ledgerAccountsMap = make(map[string][]Account) } for _, config := range ledgerConfigMap { - err := LoadLedgerAccounts(config.Id) + // 兼容性处理 + err := handleCompatible(config) + if err != nil { + return err + } + err = LoadLedgerAccounts(config.Id) + if err != nil { + return err + } + } + return nil +} + +func handleCompatible(config Config) error { + // 兼容性处理,.beancount-ns -> .beancount-gs + beancountGsConfigPath := GetLedgerConfigDocument(config.DataPath) + beancountNsConfigPath := GetCompatibleLedgerConfigDocument(config.DataPath) + if FileIfExist(beancountNsConfigPath) && !FileIfExist(beancountGsConfigPath) { + err := CopyDir(beancountNsConfigPath, beancountGsConfigPath) if err != nil { return err } diff --git a/script/file.go b/script/file.go index ac796de..1471bed 100644 --- a/script/file.go +++ b/script/file.go @@ -110,6 +110,38 @@ func CopyFile(sourceFilePath string, targetFilePath string) error { return nil } +func CopyDir(sourceDir string, targetDir string) error { + dirs, err := os.ReadDir(sourceDir) + if err != nil { + return err + } + err = MkDir(targetDir) + if err != nil { + return err + } + for _, dir := range dirs { + newSourceDir := filepath.Join(sourceDir, dir.Name()) + newTargetDir := filepath.Join(targetDir, dir.Name()) + if dir.IsDir() { + err := CopyFile(newSourceDir, newTargetDir) + if err != nil { + LogSystemError("Failed to copy dir from [" + newSourceDir + "] to [" + newTargetDir + "]") + return err + } + } else { + err := CreateFileIfNotExist(newTargetDir) + if err != nil { + return err + } + err = CopyFile(newSourceDir, newTargetDir) + if err != nil { + return err + } + } + } + return nil +} + func MkDir(dirPath string) error { err := os.MkdirAll(dirPath, os.ModePerm) if nil != err { diff --git a/script/paths.go b/script/paths.go index 3c29b3c..b228732 100644 --- a/script/paths.go +++ b/script/paths.go @@ -24,12 +24,20 @@ func GetTemplateLedgerConfigDirPath() string { return currentPath + "/template" } +func GetLedgerConfigDocument(dataPath string) string { + return dataPath + "/.beancount-gs" +} + +func GetCompatibleLedgerConfigDocument(dataPath string) string { + return dataPath + "/.beancount-ns" +} + func GetLedgerTransactionsTemplateFilePath(dataPath string) string { - return dataPath + "/.beancount-ns/transaction_template.json" + return dataPath + "/.beancount-gs/transaction_template.json" } func GetLedgerAccountTypeFilePath(dataPath string) string { - return dataPath + "/.beancount-ns/account_type.json" + return dataPath + "/.beancount-gs/account_type.json" } func GetLedgerPriceFilePath(dataPath string) string { diff --git a/template/.beancount-ns/account_type.json b/template/.beancount-gs/account_type.json similarity index 100% rename from template/.beancount-ns/account_type.json rename to template/.beancount-gs/account_type.json