update: config dir from .beancount-ns change to .beancount-gs

This commit is contained in:
baoxuebin 2022-12-27 18:41:34 +08:00
parent 7f83b3a380
commit 3ad974e4eb
4 changed files with 61 additions and 3 deletions

View File

@ -237,7 +237,25 @@ func LoadLedgerAccountsMap() error {
ledgerAccountsMap = make(map[string][]Account) ledgerAccountsMap = make(map[string][]Account)
} }
for _, config := range ledgerConfigMap { 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 { if err != nil {
return err return err
} }

View File

@ -110,6 +110,38 @@ func CopyFile(sourceFilePath string, targetFilePath string) error {
return nil 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 { func MkDir(dirPath string) error {
err := os.MkdirAll(dirPath, os.ModePerm) err := os.MkdirAll(dirPath, os.ModePerm)
if nil != err { if nil != err {

View File

@ -24,12 +24,20 @@ func GetTemplateLedgerConfigDirPath() string {
return currentPath + "/template" 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 { func GetLedgerTransactionsTemplateFilePath(dataPath string) string {
return dataPath + "/.beancount-ns/transaction_template.json" return dataPath + "/.beancount-gs/transaction_template.json"
} }
func GetLedgerAccountTypeFilePath(dataPath string) string { func GetLedgerAccountTypeFilePath(dataPath string) string {
return dataPath + "/.beancount-ns/account_type.json" return dataPath + "/.beancount-gs/account_type.json"
} }
func GetLedgerPriceFilePath(dataPath string) string { func GetLedgerPriceFilePath(dataPath string) string {