aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: f214a09de56d02cc2466f6fb64debf948e7fc6f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
)

func parseArgs() (string, error) {
	// We can only accept one argument
	if len(os.Args) != 2 {
		return "", fmt.Errorf("Usage: gh_authkey_checker <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.StatusNotFound {
		return "", fmt.Errorf("%s is an invalid user", username)
	}

	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("Expected http 200 but got %d instead", resp.StatusCode)
	}

	bodyBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	return string(bodyBytes), nil
}

func main() {
	username, err := parseArgs()
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Fetching keys for user %s", username)
	keys, err := fetchKeys(username)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Print(keys)
}