-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpcli.go
198 lines (178 loc) · 4.69 KB
/
tpcli.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
package main
import (
"fmt"
"os"
"time"
"github.com/bndr/gopencils"
"github.com/briandowns/spinner"
"github.com/codegangsta/cli"
"github.com/kennygrant/sanitize"
"github.com/mgutz/ansi"
)
// Struct to hold target proces entity data
type entityStruct struct {
Id int
Name string
Description string
StartDate string
EndDate string
CreateDate string
ResourceType string
Effort float32
Iteration struct {
Id int
Name string
}
Release struct {
Id int
Name string
}
Team struct {
Id int
Name string
}
EntityState struct {
Id int
Name string
}
}
// Ansi console decorators
var errorDecorator = ansi.ColorFunc("white:red")
var worningDecorator = ansi.ColorFunc("black:yellow")
var boldDecorator = ansi.ColorFunc("white+b")
var preloader = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
// Get entity data from target process RESTful service
func getEntity(id string, username string, password string, url string, entityType string, resp *entityStruct) (*entityStruct, error) {
auth := gopencils.BasicAuth{Username: username, Password: password}
api := gopencils.Api(url, &auth)
querystring := map[string]string{"format": "json"}
preloader.Prefix = "Fetching data:"
preloader.Start()
_, err := api.Res(entityType, resp).Id(id).Get(querystring)
preloader.Stop()
return resp, err
}
// Check if username and password flags are provided or env variables exist
func checkGlobalFlags(c *cli.Context) {
if c.GlobalString("username") == "" || c.GlobalString("password") == "" || c.GlobalString("url") == "" {
fmt.Println(errorDecorator("Please provide username, password and url flags!"))
os.Exit(0)
}
}
func displayEntity(entity *entityStruct, err error, template string) {
// If there is error in response display error
if err != nil {
fmt.Println(errorDecorator(err.Error()))
os.Exit(0)
}
// If there is no entity data display No entity message
if entity.Id == 0 {
fmt.Println(worningDecorator("Entity not found!"))
os.Exit(0)
}
// Display appropriate entity template
switch template {
case "s":
fmt.Println(boldDecorator(entity.Name))
fmt.Println(entity.ResourceType, " | ", entity.Effort, "p", " | ", entity.EntityState.Name)
case "m":
{
fmt.Println(boldDecorator(entity.Name))
fmt.Println(entity.ResourceType, " | ", entity.Effort, "p", " | ", entity.EntityState.Name)
}
case "l":
{
fmt.Println(boldDecorator(entity.Name), " | ", entity.Iteration.Name)
fmt.Println(entity.ResourceType, " | ", entity.Effort, "p", " | ", entity.EntityState.Name)
fmt.Println(sanitize.HTML(entity.Description))
}
}
}
func main() {
// Define cli app
app := cli.NewApp()
app.Name = "Target Process cli tool"
app.Author = "Ivan Padavic @ipadavic"
app.Version = "0.1.0"
// Define app flags
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "username, u",
Usage: "Username for target process Basic Auth",
EnvVar: "TPCLI_USERNAME",
},
cli.StringFlag{
Name: "password, p",
Usage: "Password for target process Basic Auth",
EnvVar: "TPCLI_PASSWORD",
},
cli.StringFlag{
Name: "url",
Usage: "Base url for your target process app rest api (custom or tpondemand)",
EnvVar: "TPCLI_URL",
},
}
// Define response holder
response := &entityStruct{}
commandsTemplateFlag := []cli.Flag{
cli.StringFlag{
Name: "template, t",
Usage: "Template for displaying entity data [s, m, l]",
Value: "s",
},
}
app.Commands = []cli.Command{
{
Name: "bug",
Aliases: []string{"b"},
Usage: "Get bug information",
Flags: commandsTemplateFlag,
Action: func(c *cli.Context) {
checkGlobalFlags(c)
resp, err := getEntity(
c.Args().First(),
c.GlobalString("username"),
c.GlobalString("password"),
c.GlobalString("url"),
"Bugs",
response)
displayEntity(resp, err, c.String("template"))
},
},
{
Name: "story",
Aliases: []string{"s"},
Usage: "Get user story information",
Flags: commandsTemplateFlag,
Action: func(c *cli.Context) {
checkGlobalFlags(c)
resp, err := getEntity(
c.Args().First(),
c.GlobalString("username"),
c.GlobalString("password"),
c.GlobalString("url"),
"UserStories",
response)
displayEntity(resp, err, c.String("template"))
},
},
{
Name: "task",
Aliases: []string{"t"},
Usage: "Get task information",
Flags: commandsTemplateFlag,
Action: func(c *cli.Context) {
checkGlobalFlags(c)
resp, err := getEntity(
c.Args().First(),
c.GlobalString("username"),
c.GlobalString("password"),
c.GlobalString("url"),
"Tasks",
response)
displayEntity(resp, err, c.String("template"))
},
},
}
app.Run(os.Args)
}