diff options
author | Alex Mayer <amayer5125@gmail.com> | 2019-06-19 23:59:52 -0400 |
---|---|---|
committer | Alex Mayer <amayer5125@gmail.com> | 2019-06-19 23:59:52 -0400 |
commit | 376f08237158c7ceab15cec4b9e8af5ca0d293a1 (patch) | |
tree | bf1e912be1462b519bb7aadf64abe474c2853096 /main.go | |
parent | a8bccea089c9bfb71e4f168b1341227d47f51d8f (diff) | |
download | gh_authkey_checker-376f08237158c7ceab15cec4b9e8af5ca0d293a1.tar gh_authkey_checker-376f08237158c7ceab15cec4b9e8af5ca0d293a1.tar.gz gh_authkey_checker-376f08237158c7ceab15cec4b9e8af5ca0d293a1.tar.bz2 gh_authkey_checker-376f08237158c7ceab15cec4b9e8af5ca0d293a1.tar.lz gh_authkey_checker-376f08237158c7ceab15cec4b9e8af5ca0d293a1.tar.xz gh_authkey_checker-376f08237158c7ceab15cec4b9e8af5ca0d293a1.tar.zst gh_authkey_checker-376f08237158c7ceab15cec4b9e8af5ca0d293a1.zip |
Return Errors From Functions And Log In Main
Clean up returns
Add empty line above each final return for consistency
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 64 |
1 files changed, 36 insertions, 28 deletions
@@ -8,59 +8,67 @@ import ( "os" ) -func parseArgs() string { +func parseArgs() (string, error) { // We can only accept one argument if len(os.Args) != 2 { - fmt.Println("Usage: gh_authkey_checker <username>") - os.Exit(1) + return "", fmt.Errorf("Usage: gh_authkey_checker <username>") } - return os.Args[1] -} -func fetchKeys(username string) string { - log.Printf("Fetching keys for user %s", username) + return os.Args[1], nil +} +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.StatusOK { + return "", fmt.Errorf("Expected http 200 but got %d instead", resp.StatusCode) } - return "" -} -func checkUsername(username string) { - log.Printf("Checking for GitHub user %s", username) + bodyBytes, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + + return string(bodyBytes), nil +} +func checkUsername(username string) error { url := fmt.Sprintf("https://github.com/%s.keys", username) response, err := http.Get(url) if err != nil { - log.Fatalln(err) + return err } if response.StatusCode != http.StatusOK { - log.Fatalf("%s is an invalid user", username) + return fmt.Errorf("%s is an invalid user", username) } - log.Printf("Found valid user %s", username) - - return + return nil } func main() { - username := parseArgs() - checkUsername(username) - keys := fetchKeys(username) + username, err := parseArgs() + if err != nil { + log.Fatal(err) + } + + log.Printf("Checking for GitHub user %s", username) + err = checkUsername(username) + if err != nil { + log.Fatal(err) + } + log.Printf("Found valid user %s", username) + + log.Printf("Fetching keys for user %s", username) + keys, err := fetchKeys(username) + if err != nil { + log.Fatal(err) + } + fmt.Print(keys) - return } |