-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(matcher): parse through status code as argument
prase and match functions
- Loading branch information
1 parent
1ab304d
commit b3158a4
Showing
2 changed files
with
54 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,52 @@ | ||
package matcher | ||
|
||
import "fmt" | ||
import ( | ||
"fmt" | ||
"strings" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func MatchParser(matchType string) { | ||
fmt.Println("matching option:", matchType) | ||
func matchStatusCode(url string, matchCodes []int) (bool, string) { | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return false, "" | ||
} | ||
defer resp.Body.Close() | ||
|
||
for _, code := range matchCodes { | ||
if resp.StatusCode == code { | ||
return true, fmt.Sprintf("Status Code: %d", code) | ||
} | ||
} | ||
return false, fmt.Sprintf("Status Code: %d", resp.StatusCode) | ||
} | ||
|
||
func parseStatusCodes(statusCodeList string) ([]int, error) { | ||
codeStrs := strings.Split(statusCodeList, ",") | ||
var codes []int | ||
for _, codeStr := range codeStrs { | ||
var code int | ||
if _, err := fmt.Sscanf(codeStr, "%d", &code); err != nil { | ||
return nil, err | ||
} | ||
if code < 600 && code >= 100 { | ||
codes = append(codes, code) | ||
} else { | ||
fmt.Printf("[WARN] `%d` not considered, invalid status code.", code) | ||
} | ||
} | ||
if len(codes) == 1 && codes[0] == 0 { | ||
return nil, fmt.Errorf("no valid status code given") | ||
} | ||
return codes, nil | ||
} | ||
|
||
func MatchParser(statuses string) { | ||
log.Println("Matching using the following status codes:", statuses) | ||
matchCodes, err := parseStatusCodes(statuses) | ||
if err != nil { | ||
log.Fatal("Error parsing status codes:", err) | ||
} | ||
log.Println("Matching status codes:", matchCodes) | ||
} |