This repository was archived by the owner on Apr 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathme.go
More file actions
78 lines (67 loc) · 1.67 KB
/
me.go
File metadata and controls
78 lines (67 loc) · 1.67 KB
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
73
74
75
76
77
78
package trackerapi
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
u "os/user"
"github.com/GoBootcamp/clirescue/cmdutil"
"github.com/GoBootcamp/clirescue/user"
)
var (
URL string = "https://www.pivotaltracker.com/services/v5/me"
FileLocation string = homeDir() + "/.tracker"
currentUser *user.User = user.New()
Stdout *os.File = os.Stdout
)
func Me() {
setCredentials()
parse(makeRequest())
ioutil.WriteFile(FileLocation, []byte(currentUser.APIToken), 0644)
}
func makeRequest() []byte {
client := &http.Client{}
req, err := http.NewRequest("GET", URL, nil)
req.SetBasicAuth(currentUser.Username, currentUser.Password)
resp, err := client.Do(req)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Print(err)
}
fmt.Printf("\n****\nAPI response: \n%s\n", string(body))
return body
}
func parse(body []byte) {
var meResp = new(MeResponse)
err := json.Unmarshal(body, &meResp)
if err != nil {
fmt.Println("error:", err)
}
currentUser.APIToken = meResp.APIToken
}
func setCredentials() {
fmt.Fprint(Stdout, "Username: ")
var username = cmdutil.ReadLine()
cmdutil.Silence()
fmt.Fprint(Stdout, "Password: ")
var password = cmdutil.ReadLine()
currentUser.Login(username, password)
cmdutil.Unsilence()
}
func homeDir() string {
usr, _ := u.Current()
return usr.HomeDir
}
type MeResponse struct {
APIToken string `json:"api_token"`
Username string `json:"username"`
Name string `json:"name"`
Email string `json:"email"`
Initials string `json:"initials"`
Timezone struct {
Kind string `json:"kind"`
Offset string `json:"offset"`
OlsonName string `json:"olson_name"`
} `json:"time_zone"`
}