Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to ledger-cli file to change currency #180

Merged
merged 1 commit into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ bazel-*

/tmp
*/**/*un~
*/**/*.test
*un~
.DS_Store
*/**/.DS_Store
Expand Down
11 changes: 9 additions & 2 deletions ledger-cli/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ var commandFile = &cli.Command{
Description: `
Loads a file in the ledger cli format
`,
Flags: []cli.Flag{},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "currency",
Aliases: []string{"c"},
Value: "USD",
Usage: "Specify the currency that the ledger file will be in, default to USD",
},
},
Action: func(ctx *cli.Context) error {
err, cfg := cmd.MakeConfig(ctx)
if err != nil {
Expand All @@ -60,7 +67,7 @@ var commandFile = &cli.Command{
return fmt.Errorf("Could not read file %s (%v)", ledgerFileName, err)
}

generalLedger, parseError := ParseLedger(ledgerFileReader)
generalLedger, parseError := ParseLedger(ledgerFileReader, ctx.String("currency"))
if parseError != nil {
return fmt.Errorf("Could not parse file (%v)", parseError)
}
Expand Down
8 changes: 4 additions & 4 deletions ledger-cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const (
// ParseLedger parses a ledger file and returns a list of Transactions.
//
// Transactions are sorted by date.
func ParseLedger(ledgerReader io.Reader) (generalLedger []*Transaction, err error) {
parseLedger(ledgerReader, func(t *Transaction, e error) (stop bool) {
func ParseLedger(ledgerReader io.Reader, currency string) (generalLedger []*Transaction, err error) {
parseLedger(ledgerReader, currency, func(t *Transaction, e error) (stop bool) {
if e != nil {
err = e
stop = true
Expand All @@ -55,7 +55,7 @@ func ParseLedger(ledgerReader io.Reader) (generalLedger []*Transaction, err erro

var accountToAmountSpace = regexp.MustCompile(" {2,}|\t+")

func parseLedger(ledgerReader io.Reader, callback func(t *Transaction, err error) (stop bool)) {
func parseLedger(ledgerReader io.Reader, currency string, callback func(t *Transaction, err error) (stop bool)) {
var trans *Transaction
scanner := bufio.NewScanner(ledgerReader)
var line string
Expand Down Expand Up @@ -122,7 +122,7 @@ func parseLedger(ledgerReader io.Reader, callback func(t *Transaction, err error
}
lastIndex := len(nonEmptyWords) - 1
balErr, rationalNum := getBalance(strings.Trim(nonEmptyWords[lastIndex], whitespace))
accChange.Currency = "USD"
accChange.Currency = currency
if !balErr {
// Assuming no balance and whole line is account name
accChange.Name = strings.Join(nonEmptyWords, " ")
Expand Down
3 changes: 3 additions & 0 deletions utils/ledgercli-files/transaction-codes-1.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2009/10/29 (XFER) Panera Bread
Expenses:Food 4.50
Assets:Checking
15 changes: 15 additions & 0 deletions utils/ledgercli-files/transaction-codes-2.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
2009/10/29 (XFER) Panera Bread
Expenses:Food 4.50
Assets:Checking

2009/10/30 (DEP) Pay day!
Assets:Checking 20.00
Income

2009/10/30 (XFER) Panera Bread
Expenses:Food 4.50
Assets:Checking

2009/10/31 (559385768438A8D7) Panera Bread
Expenses:Food 4.50
Liabilities:Credit Card