Skip to content

Commit

Permalink
Support formatting as csv, json and markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jsattler committed Dec 29, 2021
1 parent 490baa3 commit dabe2af
Showing 1 changed file with 45 additions and 15 deletions.
60 changes: 45 additions & 15 deletions comdirect/cmd/transaction.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package cmd

import (
"context"
"encoding/csv"
"encoding/json"
"fmt"
"github.com/jsattler/go-comdirect/pkg/comdirect"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"log"
"os"
"time"
)

var (
transactionCmd = &cobra.Command{
transactionHeader = []string{"REMITTER", "DEPTOR", "BOOKING DATE", "STATUS", "TYPE", "VALUE", "UNIT"}
transactionCmd = &cobra.Command{
Use: "transaction",
Short: "list account transactions",
Args: cobra.MinimumNArgs(1),
Expand All @@ -21,23 +22,56 @@ var (
)

func transaction(cmd *cobra.Command, args []string) {
client := InitClient()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
client := initClient()
ctx, cancel := contextWithTimeout()
defer cancel()

options := comdirect.EmptyOptions()
options.Add(comdirect.PagingCountQueryKey, pageCount)
options.Add(comdirect.PagingFirstQueryKey, pageIndex)
options.Add(comdirect.PagingCountQueryKey, countFlag)
options.Add(comdirect.PagingFirstQueryKey, indexFlag)
transactions, err := client.Transactions(ctx, args[0], options)
if err != nil {
log.Fatal(err)
}
printTransactionTable(transactions)

switch formatFlag {
case "json":
printJSON(transactions)
case "markdown":
printTransactionTable(transactions)
case "csv":
printTransactionCSV(transactions)
default:
printTransactionTable(transactions)
}
}

func printJSON(v interface{}) {
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}

func printTransactionCSV(transactions *comdirect.AccountTransactions) {
table := csv.NewWriter(os.Stdout)
table.Write(transactionHeader)
for _, t := range transactions.Values {
holderName := t.Remitter.HolderName
if len(holderName) > 30 {
holderName = holderName[:30]
} else if holderName == "" {
holderName = "N/A"
}
table.Write([]string{holderName, t.Creditor.HolderName, t.BookingDate, t.BookingStatus, t.TransactionType.Text, formatAmountValue(t.Amount), t.Amount.Unit})
}
table.Flush()
}
func printTransactionTable(transactions *comdirect.AccountTransactions) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"REMITTER", "DEPTOR", "BOOKING DATE", "STATUS", "TYPE", "AMOUNT"})
table.SetHeader(transactionHeader)
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_RIGHT})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetCaption(true, fmt.Sprintf("%d out of %d", len(transactions.Values), transactions.Paging.Matches))
Expand All @@ -46,13 +80,9 @@ func printTransactionTable(transactions *comdirect.AccountTransactions) {
if len(holderName) > 30 {
holderName = holderName[:30]
} else if holderName == "" {
holderName = "UNKNOWN"
}
value := t.Amount.Value
if value[0] != '-' {
value = "+" + value
holderName = "N/A"
}
table.Append([]string{holderName, t.Creditor.HolderName, t.BookingDate, t.BookingStatus, t.TransactionType.Text, boldGreen(value)})
table.Append([]string{holderName, t.Creditor.HolderName, t.BookingDate, t.BookingStatus, t.TransactionType.Text, formatAmountValue(t.Amount), t.Amount.Unit})
}
table.Render()
}

0 comments on commit dabe2af

Please sign in to comment.