Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Mar 9, 2019
1 parent be281e1 commit 429282e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
23 changes: 11 additions & 12 deletions internal/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bufio"
"compress/gzip"
"io"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -33,7 +32,7 @@ func findLocalFiles(urlStr string) []string {
})

if err != nil {
log.Fatal(err)
abort(err)
}

return files
Expand Down Expand Up @@ -80,7 +79,7 @@ func findFileMatches(filename string) ([][]string, int) {
} else {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
abort(err)
}

defer f.Close()
Expand All @@ -97,7 +96,7 @@ func processFile(file ReadSeekCloser, filename string) ([][]string, int) {
file.Read(head)
kind, err := filetype.Match(head)
if err != nil {
log.Fatal(err)
abort(err)
}
// fmt.Println(kind.MIME.Value)

Expand All @@ -114,12 +113,12 @@ func processFile(file ReadSeekCloser, filename string) ([][]string, int) {
} else if kind.MIME.Value == "application/pdf" {
pdfReader, err := pdf.NewPdfReader(file)
if err != nil {
log.Fatal(err)
abort(err)
}

numPages, err := pdfReader.GetNumPages()
if err != nil {
log.Fatal(err)
abort(err)
}

content := ""
Expand All @@ -129,12 +128,12 @@ func processFile(file ReadSeekCloser, filename string) ([][]string, int) {

page, err := pdfReader.GetPage(pageNum)
if err != nil {
log.Fatal(err)
abort(err)
}

contentStreams, err := page.GetContentStreams()
if err != nil {
log.Fatal(err)
abort(err)
}

// If the value is an array, the effect shall be as if all of the streams in the array were concatenated,
Expand All @@ -147,7 +146,7 @@ func processFile(file ReadSeekCloser, filename string) ([][]string, int) {
cstreamParser := pdfcontent.NewContentStreamParser(pageContentStr)
txt, err := cstreamParser.ExtractText()
if err != nil {
log.Fatal(err)
abort(err)
}
content += txt
}
Expand All @@ -158,7 +157,7 @@ func processFile(file ReadSeekCloser, filename string) ([][]string, int) {
// TODO make zip work with S3
reader, err := zip.OpenReader(filename)
if err != nil {
log.Fatal(err)
abort(err)
}
defer reader.Close()

Expand All @@ -169,7 +168,7 @@ func processFile(file ReadSeekCloser, filename string) ([][]string, int) {

fileReader, err := file.Open()
if err != nil {
log.Fatal(err)
abort(err)
}
defer fileReader.Close()

Expand All @@ -188,7 +187,7 @@ func processFile(file ReadSeekCloser, filename string) ([][]string, int) {
} else if kind.MIME.Value == "application/gzip" {
gz, err := gzip.NewReader(file)
if err != nil {
log.Fatal(err)
abort(err)
}

scanner := bufio.NewScanner(gz)
Expand Down
6 changes: 6 additions & 0 deletions internal/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"fmt"
"os"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -294,3 +295,8 @@ func showLowConfidenceMatchHelp(matchList []ruleMatch) {
fmt.Println("Also found " + pluralize(len(lowConfidenceMatches), "low confidence match") + ". Use --show-all to view them")
}
}

func abort(err error) {
fmt.Println(err)
os.Exit(1)
}
9 changes: 4 additions & 5 deletions internal/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package internal
import (
"bytes"
"io/ioutil"
"log"
"net/url"
"strings"

Expand All @@ -18,7 +17,7 @@ func findS3Files(urlStr string) []string {
if strings.HasSuffix(urlStr, "/") {
u, err1 := url.Parse(urlStr)
if err1 != nil {
log.Fatal(err1)
abort(err1)
}
bucket := u.Host
key := u.Path[1:]
Expand Down Expand Up @@ -52,7 +51,7 @@ func downloadS3File(filename string) ReadSeekCloser {

u, err := url.Parse(filename)
if err != nil {
log.Fatal(err)
abort(err)
}
bucket := u.Host
key := u.Path
Expand All @@ -65,12 +64,12 @@ func downloadS3File(filename string) ReadSeekCloser {
Key: aws.String(key),
})
if err != nil {
log.Fatal(err)
abort(err)
}

buff, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
abort(err)
}

return bytes.NewReader(buff)
Expand Down
15 changes: 7 additions & 8 deletions internal/sql_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package internal

import (
"fmt"
"log"

"github.com/jmoiron/sqlx"
"github.com/lib/pq"
Expand All @@ -19,15 +18,15 @@ type SqlAdapter struct {
func (a *SqlAdapter) Init(url string) {
u, err := dburl.Parse(url)
if err != nil {
log.Fatal(err)
abort(err)
}

db, err := sqlx.Connect(u.Driver, u.DSN)
if err != nil {
// TODO prompt for password if needed
// var input string
// fmt.Scanln(&input)
log.Fatal(err)
abort(err)
}
// defer db.Close()

Expand All @@ -50,7 +49,7 @@ func (a SqlAdapter) FetchTables() (tables []table) {

err := db.Select(&tables, query)
if err != nil {
log.Fatal(err)
abort(err)
}

return tables
Expand Down Expand Up @@ -83,13 +82,13 @@ func (a SqlAdapter) FetchTableData(table table, limit int) ([]string, [][]string
// run query on each table
rows, err := db.Query(sql)
if err != nil {
log.Fatal(err)
abort(err)
}

// read everything as string and discard empty strings
cols, err := rows.ColumnTypes()
if err != nil {
log.Fatal(err)
abort(err)
}

// map types
Expand Down Expand Up @@ -123,7 +122,7 @@ func (a SqlAdapter) FetchTableData(table table, limit int) ([]string, [][]string
for rows.Next() {
err = rows.Scan(dest...)
if err != nil {
log.Fatal(err)
abort(err)
}

for i, raw := range rawResult {
Expand Down Expand Up @@ -154,7 +153,7 @@ func tsmSystemRowsSupported(db *sqlx.DB) bool {
var count int
err := row.Scan(&count)
if err != nil {
log.Fatal(err)
abort(err)
}
return count > 0
}

0 comments on commit 429282e

Please sign in to comment.