Skip to content

Commit

Permalink
update: config dir from .beancount-ns change to .beancount-gs
Browse files Browse the repository at this point in the history
  • Loading branch information
baoxuebin committed Dec 27, 2022
1 parent 7f83b3a commit 3ad974e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
20 changes: 19 additions & 1 deletion script/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
32 changes: 32 additions & 0 deletions script/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions script/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
File renamed without changes.

0 comments on commit 3ad974e

Please sign in to comment.