aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 32ddf6c4f9d03b49a77987f2caa46843fddee8d2 (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
package main

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

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() {
	// Ensure we have the correct number of arguments
	if len(os.Args) != 2 {
		fmt.Println("Usage: gh_authkey_checker <username>")
		os.Exit(1)
	}

	username := os.Args[1]

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

	fmt.Print(keys)
}