Skip to content
Open
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
18 changes: 10 additions & 8 deletions comdirect/cmd/balance.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cmd

import (
"encoding/csv"
"log"
"os"

"github.com/jsattler/go-comdirect/pkg/comdirect"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"log"
"os"
)

var (
Expand Down Expand Up @@ -39,12 +39,14 @@ func balance(cmd *cobra.Command, args []string) {
}

func printBalanceCSV(balances *comdirect.AccountBalances) {
table := csv.NewWriter(os.Stdout)
table.Write([]string{"ID", "TYPE", "IBAN", "BALANCE"})
for _, a := range balances.Values {
table.Write([]string{a.AccountId, a.Account.AccountType.Text, a.Account.Iban, a.Balance.Value})
t, err := getCSVTemplate("balance.tmpl")
if err != nil {
log.Fatal(err)
}
err = t.ExecuteTemplate(os.Stdout, t.Name(), balances)
if err != nil {
log.Fatal(err)
}
table.Flush()
}

func printBalanceTable(account *comdirect.AccountBalances) {
Expand Down
17 changes: 10 additions & 7 deletions comdirect/cmd/depot.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"encoding/csv"
"log"
"os"

"github.com/jsattler/go-comdirect/pkg/comdirect"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"os"
)

var (
Expand Down Expand Up @@ -38,12 +39,14 @@ func depot(cmd *cobra.Command, args []string) {
}

func printDepotsCSV(depots *comdirect.Depots) {
table := csv.NewWriter(os.Stdout)
table.Write([]string{"DEPOT ID", "DISPLAY ID", "HOLDER NAME", "CLIENT ID"})
for _, d := range depots.Values {
table.Write([]string{d.DepotId, d.DepotDisplayId, d.HolderName, d.ClientId})
t, err := getCSVTemplate("depot.tmpl")
if err != nil {
log.Fatal(err)
}
err = t.ExecuteTemplate(os.Stdout, t.Name(), depots)
if err != nil {
log.Fatal(err)
}
table.Flush()
}
func printDepotsTable(depots *comdirect.Depots) {
table := tablewriter.NewWriter(os.Stdout)
Expand Down
22 changes: 18 additions & 4 deletions comdirect/cmd/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package cmd

import (
"fmt"
"github.com/jsattler/go-comdirect/pkg/comdirect"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"log"
"os"
"strings"
"time"

"github.com/jsattler/go-comdirect/pkg/comdirect"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)

var (
Expand Down Expand Up @@ -52,6 +53,8 @@ func document(cmd *cobra.Command, args []string) {
printJSON(filtered)
case "markdown":
printDocumentTable(filtered)
case "csv":
printDocumentCSV(filtered)
default:
printDocumentTable(filtered)
}
Expand All @@ -68,13 +71,24 @@ func download(client *comdirect.Client, documents *comdirect.Documents) {
log.Fatal("failed to download document: ", err)
}
// TODO: think about a better solution to limit download requests to 10/sec
if i % 10 == 0 && i != 0{
if i%10 == 0 && i != 0 {
time.Sleep(900 * time.Millisecond)
}
fmt.Printf("Download complete for document with ID %s\n", d.DocumentID)
}
}

func printDocumentCSV(documents *comdirect.Documents) {
t, err := getCSVTemplate("document.tmpl")
if err != nil {
log.Fatal(err)
}
err = t.ExecuteTemplate(os.Stdout, t.Name(), documents)
if err != nil {
log.Fatal(err)
}
}

func printDocumentTable(documents *comdirect.Documents) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "NAME", "DATE", "OPENED", "TYPE"})
Expand Down
18 changes: 11 additions & 7 deletions comdirect/cmd/position.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"encoding/csv"
"log"
"os"

"github.com/jsattler/go-comdirect/pkg/comdirect"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"os"
)

var (
Expand Down Expand Up @@ -39,12 +40,15 @@ func position(cmd *cobra.Command, args []string) {
}

func printPositionsCSV(positions *comdirect.DepotPositions) {
table := csv.NewWriter(os.Stdout)
table.Write([]string{"POSITION ID", "WKN", "QUANTITY", "CURRENT PRICE", "PREVDAY %", "PURCHASE %", "PURCHASE", "CURRENT"})
for _, d := range positions.Values {
table.Write([]string{d.PositionId, d.Wkn, d.Quantity.Value, d.CurrentPrice.Price.Value, d.ProfitLossPrevDayRel, d.ProfitLossPurchaseRel, d.PurchaseValue.Value, d.CurrentValue.Value})
t, err := getCSVTemplate("position.tmpl")
if err != nil {
log.Fatal(err)
}
table.Flush()
err = t.ExecuteTemplate(os.Stdout, t.Name(), positions)
if err != nil {
log.Fatal(err)
}

}

func printPositionsTable(depots *comdirect.DepotPositions) {
Expand Down
23 changes: 10 additions & 13 deletions comdirect/cmd/report.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"encoding/csv"
"log"
"os"

"github.com/jsattler/go-comdirect/pkg/comdirect"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"os"
)

var (
Expand Down Expand Up @@ -39,18 +40,14 @@ func report(cmd *cobra.Command, args []string) {
}

func printReportsCSV(reports *comdirect.Reports) {
table := csv.NewWriter(os.Stdout)
table.Write(reportsHeader)
for _, r := range reports.Values {
var balance string
if r.Balance.Balance.Value == "" {
balance = formatAmountValue(r.Balance.PrevDayValue)
} else {
balance = formatAmountValue(r.Balance.Balance)
}
table.Write([]string{r.ProductID, r.ProductType, balance})
t, err := getCSVTemplate("report.tmpl")
if err != nil {
log.Fatal(err)
}
err = t.ExecuteTemplate(os.Stdout, t.Name(), reports)
if err != nil {
log.Fatal(err)
}
table.Flush()
}

func printReportsTable(reports *comdirect.Reports) {
Expand Down
23 changes: 23 additions & 0 deletions comdirect/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"text/template"
"time"

"github.com/jsattler/go-comdirect/comdirect/keychain"
"github.com/jsattler/go-comdirect/comdirect/tpl"
"github.com/jsattler/go-comdirect/pkg/comdirect"
"github.com/spf13/cobra"
)
Expand All @@ -26,6 +29,7 @@ var (
passwordFlag string
clientIDFlag string
clientSecretFlag string
templateFlag string

rootCmd = &cobra.Command{
Use: "comdirect",
Expand Down Expand Up @@ -54,6 +58,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&formatFlag, "format", "f", "markdown", "output format (markdown, csv or json)")
rootCmd.PersistentFlags().IntVarP(&timeoutFlag, "timeout", "t", 30, "timeout in seconds to validate session TAN (default 30sec)")
rootCmd.PersistentFlags().StringVar(&excludeFlag, "exclude", "", "exclude field from response")
rootCmd.PersistentFlags().StringVar(&templateFlag, "template", "", "template file name to format csv output")

rootCmd.AddCommand(documentCmd)
rootCmd.AddCommand(depotCmd)
Expand Down Expand Up @@ -108,3 +113,21 @@ func initClient() *comdirect.Client {
}
return comdirect.NewWithAuthentication(authentication)
}

func getCSVTemplate(tplName string) (*template.Template, error) {
if templateFlag != "" {
tplName = filepath.Base(templateFlag)
}

t := template.New(tplName).Funcs(
template.FuncMap{
"formatAmountValue": formatAmountValue,
"holderName": holderName,
},
)

if templateFlag != "" {
return t.ParseFiles(templateFlag)
}
return t.ParseFS(tpl.Default, tplName)
}
29 changes: 17 additions & 12 deletions comdirect/cmd/transaction.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"encoding/csv"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -112,19 +111,16 @@ func printJSON(v interface{}) {
}

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})
t, err := getCSVTemplate("transaction.tmpl")
if err != nil {
log.Fatal(err)
}
err = t.ExecuteTemplate(os.Stdout, t.Name(), transactions)
if err != nil {
log.Fatal(err)
}
table.Flush()
}

func printTransactionTable(transactions *comdirect.AccountTransactions) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(transactionHeader)
Expand All @@ -143,3 +139,12 @@ func printTransactionTable(transactions *comdirect.AccountTransactions) {
}
table.Render()
}

func holderName(holderName string) string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like that this function is not used or do I miss something? In the past the length of the holderName could be quite long, that's why I limited it to 30 chars.

if len(holderName) > 30 {
holderName = holderName[:30]
} else if holderName == "" {
holderName = "N/A"
}
return holderName
}
4 changes: 4 additions & 0 deletions comdirect/tpl/balance.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ID,TYPE,IBAN,BALANCE
{{- range .Values}}
{{.AccountId}},{{.Account.AccountType.Text}},{{.Account.Iban}},{{.Balance.Value}}
{{- end}}
4 changes: 4 additions & 0 deletions comdirect/tpl/depot.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DEPOT ID,DISPLAY ID,HOLDER NAME,CLIENT ID
{{- range .Values}}
{{.DepotId}},{{.DepotDisplayId}},{{.HolderName}},{{.ClientId}}
{{- end}}
4 changes: 4 additions & 0 deletions comdirect/tpl/document.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ID,NAME,DATE,OPENED,TYPE
{{- range .Values}}
{{.DocumentID}},"{{.Name}}",{{.DateCreation}},{{.DocumentMetaData.AlreadyRead}},{{.MimeType}}
{{- end}}
4 changes: 4 additions & 0 deletions comdirect/tpl/position.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POSITION ID,WKN,QUANTITY,CURRENT PRICE,PREVDAY %,PURCHASE %,PURCHASE,CURRENT
{{- range .Values}}
{{.PositionId}},{{.Wkn}},{{.Quantity.Value}},{{.CurrentPrice.Price.Value}},{{.ProfitLossPrevDayRel}},{{.ProfitLossPurchaseRel}},{{.PurchaseValue.Value}},{{.CurrentValue.Value}}
{{- end}}
4 changes: 4 additions & 0 deletions comdirect/tpl/report.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ID,TYPE,BALANCE
{{- range .Values}}
{{.ProductID}},{{.ProductType}},{{if eq .Balance.Balance.Value ""}}{{formatAmountValue .Balance.PrevDayValue}}{{else}}{{formatAmountValue .Balance.Balance}}{{end}}
{{- end}}
8 changes: 8 additions & 0 deletions comdirect/tpl/tpl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tpl

import (
"embed"
)

//go:embed *.tmpl
var Default embed.FS
4 changes: 4 additions & 0 deletions comdirect/tpl/transaction.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
REMITTER,DEPTOR,BOOKING DATE,STATUS,TYPE,VALUE,UNIT
{{- range .Values}}
"{{holderName .Remitter.HolderName}}","{{.Creditor.HolderName}}",{{.BookingDate}},{{.BookingStatus}},{{.TransactionType.Text}},{{formatAmountValue .Amount}},{{.Amount.Unit}}
{{- end}}
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.7.0
github.com/zalando/go-keyring v0.2.3
golang.org/x/term v0.11.0
golang.org/x/term v0.12.0
golang.org/x/time v0.3.0
)

require (
github.com/alessio/shellescape v1.4.1 // indirect
github.com/alessio/shellescape v1.4.2 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.3.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/sys v0.12.0 // indirect
)
Loading