Skip to content

Commit

Permalink
Fix linter issues
Browse files Browse the repository at this point in the history
Using golangci-lint
  • Loading branch information
dude333 committed Mar 22, 2021
1 parent 89775bd commit 40dfa8b
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 162 deletions.
20 changes: 10 additions & 10 deletions cli/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ func init() {
listCmd.Flags().Float32VarP(&netProfitRate, "lucroLiquido", "l", -0.8, "Lista empresas com lucros lucros positivos e com a taxa de crescimento definida")

listCmd.Run = func(cmd *cobra.Command, args []string) {
var err error

if listCmd.Flags().NFlag() == 0 {
listCmd.Help()
_ = listCmd.Help()
return
}

if listCompanies {
rapina.ListCompanies()
}
if sector != "" {
rapina.ListSector(sector, yamlFile)
err = rapina.ListCompanies()
} else if sector != "" {
err = rapina.ListSector(sector, yamlFile)
} else if listCmd.Flags().Changed("lucroLiquido") {
err = rapina.ListCompaniesProfits(netProfitRate)
}
if listCmd.Flags().Changed("lucroLiquido") {
err := rapina.ListCompaniesProfits(netProfitRate)
if err != nil {
fmt.Println("[x]", err)
}
if err != nil {
fmt.Println("[x]", err)
}
}

Expand Down
11 changes: 5 additions & 6 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func FetchCVM() error {
type fn func(*sql.DB, int) error

// try to run the function 'f' 'n' times, in case there are network errors.
func try(f fn, db *sql.DB, errMsg string, now, limit, n int) error {
func try(f fn, db *sql.DB, errMsg string, now, limit, n int) {
tries := n
var err error

Expand All @@ -86,8 +86,6 @@ func try(f fn, db *sql.DB, errMsg string, now, limit, n int) error {
tries = n
}
}

return err
}

// processAnnualReport will get data from .zip files downloaded
Expand Down Expand Up @@ -247,9 +245,10 @@ func (wc WriteCounter) printProgress() {
func downloadFile(url, filepath string) (err error) {
// Create dir if necessary
basepath := path.Dir(filepath)
os.MkdirAll(basepath, os.ModePerm)
//
err = nil
if err = os.MkdirAll(basepath, os.ModePerm); err != nil {
return err
}

// Create the file
out, err := os.Create(filepath)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion parsers/companies.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func loadCompanies(db *sql.DB) (map[string]company, error) {

defer rows.Close()
for rows.Next() {
rows.Scan(&id, &cnpj, &name)
err := rows.Scan(&id, &cnpj, &name)
if err != nil && err != sql.ErrNoRows {
return nil, err
}
companies[cnpj] = company{id, name}
}

Expand Down
8 changes: 5 additions & 3 deletions parsers/financial.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ func ImportCsv(db *sql.DB, dataType string, file string) (err error) {
if v > 0 {
fmt.Printf("[i] Apagando tabela %s versão %d (versão atual: %d)\n", table, v, currentDbVersion)
}
wipeDB(db, t)
if err := wipeDB(db, t); err != nil {
return err
}
}
if err = createTable(db, t); err != nil {
if err := createTable(db, t); err != nil {
return err
}

Expand Down Expand Up @@ -192,7 +194,7 @@ func populateTable(db *sql.DB, dataType, file string) (int, error) {
return count, errors.Wrapf(err, "erro ao ler arquivo %s", file)
}

saveCompanies(db, companies)
err = saveCompanies(db, companies)

return count, err
}
Expand Down
3 changes: 1 addition & 2 deletions parsers/financial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ func BenchmarkPrepareFields(b *testing.B) {
companies := make(map[string]company)
companies["54321"] = company{1, "A"}

h := make(map[string]int)
h = map[string]int{"x": 0, "y": 1, "DT_FIM_EXERC": 2, "CNPJ_CIA": 3}
h := map[string]int{"x": 0, "y": 1, "DT_FIM_EXERC": 2, "CNPJ_CIA": 3}

f := []string{"X", "Y", "2020-02-25", "54321"}

Expand Down
5 changes: 1 addition & 4 deletions parsers/fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import (
// Diacritics are removed from 'src' and 'list'.
//
func FuzzyMatch(src string, list []string, distance int) bool {
if FuzzyFind(src, list, distance) != "" {
return true
}
return false
return FuzzyFind(src, list, distance) != ""
}

//
Expand Down
4 changes: 3 additions & 1 deletion parsers/md5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ func TestIsNewFile(t *testing.T) {
return
}

createTable(db, "MD5")
if err := createTable(db, "MD5"); err != nil {
t.Errorf("could not create table: %v", err)
}

file := "../cli/.data/bpa_cia_aberta_con_2017.csv"
isNew, err := isNewFile(db, file)
Expand Down
12 changes: 7 additions & 5 deletions parsers/sectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func SectorsToYaml(yamlFile string) (err error) {
lastSub = subsectors[i]
fmt.Fprintln(w, " - Segmento:", elem.Text)
fmt.Fprintln(w, " Empresas:")
companies(w, "http://bvmf.bmfbovespa.com.br/cias-listadas/empresas-listadas/"+elem.Attr("href"))
_ = companies(w, "http://bvmf.bmfbovespa.com.br/cias-listadas/empresas-listadas/"+elem.Attr("href"))
}

fmt.Printf("\r[%s]", progress[p%6])
Expand All @@ -87,7 +87,7 @@ func SectorsToYaml(yamlFile string) (err error) {
fmt.Print("[ ] Lendo informações do site da B3")
fmt.Fprintln(w, "Setores:")

c.Visit("http://bvmf.bmfbovespa.com.br/cias-listadas/empresas-listadas/BuscaEmpresaListada.aspx?opcao=1&indiceAba=1&Idioma=pt-br")
err = c.Visit("http://bvmf.bmfbovespa.com.br/cias-listadas/empresas-listadas/BuscaEmpresaListada.aspx?opcao=1&indiceAba=1&Idioma=pt-br")

fmt.Println()
w.Flush()
Expand All @@ -98,7 +98,7 @@ func SectorsToYaml(yamlFile string) (err error) {
//
// companies lists all companies in the same sector/subsector/segment
//
func companies(w *bufio.Writer, url string) {
func companies(w *bufio.Writer, url string) error {
c := colly.NewCollector(
// Restrict crawling to specific domains
// colly.AllowedDomains("bvmf.bmfbovespa.com.br"),
Expand All @@ -122,7 +122,7 @@ func companies(w *bufio.Writer, url string) {

})

c.Visit(url)
return c.Visit(url)
}

//
Expand Down Expand Up @@ -177,7 +177,9 @@ func FromSector(company, yamlFile string) (companies []string, sectorName string
}

s := S{}
yaml.Unmarshal(y, &s)
if err := yaml.Unmarshal(y, &s); err != nil {
return nil, "", err
}

for _, sector := range s.Sectors {
for _, subsector := range sector.Subsectors {
Expand Down
2 changes: 1 addition & 1 deletion parsers/sectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ func createYaml(filename string) {
- GRENDENE S.A.
- VULCABRAS/AZALEIA S.A.`)

ioutil.WriteFile(filename, yaml, 0644)
_ = ioutil.WriteFile(filename, yaml, 0644)
}
25 changes: 2 additions & 23 deletions parsers/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package parsers

import (
"hash/fnv"
"strconv"
"time"
"unicode"

"github.com/pkg/errors"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
Expand All @@ -28,27 +26,8 @@ func Hash(s string) uint32 {
// RemoveDiacritics transforms, for example, "žůžo" into "zuzo"
//
func RemoveDiacritics(original string) (result string) {
isMn := func(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}

t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, _ = transform.String(t, original)

return
}

//
// yearRange transforms a year string (e.g., "2020") in two int64
// containing the 1st and last day of that year in Unix timestamp.
//
func yearRange(year string) (int64, int64, error) {
y, err := strconv.Atoi(year)
if err != nil {
return 0, 0, errors.Wrapf(err, "ano incorreto: %s.", year)
}
firstday := time.Date(y, 1, 1, 0, 0, 0, 0, time.Local)
lastday := firstday.AddDate(1, 0, 0).Add(time.Nanosecond * -1)

return firstday.Unix(), lastday.Unix(), nil
}
Loading

0 comments on commit 40dfa8b

Please sign in to comment.