Skip to content

Commit

Permalink
add beancount syntax check api
Browse files Browse the repository at this point in the history
  • Loading branch information
BaoXuebin committed Aug 14, 2022
1 parent 2839749 commit 735e322
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions script/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ func GetLedgerPriceFilePath(dataPath string) string {
func GetLedgerMonthsFilePath(dataPath string) string {
return dataPath + "/month/months.bean"
}

func GetLedgerIndexFilePath(dataPath string) string {
LogInfo(dataPath, dataPath+"/index.bean")
return dataPath + "/index.bean"
}
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func RegisterRouter(router *gin.Engine) {
authorized.POST("/file", service.UpdateLedgerSourceFileContent)
authorized.POST("/import/alipay", service.ImportAliPayCSV)
authorized.POST("/import/wx", service.ImportWxPayCSV)
authorized.GET("/ledger/check", service.CheckLedger)
authorized.DELETE("/ledger", service.DeleteLedger)
}
}
Expand Down
22 changes: 22 additions & 0 deletions service/ledger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"bytes"
"crypto/sha1"
"encoding/hex"
"io"
Expand Down Expand Up @@ -207,6 +208,27 @@ func DeleteLedger(c *gin.Context) {
OK(c, "OK")
}

func CheckLedger(c *gin.Context) {
var stderr bytes.Buffer
ledgerConfig := script.GetLedgerConfigFromContext(c)
cmd := exec.Command("bean-check", script.GetLedgerIndexFilePath(ledgerConfig.DataPath))
cmd.Stderr = &stderr
output, err := cmd.Output()
if err != nil {
errors := strings.Split(stderr.String(), "\r\n")
result := make([]string, 0)
for _, e := range errors {
if e == "" {
continue
}
result = append(result, e)
}
OK(c, result)
} else {
OK(c, string(output))
}
}

func createNewLedger(loginForm LoginForm, ledgerId string) (*script.Config, error) {
// create new ledger
serverConfig := script.GetServerConfig()
Expand Down
1 change: 1 addition & 0 deletions service/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func saveTransaction(c *gin.Context, addTransactionForm AddTransactionForm, ledg
}
// 判断是否涉及多币种的转换
if account.Currency != ledgerConfig.OperatingCurrency && entry.Account != ledgerConfig.OpeningBalances {
autoBalance = true
// 根据 number 的正负来判断是买入还是卖出
if entry.Number.GreaterThan(decimal.NewFromInt(0)) {
// {351.729 CNY, 2021-09-29}
Expand Down

0 comments on commit 735e322

Please sign in to comment.