/
main.go
47 lines (35 loc) · 1.27 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
package main
import (
"flag"
"fmt"
"log"
"github.com/zpatrick/rclient"
)
type repository struct {
Name string `json:"name,omitempty"`
}
func main() {
username := flag.String("u", "", "username for your github account")
password := flag.String("p", "", "password for your github account")
flag.Parse()
if *username == "" || *password == "" {
log.Fatal("username and password are required")
}
client := rclient.NewRestClient("https://api.github.com")
var repo repository
request := repository{Name: "my_sample_repo"}
// add auth to the request
if err := client.Post("/user/repos", request, &repo, rclient.BasicAuth(*username, *password)); err != nil {
log.Fatalf("Failed to create repository: %v", err)
}
fmt.Printf("Successfully created repository %s\n", repo.Name)
fmt.Printf("Press the Enter Key to delete this repository: ")
fmt.Scanln()
// also, you can set basic auth for each request the client makes
client = rclient.NewRestClient("https://api.github.com", rclient.RequestOptions(rclient.BasicAuth(*username, *password)))
path := fmt.Sprintf("/repos/%s/%s", *username, repo.Name)
if err := client.Delete(path, nil, nil); err != nil {
log.Fatalf("Failed to delete repository: %v", err)
}
fmt.Printf("Successfully deleted repository %s\n", repo.Name)
}