Skip to content

Commit

Permalink
prepared utils go file
Browse files Browse the repository at this point in the history
  • Loading branch information
goldfix committed May 12, 2019
1 parent 10c2b7c commit fa9410b
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ tasks:
go-test:
desc: Run all tests.
cmds:
- go test ./cmd/... -v
- go test ./... -v

64 changes: 0 additions & 64 deletions cmd/pigHosts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"reflect"
"strings"

"net/http"

"github.com/docopt/docopt-go"
)
Expand All @@ -27,63 +23,3 @@ Options:
fmt.Printf("%v", reflect.TypeOf(arguments["FILE"]))

}

// removeLocalHost
func removeLocalHost(s string) (string, error) {
s = strings.ReplaceAll(s, "127.0.0.1", "")
s = strings.ReplaceAll(s, "0.0.0.0", "")
s = strings.TrimSpace(s)
return s, nil
}

// removeComments
func removeComments(s string) (string, error) {
pos := strings.Index(s, "#")
if pos > -1 {
s = s[0:pos]
}
s = strings.TrimSpace(s)
return s, nil
}

// getRemoteList
func getRemoteList(url string) ([]string, error) {
resp, err := http.Get(url)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
err = fmt.Errorf("Status different 200 (%s, %d)", resp.Status, resp.StatusCode)
return nil, err
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
return nil, err
}

f := func(c rune) bool {
return c == '\n'
}

r := strings.FieldsFunc(strings.ReplaceAll(string(b), "\r\n", "\n"), f)
return r, nil
}

func prepareHostsList(urls []string) ([]string, error) {
hosts := make([]string, 0)
for i := range urls {
h, err := getRemoteList(urls[i])
if err != nil {
fmt.Println(err.Error())
return nil, err
}
hosts = append(hosts, h...)
}

return hosts, nil
}
113 changes: 113 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package pighosts

import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)

// NON_ROUTABLE 0.0.0.0
var NON_ROUTABLE string = "0.0.0.0"

// LOCALHOST 127.0.0.1
var LOCALHOST string = "127.0.0.1"

// removeLocalHost
func removeLocalHost(s string) (string, error) {
s = strings.ReplaceAll(s, "127.0.0.1", "")
s = strings.ReplaceAll(s, "0.0.0.0", "")
s = strings.TrimSpace(s)
return s, nil
}

// removeComments
func removeComments(s string) (string, error) {
pos := strings.Index(s, "#")
if pos > -1 {
s = s[0:pos]
}
s = strings.TrimSpace(s)
return s, nil
}

// getRemoteList
func getRemoteList(url string) ([]string, error) {
resp, err := http.Get(url)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
err = fmt.Errorf("Status different 200 (%s, %d)", resp.Status, resp.StatusCode)
return nil, err
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
return nil, err
}

f := func(c rune) bool {
return c == '\n'
}

r := strings.FieldsFunc(strings.ReplaceAll(string(b), "\r\n", "\n"), f)
return r, nil
}

// prepareHostsList
func prepareHostsList(urls []string) (map[string]int, error) {
hosts := make(map[string]int, 0)

for u := range urls {
lstHost, err := getRemoteList(urls[u])
if chkErr(err) {
return nil, err
}

for l := range lstHost {
hst := lstHost[l]
hst, err = removeComments(hst)
if chkErr(err) {
return nil, err
}
hst, err = removeLocalHost(hst)
if chkErr(err) {
return nil, err
}
}
}
return hosts, nil
}

//prepareHostFile
func prepareHostFile(hosts map[string]int) ([]string, error) {
result := make([]string, 0)
for k := range hosts {
if k != "" {
result = append(result, k)
}

}

return nil, nil
}

//writeHostFile
func writeHostFile() error {

return nil
}

//chkErr
func chkErr(err error) bool {
if err != nil {
fmt.Println(err.Error())
return true
}
return false
}
11 changes: 7 additions & 4 deletions cmd/pigHosts/main_test.go → utils_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pighosts

import (
"reflect"
Expand Down Expand Up @@ -99,11 +99,13 @@ func Test_prepareHostsList(t *testing.T) {
tests := []struct {
name string
args args
want []string
want map[string]int
wantErr bool
}{
{"Test_prepareHostsList_1", args{urls: []string{"https://drive.google.com/uc?authuser=0&id=1BfGJJLtimhoOi9Sm3jYLF6d8XtYBJ5KY&export=download"}},
[]string{"127.0.0.1 localhost", "127.0.0.1 localhost.localdomain", "# TEST # ", "127.0.0.1 local", "255.255.255.255 broadcasthost"}, false},
{"Test_prepareHostsList_1", args{urls: []string{"https://drive.google.com/uc?authuser=0&id=1BfGJJLtimhoOi9Sm3jYLF6d8XtYBJ5KY&export=download",
"https://drive.google.com/uc?authuser=0&id=1-QRZf_ymrWFZ4XgmXTZJrkhqzhdJMphB&export=download"}},
map[string]int{"localhost": 2, "localhost.localdomain": 2, "local": 3, "255.255.255.255 broadcasthost": 3},
false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -116,5 +118,6 @@ func Test_prepareHostsList(t *testing.T) {
t.Errorf("prepareHostsList() = %v, want %v", got, tt.want)
}
})

}
}

0 comments on commit fa9410b

Please sign in to comment.