Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from engvik/feature/json-output
Browse files Browse the repository at this point in the history
feature: json output
  • Loading branch information
engvik authored Feb 9, 2021
2 parents 5c67e74 + 7020680 commit 29623dc
Show file tree
Hide file tree
Showing 35 changed files with 1,044 additions and 195 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
default: build

APP=sbanken
VERSION=1.4.0
VERSION=1.5.0

## build: build binaries and generate checksums
build:
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ Global option: `--http-timeout value`
Config option: `http-timeout: value`
Default: `30`

### Output

Set output format.

Global option: `--output value`
Config option: `output: value`
Default: `table`

Available formats:

```
table
json
```

## Usage

```
Expand All @@ -148,7 +163,7 @@ USAGE:
main [global options] command [command options] [arguments...]

VERSION:
1.3.0
1.5.0

COMMANDS:
accounts, a list and read accounts
Expand All @@ -166,6 +181,7 @@ GLOBAL OPTIONS:
--client-secret value, -s value the client secret [$SBANKEN_CLIENT_SECRET]
--customer-id value, --cuid value customer id [$SBANKEN_CUSTOMER_ID]
--style value set output style
--output value set output format (default: "table")
--colors add colors to values (default: false)
--http-timeout value timeout in seconds (default: 30)
--config value, -c value path to YAML config [$SBANKEN_CONFIG]
Expand Down
9 changes: 3 additions & 6 deletions cmd/sbanken/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@ import (

"github.com/engvik/sbanken-cli/internal/cli"
"github.com/engvik/sbanken-cli/internal/sbanken"
"github.com/engvik/sbanken-cli/internal/table"
)

// VERSION is the current sbanken-cli version
const VERSION string = "1.4.0"
const VERSION string = "1.5.0"

func main() {
ctx := context.Background()
writer := table.NewWriter()
writer.SetOutputMirror(os.Stdout)

conn, err := sbanken.NewEmptyConnection(writer)
conn, err := sbanken.NewEmptyConnection()
if err != nil {
log.Fatal(err)
}

app := cli.New(ctx, conn, writer, VERSION)
app := cli.New(ctx, conn, VERSION)

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
Expand Down
11 changes: 3 additions & 8 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type sbankenConn interface {
ConnectClient(context.Context, *cli.Context, string) error
SetConfig(*sbanken.Config)
SetWriter(*cli.Context)
ListAccounts(*cli.Context) error
ReadAccount(*cli.Context) error
ListCards(*cli.Context) error
Expand All @@ -29,13 +30,8 @@ type sbankenConn interface {
GetCustomer(*cli.Context) error
}

type tableWriter interface {
SetStyle(string)
SetColors(bool)
}

// New creates a new cli app.
func New(ctx context.Context, conn sbankenConn, tw tableWriter, version string) *cli.App {
func New(ctx context.Context, conn sbankenConn, version string) *cli.App {
flags := getGlobalFlags()

app := &cli.App{
Expand Down Expand Up @@ -93,8 +89,7 @@ func New(ctx context.Context, conn sbankenConn, tw tableWriter, version string)
conn.SetConfig(cfg)
}

tw.SetStyle(c.String("style"))
tw.SetColors(c.Bool("colors"))
conn.SetWriter(c)

return nil
},
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/customers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ func getCustomerCommand(conn customers) *cli.Command {
Name: "customer",
Usage: "get customer data",
Aliases: []string{"cu"},
Action: conn.GetCustomer,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "customer-id",
Usage: "include customer id in output",
},
},
Action: conn.GetCustomer,
}
}
5 changes: 5 additions & 0 deletions internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func getGlobalFlags() []cli.Flag {
Name: "style",
Usage: "set output style",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "output",
Usage: "set output format",
Value: "table",
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "colors",
Usage: "add colors to values",
Expand Down
28 changes: 28 additions & 0 deletions internal/output/json/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package json

import (
"encoding/json"
"log"

"github.com/engvik/sbanken-go"
)

func (w *Writer) ListAccounts(accounts []sbanken.Account) {
json, err := json.MarshalIndent(accounts, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}

func (w *Writer) ReadAccount(account sbanken.Account) {
json, err := json.MarshalIndent(account, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}
18 changes: 18 additions & 0 deletions internal/output/json/cards.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package json

import (
"encoding/json"
"log"

"github.com/engvik/sbanken-go"
)

func (w *Writer) ListCards(cards []sbanken.Card) {
json, err := json.MarshalIndent(cards, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}
18 changes: 18 additions & 0 deletions internal/output/json/customers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package json

import (
"encoding/json"
"log"

"github.com/engvik/sbanken-go"
)

func (w *Writer) GetCustomer(customer sbanken.Customer) {
json, err := json.MarshalIndent(customer, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}
38 changes: 38 additions & 0 deletions internal/output/json/efakturas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package json

import (
"encoding/json"
"log"

"github.com/engvik/sbanken-go"
)

func (w *Writer) ListEfakturas(efakturas []sbanken.Efaktura) {
json, err := json.MarshalIndent(efakturas, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}

func (w *Writer) PayEfaktura(q *sbanken.EfakturaPayQuery) {
json, err := json.MarshalIndent(q, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}

func (w *Writer) ReadEfaktura(efaktura sbanken.Efaktura) {
json, err := json.MarshalIndent(efaktura, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}
21 changes: 21 additions & 0 deletions internal/output/json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package json

import (
"io"
"log"
)

type Writer struct{}

func NewWriter() *Writer {
return &Writer{}
}

func (w *Writer) SetOutputMirror(m io.Writer) {
log.SetFlags(0)
log.SetOutput(m)
}

func (w *Writer) SetStyle(_ string) {}

func (w *Writer) SetColors(_ bool) {}
28 changes: 28 additions & 0 deletions internal/output/json/payments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package json

import (
"encoding/json"
"log"

"github.com/engvik/sbanken-go"
)

func (w *Writer) ListPayments(payments []sbanken.Payment) {
json, err := json.MarshalIndent(payments, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}

func (w *Writer) ReadPayment(payment sbanken.Payment) {
json, err := json.MarshalIndent(payment, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}
18 changes: 18 additions & 0 deletions internal/output/json/standing_orders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package json

import (
"encoding/json"
"log"

"github.com/engvik/sbanken-go"
)

func (w *Writer) ListStandingOrders(standingOrders []sbanken.StandingOrder, _ bool) {
json, err := json.MarshalIndent(standingOrders, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}
18 changes: 18 additions & 0 deletions internal/output/json/transactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package json

import (
"encoding/json"
"log"

"github.com/engvik/sbanken-go"
)

func (w *Writer) ListTransactions(transactions []sbanken.Transaction, _ bool, _ bool, _ bool) {
json, err := json.MarshalIndent(transactions, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}
18 changes: 18 additions & 0 deletions internal/output/json/transfer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package json

import (
"encoding/json"
"log"

"github.com/engvik/sbanken-go"
)

func (w *Writer) Transfer(q *sbanken.TransferQuery) {
json, err := json.MarshalIndent(q, "", " ")
if err != nil {
log.Printf("Error creating json output: %s\n", err)
return
}

log.Println(string(json))
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func (w *Writer) GetCustomer(customer sbanken.Customer) {
w.table.AppendRow(table.Row{"CustomerID", customer.CustomerID})
w.table.AppendRow(table.Row{"First Name", customer.FirstName})
w.table.AppendRow(table.Row{"Last Name", customer.LastName})
w.table.AppendRow(table.Row{"Email Address", customer.EmailAddress})
Expand All @@ -21,4 +22,7 @@ func (w *Writer) GetCustomer(customer sbanken.Customer) {
}

w.table.Render()
w.table.ResetHeaders()
w.table.ResetRows()
w.table.ResetFooters()
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 29623dc

Please sign in to comment.