Skip to content

Commit

Permalink
Merge branch 'issue-14'
Browse files Browse the repository at this point in the history
  • Loading branch information
1jz committed Oct 7, 2020
2 parents b9f208b + 661b7b7 commit 0b4f5cb
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions wisa.go
Expand Up @@ -41,9 +41,10 @@ func removeDuplicate(urls []string) []string {
return result
}

func checkLink(wg *sync.WaitGroup, url string) (RequestResult, error) {
func checkLink(wg *sync.WaitGroup, url string) (RequestResult, int, error) {
var r RequestResult
var reqErr error = nil
var status int

// defered function is run when surrounding functions are completed
defer wg.Done()
Expand All @@ -64,15 +65,18 @@ func checkLink(wg *sync.WaitGroup, url string) (RequestResult, error) {
r = RequestResult{url, resp.StatusCode}
if !*jsonPtr {
if resp.StatusCode == 200 {
status = 0
color.New(color.FgGreen).Printf("[GOOD] [%d] %s\n", resp.StatusCode, url)
} else if resp.StatusCode == 400 || resp.StatusCode == 404 {
status = 3
color.New(color.FgRed).Printf("[BAD] [%d] %s\n", resp.StatusCode, url)
} else {
status = 3
color.New(color.FgGray).Printf("[UNKNOWN] [%d] %s\n", resp.StatusCode, url)
}
}
}
return r, reqErr
return r, status, reqErr
}

func main() {
Expand Down Expand Up @@ -132,20 +136,27 @@ func main() {
var wg sync.WaitGroup

// json output stuff
var mut sync.Mutex
var jsonSlice []RequestResult
var mut sync.Mutex
finalExit := 0

// check if urls found are alive
for _, url := range urls {
wg.Add(1)
go func(url string) {
res, err := checkLink(&wg, url)
res, status, err := checkLink(&wg, url)

if err == nil {
mut.Lock()
jsonSlice = append(jsonSlice, res)
mut.Unlock()
}

if status == 3 {
mut.Lock()
finalExit = status
mut.Unlock()
}
}(url)
}

Expand All @@ -162,4 +173,5 @@ func main() {
}

}
os.Exit(finalExit)
}

0 comments on commit 0b4f5cb

Please sign in to comment.