package util import ( "errors" "fmt" "strings" ) var ( AllDcs = []string{ "DFW1", "DFW2", "DFW3", "IAD2", "IAD3", "IAD60", "ORD1", "LON3", "LON5", "SYD1", "SYD2", "SYD4", "HKG1", "HKO1", "FRA1", "SAT6", "AUS1", "AUS2", "YYZ1", "YYZ2", "AMS1", "SFO1", "BCB1", "EWR1", } AllRegions = []string{ "DFW", "ORD", "IAD", "LON", "SYD", "HKG", "HKO", "FRA", "SAT", "AUS", "YYZ", "AMS", "SFO", "BCB", "EWR", } ) // PadRight appends spaces to a string up to the given size func PadRight(str string, size int) string { l := len(str) if l >= size { return str } output := str for i := 0; i < size; i++ { output = output + " " } return output } // DcAndRegionListToDcList takes in a comma separated list and.or array of DCs or Regions (DFW1, ORD, etc) // and returns a unique list of all matching DCs, both in comma-separated string and array form. // an error is returned if an unknown DC/Region is encountered. // This function was created to make it easier to pull netdevices from mongo by DC or region. func DcAndRegionListToDcList(inString string, inArray []string) (outString string, outArray []string, invalid error) { errorString := "" var joinedArray []string for _, l := range strings.Split(strings.Replace(inString, " ", "", -1), ",") { joinedArray = append(joinedArray, strings.ToUpper(l)) } for _, l := range inArray { joinedArray = append(joinedArray, strings.ToUpper(l)) } dcSet := make(map[string]struct{}) for _, dc := range AllDcs { dcSet[dc] = struct{}{} } regionSet := make(map[string]struct{}) for _, reg := range AllRegions { regionSet[reg] = struct{}{} } outputSet := make(map[string]struct{}) for _, l := range joinedArray { if _, ok := dcSet[l]; ok { outputSet[l] = struct{}{} continue } if _, ok := regionSet[l]; ok { for _, ll := range AllDcs { if strings.Contains(ll, l) { outputSet[ll] = struct{}{} } } continue } errorString += fmt.Sprintf("%s, ", l) } for dc := range outputSet { outArray = append(outArray, dc) } if errorString != "" { invalid = errors.New("Invalid DCs/Regions (" + strings.TrimRight(errorString, ", ") + ")") } outString = strings.Join(outArray, ",") return } //TableSpacer will take in a [][]string and return a []string with evenly spaced table cells. func TableSpacer(table [][]string, separator string) (lines []string) { //will generate evenly distributed table lines //figure out our cell widths cellWidths := make(map[int]int) for _, r := range table { for n, c := range r { if w, ok := cellWidths[n]; ok { if len(c) > w { cellWidths[n] = len(c) } } else { cellWidths[n] = len(c) } } } //write our data to cells for _, r := range table { line := separator for n, c := range r { line += " " line += c for i := 0; i < (cellWidths[n] - len(c)); i++ { line += " " } line += " " line += separator } lines = append(lines, line) } return }