github.com/rainforestapp/rainforest-cli@v2.12.0+incompatible/resources.go (about) 1 package main 2 3 import ( 4 "strconv" 5 6 "github.com/olekukonko/tablewriter" 7 "github.com/rainforestapp/rainforest-cli/rainforest" 8 "github.com/urfave/cli" 9 ) 10 11 // printResourceTable uses olekukonko/tablewriter as a pretty printer 12 // for the tabular resources we get from the API and formatted using formatAsTable. 13 func printResourceTable(headers []string, rows [][]string) { 14 // Init tablewriter with out global var as a target 15 table := tablewriter.NewWriter(tablesOut) 16 17 // Prepare the tablewriter 18 table.SetHeader(headers) 19 table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: false}) 20 table.SetCenterSeparator("|") 21 table.AppendBulk(rows) // Add Bulk Data 22 23 // Render prints out the table to output specified during init. 24 table.Render() 25 } 26 27 // resourceAPI is part of the API connected to available resources 28 type resourceAPI interface { 29 GetFolders() ([]rainforest.Folder, error) 30 GetBrowsers() ([]rainforest.Browser, error) 31 GetSites() ([]rainforest.Site, error) 32 GetEnvironments() ([]rainforest.Environment, error) 33 GetFeatures() ([]rainforest.Feature, error) 34 GetRunGroups() ([]rainforest.RunGroup, error) 35 } 36 37 // printFolders fetches and prints out the available folders from the API 38 func printFolders(api resourceAPI) error { 39 // Fetch the list of folders from the Rainforest 40 folders, err := api.GetFolders() 41 if err != nil { 42 return cli.NewExitError(err.Error(), 1) 43 } 44 45 rows := make([][]string, len(folders)) 46 for i, folder := range folders { 47 rows[i] = []string{strconv.Itoa(folder.ID), folder.Title} 48 } 49 50 printResourceTable([]string{"Folder ID", "Folder Name"}, rows) 51 return nil 52 } 53 54 // printBrowsers fetches and prints out the browsers available to the client 55 func printBrowsers(api resourceAPI) error { 56 // Fetch the list of browsers from the Rainforest 57 browsers, err := api.GetBrowsers() 58 if err != nil { 59 return cli.NewExitError(err.Error(), 1) 60 } 61 62 rows := make([][]string, len(browsers)) 63 for i, browser := range browsers { 64 rows[i] = []string{browser.Name, browser.Description} 65 } 66 67 printResourceTable([]string{"Browser ID", "Browser Name"}, rows) 68 return nil 69 } 70 71 // printSites fetches and prints out the defined sites 72 func printSites(api resourceAPI) error { 73 // Fetch the list of sites from the Rainforest 74 sites, err := api.GetSites() 75 if err != nil { 76 return cli.NewExitError(err.Error(), 1) 77 } 78 79 humanizedSiteCategories := map[string]string{ 80 "device_farm": "Device Farm", 81 "android": "Android", 82 "ios": "iOS", 83 "site": "Site", 84 } 85 86 rows := make([][]string, len(sites)) 87 for i, site := range sites { 88 category, ok := humanizedSiteCategories[site.Category] 89 if !ok { 90 category = site.Category 91 } 92 rows[i] = []string{strconv.Itoa(site.ID), site.Name, category} 93 } 94 95 printResourceTable([]string{"Site ID", "Site Name", "Category"}, rows) 96 return nil 97 } 98 99 // printEnvironments fetches and prints out the defined enviroments 100 func printEnvironments(api resourceAPI) error { 101 // Fetch the list of enviroments from the Rainforest 102 environments, err := api.GetEnvironments() 103 if err != nil { 104 return cli.NewExitError(err.Error(), 1) 105 } 106 107 rows := make([][]string, len(environments)) 108 for i, environment := range environments { 109 rows[i] = []string{strconv.Itoa(environment.ID), environment.Name} 110 } 111 112 printResourceTable([]string{"Environment ID", "Environment Name"}, rows) 113 return nil 114 } 115 116 // printFeatures fetches and prints features 117 func printFeatures(api resourceAPI) error { 118 // Fetch the list of features from the Rainforest 119 features, err := api.GetFeatures() 120 if err != nil { 121 return cli.NewExitError(err.Error(), 1) 122 } 123 124 rows := make([][]string, len(features)) 125 for i, feature := range features { 126 rows[i] = []string{strconv.Itoa(feature.ID), feature.Title} 127 } 128 129 printResourceTable([]string{"Feature ID", "Feature Title"}, rows) 130 return nil 131 } 132 133 // printRunGroups fetches and prints runGroups 134 func printRunGroups(api resourceAPI) error { 135 // Fetch the list of runGroups from the Rainforest 136 runGroups, err := api.GetRunGroups() 137 if err != nil { 138 return cli.NewExitError(err.Error(), 1) 139 } 140 141 rows := make([][]string, len(runGroups)) 142 for i, runGroup := range runGroups { 143 rows[i] = []string{strconv.Itoa(runGroup.ID), runGroup.Title} 144 } 145 146 printResourceTable([]string{"Run Group ID", "Run Group Title"}, rows) 147 return nil 148 }