-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
72 lines (52 loc) · 1.1 KB
/
main.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
fmt.Fprintln(os.Stderr, "No tokens detected. Hint: cat tokens.txt | circle-test")
os.Exit(1)
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
token := scanner.Text()
err := testCircleCIToken(token)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
}
}
func testCircleCIToken(token string) error {
fmt.Println("Testing token ->", token)
client := &http.Client{}
req, err := http.NewRequest("GET", "https://circleci.com/api/v1.1/me", nil)
if err != nil {
return err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Circle-Token", token)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var prettyJson bytes.Buffer
err = json.Indent(&prettyJson, body, "", "\t")
if err != nil {
return err
}
fmt.Println(string(prettyJson.Bytes()))
return nil
}