-
Notifications
You must be signed in to change notification settings - Fork 25
/
resources.go
201 lines (166 loc) · 5.33 KB
/
resources.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
package main
import (
"fmt"
"os"
"strconv"
"github.com/olekukonko/tablewriter"
"github.com/rainforestapp/rainforest-cli/rainforest"
"github.com/urfave/cli"
)
// printResourceTable uses olekukonko/tablewriter as a pretty printer
// for the tabular resources we get from the API and formatted using formatAsTable.
func printResourceTable(headers []string, rows [][]string) {
// Init tablewriter with out global var as a target
table := tablewriter.NewWriter(tablesOut)
// Prepare the tablewriter
table.SetHeader(headers)
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.AppendBulk(rows) // Add Bulk Data
// Render prints out the table to output specified during init.
table.Render()
}
// resourceAPI is part of the API connected to available resources
type resourceAPI interface {
GetFolders() ([]rainforest.Folder, error)
GetPlatforms() ([]rainforest.Platform, error)
GetSites() ([]rainforest.Site, error)
GetEnvironments() ([]rainforest.Environment, error)
GetFeatures() ([]rainforest.Feature, error)
GetRunGroups() ([]rainforest.RunGroup, error)
GetRunJunit(int) (*string, error)
}
// printFolders fetches and prints out the available folders from the API
func printFolders(api resourceAPI) error {
// Fetch the list of folders from the Rainforest
folders, err := api.GetFolders()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
rows := make([][]string, len(folders))
for i, folder := range folders {
rows[i] = []string{strconv.Itoa(folder.ID), folder.Title}
}
printResourceTable([]string{"Folder ID", "Folder Name"}, rows)
return nil
}
// printPlatforms fetches and prints out the platforms available to the client
func printPlatforms(api resourceAPI) error {
// Fetch the list of platforms from the Rainforest
platforms, err := api.GetPlatforms()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
rows := make([][]string, len(platforms))
for i, platform := range platforms {
rows[i] = []string{platform.Name, platform.Description}
}
printResourceTable([]string{"Platform ID", "Platform Name"}, rows)
return nil
}
// printSites fetches and prints out the defined sites
func printSites(api resourceAPI) error {
// Fetch the list of sites from the Rainforest
sites, err := api.GetSites()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
humanizedSiteCategories := map[string]string{
"device_farm": "Device Farm",
"android": "Android",
"ios": "iOS",
"site": "Site",
}
rows := make([][]string, len(sites))
for i, site := range sites {
category, ok := humanizedSiteCategories[site.Category]
if !ok {
category = site.Category
}
rows[i] = []string{strconv.Itoa(site.ID), site.Name, category}
}
printResourceTable([]string{"Site ID", "Site Name", "Category"}, rows)
return nil
}
// printEnvironments fetches and prints out the defined enviroments
func printEnvironments(api resourceAPI) error {
// Fetch the list of enviroments from the Rainforest
environments, err := api.GetEnvironments()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
rows := make([][]string, len(environments))
for i, environment := range environments {
rows[i] = []string{strconv.Itoa(environment.ID), environment.Name}
}
printResourceTable([]string{"Environment ID", "Environment Name"}, rows)
return nil
}
// printFeatures fetches and prints features
func printFeatures(api resourceAPI) error {
// Fetch the list of features from the Rainforest
features, err := api.GetFeatures()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
rows := make([][]string, len(features))
for i, feature := range features {
rows[i] = []string{strconv.Itoa(feature.ID), feature.Title}
}
printResourceTable([]string{"Feature ID", "Feature Title"}, rows)
return nil
}
// printRunGroups fetches and prints runGroups
func printRunGroups(api resourceAPI) error {
// Fetch the list of runGroups from the Rainforest
runGroups, err := api.GetRunGroups()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
rows := make([][]string, len(runGroups))
for i, runGroup := range runGroups {
rows[i] = []string{strconv.Itoa(runGroup.ID), runGroup.Title}
}
printResourceTable([]string{"Run Group ID", "Run Group Title"}, rows)
return nil
}
func augmentJunitFileName(junitFile string, rerunAttempt uint) string {
if rerunAttempt > 0 {
junitFile = fmt.Sprintf("%v.%v", junitFile, rerunAttempt)
}
return junitFile
}
// write writeJunit fetches and writes a junit.xml file
func writeJunit(c cliContext, api resourceAPI, runID int) error {
var err error
if runID > 0 {
// noop
} else if runIDArg := c.Args().Get(0); runIDArg != "" {
runID, err = strconv.Atoi(runIDArg)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
} else {
return cli.NewExitError("No run ID argument found.", 1)
}
junitFile := c.String("junit-file")
if junitFile == "" {
return cli.NewExitError("JUnit output file not specified", 1)
}
rerunAttempt := c.Uint("rerun-attempt")
if rerunAttempt > 0 {
junitFile = augmentJunitFileName(junitFile, rerunAttempt)
}
xml, err := api.GetRunJunit(runID)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
file, err := os.Create(junitFile)
defer file.Close()
if err != nil {
return cli.NewExitError(err.Error(), 1)
} else {
file.WriteString(*xml)
}
return nil
}