// Normalizes a result set into a minimal range expression, such as // +{foo,bar}.example.com+. func Compress(nodes *Result) string { noDomain := []string{} domains := map[string][]string{} for node := range nodes.Iter() { tokens := strings.SplitN(node.(string), ".", 2) if len(tokens) == 2 { domains[tokens[1]] = append(domains[tokens[1]], tokens[0]) } else { noDomain = append(noDomain, node.(string)) } } sort.Sort(sortorder.Natural(noDomain)) result := compressNumeric(noDomain) var domainKeys = []string{} for domain, _ := range domains { domainKeys = append(domainKeys, domain) } sort.Sort(sortorder.Natural(domainKeys)) for _, domain := range domainKeys { domainNodes := domains[domain] sort.Sort(sortorder.Natural(domainNodes)) domainNodes = compressNumeric(domainNodes) joined := strings.Join(domainNodes, ",") if len(domainNodes) > 1 { joined = "{" + joined + "}" } result = append(result, joined+"."+domain) } return strings.Join(result, ",") }
// GetControlKeysFor returns the control keys for the given standard key. func (certification Certification) GetControlKeysFor(standardKey string) []string { var controlNames []string for controlName := range certification.Standards[standardKey] { controlNames = append(controlNames, controlName) } sort.Sort(sortorder.Natural(controlNames)) return controlNames }
// GetSortedStandards returns a list of sorted standard names func (certification Certification) GetSortedStandards() []string { var standardNames []string for standardName := range certification.Standards { standardNames = append(standardNames, standardName) } sort.Sort(sortorder.Natural(standardNames)) return standardNames }
// GetSortedControls returns a list of sorted controls func (standard Standard) GetSortedControls() []string { var controlNames []string for controlName := range standard.Controls { controlNames = append(controlNames, controlName) } sort.Sort(sortorder.Natural(controlNames)) return controlNames }
// GetSortedData returns a list of sorted controls func (standard Standard) GetSortedData(callback func(string)) { var controlNames []string for controlName := range standard.Controls { controlNames = append(controlNames, controlName) } sort.Sort(sortorder.Natural(controlNames)) for _, controlName := range controlNames { callback(controlName) } }
// Expand takes a range expression as argument // and returns an slice of strings as result // err is set to nil on success func (e *Erg) Expand(query string) (result []string, err error) { protocol := "http" if e.ssl { protocol = "https" } // TODO: Remove this with go 1.4 // http://stackoverflow.com/questions/25008571/golang-issue-x509-cannot-verify-signature-algorithm-unimplemented-on-net-http tr := &http.Transport{ TLSClientConfig: &tls.Config{ MaxVersion: tls.VersionTLS11, PreferServerCipherSuites: true, }, } client := &http.Client{Transport: tr} resp, err := client.Get(fmt.Sprintf("%s://%s:%d/range/list?%s", protocol, e.host, e.port, url.QueryEscape(query), )) if err != nil { return nil, err } if resp.StatusCode != 200 { body, readErr := ioutil.ReadAll(resp.Body) if readErr != nil { return nil, readErr } return nil, errors.New(string(body)) } defer resp.Body.Close() scanner := bufio.NewScanner(resp.Body) grangeResult := grange.NewResult() for scanner.Scan() { grangeResult.Add(scanner.Text()) } if grangeResult.Cardinality() > 0 { for node := range grangeResult.Iter() { result = append(result, node.(string)) } if e.Sort { sort.Sort(sortorder.Natural(result)) } } return result, nil }
// GetSortedData returns a list of sorted standards func (certification Certification) GetSortedData(callback func(string, string)) { var standardNames []string for standardName := range certification.Standards { standardNames = append(standardNames, standardName) } sort.Sort(sortorder.Natural(standardNames)) for _, standardKey := range standardNames { certification.Standards[standardKey].GetSortedData(func(controlKey string) { callback(standardKey, controlKey) }) } }