From 4179d9479a24cc2be359274e9cd13002a4cf5e51 Mon Sep 17 00:00:00 2001 From: Rauno Ots Date: Tue, 17 Apr 2018 10:23:05 +0200 Subject: [PATCH] add csv output format for transactions separate different output formats --- cmd/n26/csv.go | 24 ++++++++ cmd/n26/json.go | 18 ++++++ cmd/n26/n26.go | 141 ++++++++++++++++++++++++++--------------------- cmd/n26/table.go | 20 +++++++ 4 files changed, 139 insertions(+), 64 deletions(-) create mode 100644 cmd/n26/csv.go create mode 100644 cmd/n26/json.go create mode 100644 cmd/n26/table.go diff --git a/cmd/n26/csv.go b/cmd/n26/csv.go new file mode 100644 index 0000000..fc2c74d --- /dev/null +++ b/cmd/n26/csv.go @@ -0,0 +1,24 @@ +package main + +import ( + "encoding/csv" + "io" +) + +type csvWriter struct { + *csv.Writer +} + +func NewCsvWriter(target io.Writer) (*csvWriter, error) { + writer := csv.NewWriter(target) + return &csvWriter{writer}, nil +} +func (w *csvWriter) WriteData(header []string, data [][]string) error { + if err := w.Write(header); err != nil { + return err + } + if err := w.WriteAll(data); err != nil { + return err + } + return nil +} diff --git a/cmd/n26/json.go b/cmd/n26/json.go new file mode 100644 index 0000000..beee4d6 --- /dev/null +++ b/cmd/n26/json.go @@ -0,0 +1,18 @@ +package main + +import ( + "encoding/json" + "fmt" + "github.com/guitmz/n26" +) + +type jsonWriter struct{} + +func (w jsonWriter) WriteTransactions(t *n26.Transactions) error { + formatted, err := json.MarshalIndent(t, "", " ") + if err != nil { + return err + } + fmt.Println(string(formatted)) + return nil +} diff --git a/cmd/n26/n26.go b/cmd/n26/n26.go index fc46710..e484b2a 100644 --- a/cmd/n26/n26.go +++ b/cmd/n26/n26.go @@ -11,7 +11,6 @@ import ( "github.com/guitmz/n26" "github.com/howeyc/gopass" - "github.com/olekukonko/tablewriter" "github.com/urfave/cli" ) @@ -37,8 +36,15 @@ func authentication() *n26.Auth { return &n26.Auth{username, password} } +// Interface for generic data writer that has a header and data table e.g. table writer and csv writer +type dataWriter interface { + WriteData(header []string, data [][]string) error +} +type transactionWriter interface { + WriteTransactions(t *n26.Transactions) error +} + func main() { - table := tablewriter.NewWriter(os.Stdout) app := cli.NewApp() app.Version = "1.2.0" app.UsageText = "n26 command [json|statement ID]" @@ -59,9 +65,7 @@ func main() { available := strconv.FormatFloat(balance.AvailableBalance, 'f', -1, 64) usable := strconv.FormatFloat(balance.UsableBalance, 'f', -1, 64) data := [][]string{[]string{balance.IBAN, balance.BIC, available, usable}} - table.SetHeader([]string{"IBAN", "BIC", "Available Balance", "Usable Balance"}) - table.AppendBulk(data) - table.Render() + NewTableWriter().WriteData([]string{"IBAN", "BIC", "Available Balance", "Usable Balance"}, data) } return nil }, @@ -76,9 +80,7 @@ func main() { fmt.Println(prettyJSON) } else { data := [][]string{[]string{fmt.Sprintf("%s %s", info.FirstName, info.LastName), info.Email, info.MobilePhoneNumber}} - table.SetHeader([]string{"Full Name", "Email", "Mobile Phone Number"}) - table.AppendBulk(data) - table.Render() + NewTableWriter().WriteData([]string{"Full Name", "Email", "Mobile Phone Number"}, data) } return nil }, @@ -97,9 +99,7 @@ func main() { time.Unix(status.Created, 0).String(), }, } - table.SetHeader([]string{"Created"}) - table.AppendBulk(data) - table.Render() + NewTableWriter().WriteData([]string{"Created"}, data) } return nil }, @@ -125,9 +125,7 @@ func main() { }, ) } - table.SetHeader([]string{"Address", "Number", "Zipcode", "City", "Type"}) - table.AppendBulk(data) - table.Render() + NewTableWriter().WriteData([]string{"Address", "Number", "Zipcode", "City", "Type"}, data) } return nil }, @@ -160,9 +158,7 @@ func main() { }, ) } - table.SetHeader([]string{"Name on Card", "Type", "Product type", "Number"}) - table.AppendBulk(data) - table.Render() + NewTableWriter().WriteData([]string{"Name on Card", "Type", "Product type", "Number"}, data) } return nil }, @@ -186,9 +182,7 @@ func main() { }, ) } - table.SetHeader([]string{"Limit", "Amount"}) - table.AppendBulk(data) - table.Render() + NewTableWriter().WriteData([]string{"Limit", "Amount"}, data) } return nil }, @@ -213,53 +207,22 @@ func main() { }, ) } - table.SetHeader([]string{"Name", "IBAN", "BIC", "Type"}) - table.AppendBulk(data) - table.Render() + NewTableWriter().WriteData([]string{"Name", "IBAN", "BIC", "Type"}, data) } return nil }, }, { - Name: "transactions", - Usage: "your past transactions", - Action: func(c *cli.Context) error { + Name: "transactions", + Usage: "list your past transactions", + ArgsUsage: "[csv|json|table]", + Action: func(c *cli.Context) (err error) { API := authentication() - prettyJSON, transactions := API.GetTransactions(c.Args().First()) - if prettyJSON != "" { - fmt.Println(prettyJSON) - } else { - data := [][]string{} - for _, transaction := range *transactions { - amount := strconv.FormatFloat(transaction.Amount, 'f', -1, 64) - var location string - if transaction.MerchantCity != "" { - location = transaction.MerchantCity - if transaction.MerchantCountry != 0 { - location += ", " - } - } - if transaction.MerchantCountry != 0 { - location += "Country Code: " + fmt.Sprint(transaction.MerchantCountry) - } - data = append(data, - []string{ - transaction.PartnerName, - transaction.PartnerIban, - transaction.PartnerBic, - transaction.MerchantName, - location, - amount, - transaction.CurrencyCode, - transaction.Type, - }, - ) - } - table.SetHeader([]string{"Name", "IBAN", "BIC", "Merchant", "Location", "Amount", "Currency", "Type"}) - table.AppendBulk(data) - table.Render() - } - return nil + writer, err := getTransactionWriter(c.Args().First()) + check(err) + _, transactions := API.GetTransactions("") + err = writer.WriteTransactions(transactions) + return }, }, { @@ -286,9 +249,7 @@ func main() { }, ) } - table.SetHeader([]string{"ID"}) - table.AppendBulk(data) - table.Render() + NewTableWriter().WriteData([]string{"ID"}, data) } } return nil @@ -299,3 +260,55 @@ func main() { sort.Sort(cli.CommandsByName(app.Commands)) app.Run(os.Args) } + +func getTransactionWriter(outType string) (transactionWriter, error) { + if outType == "json" { + return jsonWriter{}, nil + } + var table dataWriter + if outType == "csv" { + var err error + table, err = NewCsvWriter(os.Stdout) + if err != nil { + return nil, err + } + } else { + table = NewTableWriter() + } + return transactionToStringWriter{table}, nil +} + +type transactionToStringWriter struct { + out dataWriter +} + +func (w transactionToStringWriter) WriteTransactions(transactions *n26.Transactions) error { + data := [][]string{} + for _, transaction := range *transactions { + amount := strconv.FormatFloat(transaction.Amount, 'f', -1, 64) + var location string + if transaction.MerchantCity != "" { + location = transaction.MerchantCity + if transaction.MerchantCountry != 0 { + location += ", " + } + } + if transaction.MerchantCountry != 0 { + location += "Country Code: " + fmt.Sprint(transaction.MerchantCountry) + } + data = append(data, + []string{ + transaction.PartnerName, + transaction.PartnerIban, + transaction.PartnerBic, + transaction.MerchantName, + location, + amount, + transaction.CurrencyCode, + transaction.Type, + }, + ) + } + return w.out.WriteData([]string{"Name", "IBAN", "BIC", "Merchant", "Location", "Amount", "Currency", "Type"}, + data) +} diff --git a/cmd/n26/table.go b/cmd/n26/table.go new file mode 100644 index 0000000..532688d --- /dev/null +++ b/cmd/n26/table.go @@ -0,0 +1,20 @@ +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +type tblWriter struct { + *tablewriter.Table +} + +func NewTableWriter() *tblWriter { + return &tblWriter{tablewriter.NewWriter(os.Stdout)} +} +func (table *tblWriter) WriteData(header []string, data [][]string) error { + table.SetHeader(header) + table.AppendBulk(data) + table.Render() + return nil +}