-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrello.go
204 lines (160 loc) · 3.89 KB
/
trello.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"fmt"
"os"
"time"
trello "github.com/adlio/trello"
"github.com/briandowns/spinner"
hierr "github.com/reconquest/hierr-go"
)
var (
s = spinner.New(spinner.CharSets[35], 100*time.Millisecond)
)
func selectID(max int, message string) int {
var ID int
for {
fmt.Fprintf(os.Stderr, "\n:: Enter %s ID? [%d]: ", message, ID)
fmt.Scanln(&ID)
if ID <= max {
break
}
}
return ID
}
func selectVar(value, message string) string {
for {
fmt.Fprintf(os.Stderr, "\n:: Enter %s string? [%s]: ", message, value)
fmt.Scanln(&value)
if len(value) > 0 {
break
}
}
return value
}
func printBoardLine() {
fmt.Printf(
"\n|%-3s+%-20s+%-40s|",
"---",
"--------------------",
"----------------------------------------",
)
}
func printListLine() {
fmt.Printf(
"\n|%-3s+%-61s|",
"---",
"-------------------------------------------------------------",
)
}
func handleSetup(path string, config *Config) error {
config.Trello.AppKey = selectVar(config.Trello.AppKey, "AppKey")
config.Trello.Token = selectVar(config.Trello.Token, "Token")
client := trello.NewClient(config.Trello.AppKey, config.Trello.Token)
s.Start()
member, err := client.GetToken(config.Trello.Token, trello.Defaults())
if err != nil {
return hierr.Errorf(err, "can't get author")
}
config.Trello.Member = member.IDMember
s.Stop()
config.Trello.Board, err = handleBoard(client, config)
if err != nil {
return err
}
config.Trello.List, err = handleList(client, config)
if err != nil {
return err
}
err = saveConfig(path, config)
if err != nil {
return err
}
return nil
}
func handleBoard(
client *trello.Client,
config *Config,
) (string, error) {
s.Start()
myTokenID, err := client.GetToken(config.Trello.Token, trello.Defaults())
if err != nil {
return "", hierr.Errorf(err, "can't get token ID")
}
member, err := client.GetMember(myTokenID.IDMember, trello.Defaults())
if err != nil {
return "", hierr.Errorf(err, "can't get member")
}
boards, err := member.GetBoards(trello.Defaults())
if err != nil {
return "", hierr.Errorf(err, "can't get boards")
}
s.Stop()
printBoardLine()
fmt.Printf("\n|%-3s|%-20s|%-40s|", "ID", "Board", "Team")
printBoardLine()
for boardID, board := range boards {
team := ""
if board.IdOrganization != "" {
org, err := client.GetOrganization(
board.IdOrganization,
trello.Defaults(),
)
if err != nil {
return "", hierr.Errorf(err, "can't get organization")
}
team = fmt.Sprintf("%s (%s)", org.DisplayName, org.Name)
}
fmt.Printf("\n|%-3d|%-20s|%-40s|", boardID, board.Name, team)
}
printBoardLine()
ID := selectID(len(boards), "board")
return boards[ID].ID, nil
}
func handleList(
client *trello.Client,
config *Config,
) (string, error) {
s.Start()
board, err := client.GetBoard(config.Trello.Board, trello.Defaults())
if err != nil {
return "", hierr.Errorf(err, "can't get your board")
}
lists, err := board.GetLists(trello.Defaults())
if err != nil {
return "", hierr.Errorf(err, "can't get lists")
}
s.Stop()
printListLine()
fmt.Printf("\n|%-3s|%-61s|", "ID", "List")
printListLine()
for listID, list := range lists {
fmt.Printf("\n|%-3d|%-61s|", listID, list.Name)
}
printListLine()
ID := selectID(len(lists), "list")
return lists[ID].ID, nil
}
func handleAdd(
config *Config,
args map[string]interface{},
) error {
if len(config.Trello.List) == 0 {
return fmt.Errorf("first run -S and setup your config")
}
client := trello.NewClient(config.Trello.AppKey, config.Trello.Token)
s.Start()
name, _ := args["<name>"].(string)
description, _ := args["<description>"].(string)
card := trello.Card{
Name: name,
Desc: description,
IDList: config.Trello.List,
IDMembers: []string{config.Trello.Member},
}
err = client.CreateCard(&card, trello.Defaults())
if err != nil {
return hierr.Errorf(err, "can't add card to list")
}
s.Stop()
return nil
}