Skip to content

Commit

Permalink
Refactoring accomplished
Browse files Browse the repository at this point in the history
  • Loading branch information
jessp01 committed Jul 30, 2023
1 parent 04af261 commit 2c67bce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
20 changes: 5 additions & 15 deletions cmd/super-zaje/super-zaje.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -37,24 +36,15 @@ func main() {
}

var filename string
var data []byte
var resp *http.Response

if fi.Mode()&os.ModeNamedPipe == 0 {
if c.NArg() < 1 {
return errors.New("no input file provided. `zaje` needs a file or data from STDIN")
return errors.New("no input file provided. " + app.Name + " needs a file or data from STDIN")
}
filename = c.Args().Get(0)
httpRegex := regexp.MustCompile("^http(s)?://")
if httpRegex.Match([]byte(filename)) {
resp, err = http.Get(filename)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
} else {
data, _ = ioutil.ReadFile(filename)
data, err := zaje.ReadDataFromFile(filename)
if err != nil {
log.Fatal(err)
}

mimeType := http.DetectContentType(data)
Expand Down Expand Up @@ -92,7 +82,7 @@ func main() {
}

if err := scanner.Err(); err != nil {
return err
log.Fatal(err)
}
// read everything and process
} else {
Expand Down
23 changes: 5 additions & 18 deletions cmd/zaje.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import (
"bufio"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"

// "reflect"

Expand All @@ -29,25 +26,15 @@ func main() {
}

var filename string
var data []byte
var resp *http.Response

if fi.Mode()&os.ModeNamedPipe == 0 {
if c.NArg() < 1 {
return errors.New("no input file provided. `zaje` needs a file or data from STDIN")
return errors.New("no input file provided. " + app.Name + " needs a file or data from STDIN")
}
filename = c.Args().Get(0)
httpRegex := regexp.MustCompile("^http(s)?://")
if httpRegex.Match([]byte(filename)) {
resp, err = http.Get(filename)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
// get the base URL so we can adjust relative links and images
} else {
data, _ = ioutil.ReadFile(filename)
data, err := zaje.ReadDataFromFile(filename)
if err != nil {
log.Fatal(err)
}
zaje.HandleData(filename, data)
} else {
Expand All @@ -61,7 +48,7 @@ func main() {
}

if err := scanner.Err(); err != nil {
return err
log.Fatal(err)
}
// read everything and process
} else {
Expand Down
20 changes: 20 additions & 0 deletions common_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"net/http"
"os"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -293,3 +294,22 @@ func DownloadFile(url, fileName string) error {

return nil
}

// ReadDataFromFile reads from local file or a remote HTTP(s) URL and returns data
func ReadDataFromFile(filename string) ([]byte, error) {
var data []byte
var resp *http.Response
var err error
httpRegex := regexp.MustCompile("^http(s)?://")
if httpRegex.Match([]byte(filename)) {
resp, err = http.Get(filename)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
} else {
data, _ = ioutil.ReadFile(filename)
}
return data, err
}

0 comments on commit 2c67bce

Please sign in to comment.