-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
177 lines (134 loc) · 3.62 KB
/
client.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package gong
import (
"encoding/json"
"fmt"
"github.com/segmentio/go-prompt"
"io/ioutil"
"os/user"
"path/filepath"
)
// Client : Public interface for the generic client
type Client interface {
GetAuthFields() map[string]bool
GetName() string
FormatField(fieldName string, value string) string
Authenticate(fields map[string]string) bool
Start(issueType string, issueID string) (branchName string, err error)
Browse(branchName string) (string, error)
Comment(branchName, comment string) error
PrepareCommitMessage(branchName, commitMessage string) string
Create() (string, error)
}
func Create(client Client) (string, error) {
return client.Create()
}
// PrepareCommitMessage : Prepares the commit message and returns a new commit message
func PrepareCommitMessage(client Client, branchName, commitMessage string) string {
return client.PrepareCommitMessage(branchName, commitMessage)
}
// Comment : Comment on an issue
func Comment(client Client, branchName, comment string) error {
return client.Comment(branchName, comment)
}
// Browse : Open a browser on the issue related to the branch
func Browse(client Client, branchName string) (string, error) {
return client.Browse(branchName)
}
// Start : Start working on an issue
func Start(client Client, issueType, issueID string) (string, error) {
return client.Start(issueType, issueID)
}
// NewClient : Return a new client that matches the name passed in
func NewClient(clientName string) (Client, error) {
if clientName == "jira" {
return NewJiraClient(), nil
}
if clientName == "pivotal" {
return NewPivotalClient(), nil
}
return nil, fmt.Errorf("Could not find client: %v", clientName)
}
// NewAuthenticatedClient : Return a new client authenticated
func NewAuthenticatedClient() (Client, error) {
fields, err := Load()
if err != nil {
return nil, err
}
client, err := NewClient(fields["client"])
if err != nil {
return nil, err
}
authenticated := client.Authenticate(fields)
if authenticated {
return client, nil
}
return nil, fmt.Errorf("Could not load authenticated client")
}
// Login : Logs the user into the specified client
func Login(client Client) (bool, error) {
clientName := client.GetName()
fields := map[string]string{
"client": clientName,
}
for k, v := range client.GetAuthFields() {
message := fmt.Sprintf("Please enter your %v %v", clientName, k)
promptValue := ""
if v {
promptValue = prompt.PasswordMasked(message)
} else {
promptValue = prompt.String(message)
}
fields[k] = client.FormatField(k, promptValue)
}
err := Save(fields)
if err != nil {
return false, err
}
fields, err = Load()
if err != nil {
return false, err
}
authenticated := client.Authenticate(fields)
if authenticated {
return true, nil
}
return false, fmt.Errorf("Cloud not login")
}
func getUserHomeOrDefault() string {
usr, err := user.Current()
if err != nil {
return "./"
}
return usr.HomeDir
}
func getFileLocation() string {
dir := getUserHomeOrDefault()
return filepath.Join(dir, ".gong.json")
}
// Load : Load the configuration from a file
func Load() (map[string]string, error) {
fileLocation := getFileLocation()
var c = map[string]string{}
file, err := ioutil.ReadFile(fileLocation)
if err != nil {
return nil, err
}
err = json.Unmarshal(file, &c)
if err != nil {
return nil, err
}
return c, nil
}
// Save : saves the configuration to a file
func Save(values map[string]string) error {
fileLocation := getFileLocation()
loginDetails, err := json.Marshal(values)
if err != nil {
return err
}
err = ioutil.WriteFile(fileLocation, loginDetails, 0644)
if err != nil {
return err
}
return nil
}