github.com/kaituanwang/hyperledger@v2.0.1+incompatible/common/metrics/cmd/gendoc/main.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"flag"
    12  	"io/ioutil"
    13  	"os"
    14  	"text/template"
    15  
    16  	"github.com/hyperledger/fabric/common/metrics/gendoc"
    17  	"golang.org/x/tools/go/packages"
    18  )
    19  
    20  // Gendoc can be used used to discover the metrics options declared at the
    21  // package level in the fabric tree and output a table that can be used in the
    22  // documentation.
    23  
    24  var templatePath = flag.String(
    25  	"template",
    26  	"docs/source/metrics_reference.rst.tmpl",
    27  	"The documentation template.",
    28  )
    29  
    30  func main() {
    31  	flag.Parse()
    32  
    33  	patterns := flag.Args()
    34  	if len(patterns) == 0 {
    35  		patterns = []string{"github.com/hyperledger/fabric/..."}
    36  	}
    37  
    38  	pkgs, err := packages.Load(&packages.Config{Mode: packages.LoadSyntax}, patterns...)
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  
    43  	options, err := gendoc.Options(pkgs)
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  
    48  	cells, err := gendoc.NewCells(options)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	funcMap := template.FuncMap{
    54  		"PrometheusTable": func() string {
    55  			buf := &bytes.Buffer{}
    56  			gendoc.NewPrometheusTable(cells).Generate(buf)
    57  			return buf.String()
    58  		},
    59  		"StatsdTable": func() string {
    60  			buf := &bytes.Buffer{}
    61  			gendoc.NewStatsdTable(cells).Generate(buf)
    62  			return buf.String()
    63  		},
    64  	}
    65  
    66  	docTemplate, err := ioutil.ReadFile(*templatePath)
    67  	if err != nil {
    68  		panic(err)
    69  	}
    70  
    71  	tmpl, err := template.New("metrics_reference").Funcs(funcMap).Parse(string(docTemplate))
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  
    76  	if err := tmpl.Execute(os.Stdout, ""); err != nil {
    77  		panic(err)
    78  	}
    79  }