github.com/googleapis/api-linter@v1.65.2/cmd/api-linter/rules.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"sort"
     8  
     9  	"github.com/googleapis/api-linter/lint"
    10  	"github.com/olekukonko/tablewriter"
    11  )
    12  
    13  type (
    14  	listedRule struct {
    15  		Name lint.RuleName
    16  	}
    17  	listedRules       []listedRule
    18  	listedRulesByName []listedRule
    19  )
    20  
    21  func (a listedRulesByName) Len() int           { return len(a) }
    22  func (a listedRulesByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
    23  func (a listedRulesByName) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
    24  
    25  func (r listedRules) printSummaryTable() ([]byte, error) {
    26  	var buf bytes.Buffer
    27  	table := tablewriter.NewWriter(&buf)
    28  	table.SetHeader([]string{"Rule Name"})
    29  	table.SetCaption(true, fmt.Sprintf("Total Rules: %d", len(r)))
    30  	for _, rule := range r {
    31  		table.Append([]string{
    32  			string(rule.Name),
    33  		})
    34  	}
    35  	table.Render()
    36  
    37  	return buf.Bytes(), nil
    38  }
    39  
    40  func outputRules(formatType string) error {
    41  	rules := listedRules{}
    42  	for id := range globalRules {
    43  		rules = append(rules, listedRule{
    44  			Name: id,
    45  		})
    46  	}
    47  
    48  	sort.Sort(listedRulesByName(rules))
    49  
    50  	// Determine the format for printing the results.
    51  	// YAML format is the default.
    52  	marshal := getOutputFormatFunc(formatType)
    53  
    54  	// Print the results.
    55  	b, err := marshal(rules)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	w := os.Stdout
    60  	if _, err = w.Write(b); err != nil {
    61  		return err
    62  	}
    63  
    64  	return nil
    65  }