Skip to content

Commit

Permalink
Added JSON output option
Browse files Browse the repository at this point in the history
  • Loading branch information
MSTEWARDSON committed Oct 6, 2020
1 parent 4f772d8 commit 7501097
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 769 deletions.
Binary file modified LinkStatus.exe
Binary file not shown.
80 changes: 64 additions & 16 deletions LinkStatus.go
Expand Up @@ -24,7 +24,7 @@ import (
Opens and reads the given file into a single string. This string is then
checked for url's via a regex pattern.
*/
func readFile(file string) {
func readFile(file string, choice bool) {
f, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
Expand All @@ -35,19 +35,25 @@ func readFile(file string) {
fmt.Println("File output")
var temp = "test"
var k = 0
if choice == true {
fmt.Print("[")
}
for i := range result {
if temp == result[i] {
//This is to ignore dupes
} else {
checkStatus(result[i], k)
checkStatus(result[i], k, choice)
k++
}
temp = result[i]
}
if choice == true {
fmt.Println(" ]")
}
}

//Does a GET request on the given string found within the file
func checkStatus(url string, i int) {
func checkStatus(url string, i int, choice bool) {
//Colours
c := color.New(color.FgCyan)
r := color.New(color.FgRed)
Expand All @@ -58,42 +64,84 @@ func checkStatus(url string, i int) {
Timeout: 7 * time.Second,
}
response, err := client.Get(url)
if err != nil {
r.Println(i, " -> [ERROR] ", "URL: ", url)

if choice == true {
fmt.Print(" { url: '")
if err != nil {
r.Print(url)
fmt.Print("': status '")
r.Print("Error")
} else {
if response.StatusCode >= 200 && response.StatusCode <= 299 {
g.Print(url)
fmt.Print("': status '")
g.Print(response.StatusCode)
} else if response.StatusCode == 400 {
r.Print(url)
fmt.Print("': status '")
r.Print(response.StatusCode)
} else if response.StatusCode == 404 {
r.Print(url)
fmt.Print("': status '")
r.Print(response.StatusCode)
} else {
c.Print(url)
fmt.Print("': status '")
c.Print(response.StatusCode)
}
defer response.Body.Close()
}
fmt.Print("' },")
} else {
if response.StatusCode >= 200 && response.StatusCode <= 299 {
g.Println(i, " -> [GOOD] ", response.StatusCode, "URL: ", url)
} else if response.StatusCode == 400 {
r.Println(i, " -> [BAD] ", response.StatusCode, "URL: ", url)
} else if response.StatusCode == 404 {
r.Println(i, " -> [BAD] ", response.StatusCode, "URL: ", url)
if err != nil {
r.Println(i, " -> [ERROR] ", "URL: ", url)
} else {
c.Println(i, " -> [UNKNOWN] URL: ", url)
if response.StatusCode >= 200 && response.StatusCode <= 299 {
g.Println(i, " -> [GOOD] ", response.StatusCode, "URL: ", url)
} else if response.StatusCode == 400 {
r.Println(i, " -> [BAD] ", response.StatusCode, "URL: ", url)
} else if response.StatusCode == 404 {
r.Println(i, " -> [BAD] ", response.StatusCode, "URL: ", url)
} else {
c.Println(i, " -> [UNKNOWN] URL: ", url)
}
defer response.Body.Close()
}
defer response.Body.Close()
}
}

//Setting up the optional version command
var version = flag.BoolP("version", "v", false, "prints out version info")

//JSON output command agr
var JSONout = flag.BoolP("json", "j", false, "prints link output in JSON format")

func main() {

var JSONchoice = false

flag.Parse()
if *version == true {
fmt.Println("LinkStatus version 0.1.4")
fmt.Println("LinkStatus version 0.1.5")
return
}
if *JSONout == true {
fmt.Println("JSON output selected")
JSONchoice = true
}
if len(os.Args) == 1 {
fmt.Println(`
Name: LinkStatus
Usage: go run LinkStatus.go filenames
Example: go run LinkStatus.go urls.txt
Version: go run LinkStatus.go -v or --version to check version.
JSON Format: go run LinkStatus.go -j or --json to output as JSON format
`)
os.Exit(-1)
os.Exit(0)
}
fmt.Println("Checking files ", os.Args[1:])
for _, file := range os.Args[1:] {
readFile(file)
readFile(file, JSONchoice)
}
os.Exit(0)
}

0 comments on commit 7501097

Please sign in to comment.