Skip to content

Commit

Permalink
Pass more than one FII on cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dude333 committed Apr 28, 2021
1 parent 38c920f commit d13fdea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
22 changes: 7 additions & 15 deletions cmd/rapina/fii_dividends.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ Distributed under the MIT License.
package main

import (
"fmt"
"log"
"os"
"strings"

"github.com/dude333/rapina/reports"
Expand All @@ -29,18 +27,10 @@ var fiiDividendsCmd = &cobra.Command{
n = 1
}

code := args[0]

if len(code) != 6 {
fmt.Println("[x] Código inválido:", code)
fmt.Println("[i] Padrão experado: ABCD11")
os.Exit(1)
}

// FII code ('ABCD11')
if err := FIIDividends(args[0], n); err != nil {
if err := FIIDividends(args, n); err != nil {
log.Println(err)
}

},
}

Expand All @@ -52,8 +42,10 @@ func init() {
// FIIDividends prints the dividends from 'code' for 'n' months,
// starting from latest.
//
func FIIDividends(code string, n int) error {
code = strings.ToUpper(code)
func FIIDividends(codes []string, n int) error {
for i := 0; i < len(codes); i++ {
codes[i] = strings.ToUpper(codes[i])
}

db, err := openDatabase()
if err != nil {
Expand All @@ -64,7 +56,7 @@ func FIIDividends(code string, n int) error {
if err != nil {
return err
}
err = r.Dividends(code, n)
err = r.Dividends(codes, n)
if err != nil {
return err
}
Expand Down
26 changes: 23 additions & 3 deletions reports/reports_fii.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import (
"github.com/dude333/rapina/parsers"
)

var line = strings.Repeat("-", 55)

type FIITerminalReport struct {
fetchFII *fetch.FII
fetchStock *fetch.StockFetch
log *Logger
}

func NewFIITerminalReport(db *sql.DB, apiKey string) (*FIITerminalReport, error) {
Expand All @@ -25,17 +28,35 @@ func NewFIITerminalReport(db *sql.DB, apiKey string) (*FIITerminalReport, error)
return &FIITerminalReport{
fetchFII: fetchFII,
fetchStock: fetchStock,
log: log,
}, nil
}

func (t FIITerminalReport) Dividends(code string, n int) error {
func (t FIITerminalReport) Dividends(codes []string, n int) error {

for _, code := range codes {
if len(code) != 6 {
t.log.Error("Código inválido: %s", code)
t.log.Info("Padrão experado: ABCD11")
continue
}

err := t.PrintDividends(code, n)
if err != nil {
t.log.Error("%s", err)
}
}
fmt.Println(line)

return nil
}

func (t FIITerminalReport) PrintDividends(code string, n int) error {
dividends, err := t.fetchFII.Dividends(code, n)
if err != nil {
return err
}

line := strings.Repeat("-", 55)
fmt.Println(line)
fmt.Println(code)
fmt.Println(line)
Expand All @@ -50,6 +71,5 @@ func (t FIITerminalReport) Dividends(code string, n int) error {
}
}

fmt.Println(line)
return nil
}

0 comments on commit d13fdea

Please sign in to comment.