diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 59 |
1 files changed, 22 insertions, 37 deletions
@@ -8,59 +8,44 @@ import ( "os" ) -func parseArgs() string { - // We can only accept one argument - if len(os.Args) != 2 { - fmt.Println("Usage: gh_authkey_checker <username>") - os.Exit(1) - } - return os.Args[1] -} - -func fetchKeys(username string) string { - log.Printf("Fetching keys for user %s", username) - +func fetchKeys(username string) (string, error) { url := fmt.Sprintf("https://github.com/%s.keys", username) resp, err := http.Get(url) if err != nil { log.Fatal(err) } - defer resp.Body.Close() - if resp.StatusCode == http.StatusOK { - bodyBytes, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatalln(err) - } - bodyString := string(bodyBytes) - return bodyString + if resp.StatusCode == http.StatusNotFound { + return "", fmt.Errorf("%s is an invalid user", username) } - return "" -} -func checkUsername(username string) { - log.Printf("Checking for GitHub user %s", username) + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("Expected http 200 but got %d instead", resp.StatusCode) + } - url := fmt.Sprintf("https://github.com/%s.keys", username) - response, err := http.Get(url) + bodyBytes, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Fatalln(err) + return "", err } - if response.StatusCode != http.StatusOK { - log.Fatalf("%s is an invalid user", username) + return string(bodyBytes), nil +} + +func main() { + // Ensure we have the correct number of arguments + if len(os.Args) != 2 { + fmt.Println("Usage: gh_authkey_checker <username>") + os.Exit(1) } - log.Printf("Found valid user %s", username) + username := os.Args[1] - return -} + log.Printf("Fetching keys for user %s", username) + keys, err := fetchKeys(username) + if err != nil { + log.Fatal(err) + } -func main() { - username := parseArgs() - checkUsername(username) - keys := fetchKeys(username) fmt.Print(keys) - return } |