github.com/lusis/distribution@v2.0.1+incompatible/cmd/registry-api-descriptor-template/main.go (about)

     1  // registry-api-descriptor-template uses the APIDescriptor defined in the
     2  // api/v2 package to execute templates passed to the command line.
     3  //
     4  // For example, to generate a new API specification, one would execute the
     5  // following command from the repo root:
     6  //
     7  // 	$ registry-api-descriptor-template doc/spec/api.md.tmpl > doc/spec/api.md
     8  //
     9  // The templates are passed in the api/v2.APIDescriptor object. Please see the
    10  // package documentation for fields available on that object. The template
    11  // syntax is from Go's standard library text/template package. For information
    12  // on Go's template syntax, please see golang.org/pkg/text/template.
    13  package main
    14  
    15  import (
    16  	"log"
    17  	"net/http"
    18  	"os"
    19  	"path/filepath"
    20  	"regexp"
    21  	"text/template"
    22  
    23  	"github.com/docker/distribution/registry/api/v2"
    24  )
    25  
    26  var spaceRegex = regexp.MustCompile(`\n\s*`)
    27  
    28  func main() {
    29  
    30  	if len(os.Args) != 2 {
    31  		log.Fatalln("please specify a template to execute.")
    32  	}
    33  
    34  	path := os.Args[1]
    35  	filename := filepath.Base(path)
    36  
    37  	funcMap := template.FuncMap{
    38  		"removenewlines": func(s string) string {
    39  			return spaceRegex.ReplaceAllString(s, " ")
    40  		},
    41  		"statustext":    http.StatusText,
    42  		"prettygorilla": prettyGorillaMuxPath,
    43  	}
    44  
    45  	tmpl := template.Must(template.New(filename).Funcs(funcMap).ParseFiles(path))
    46  
    47  	if err := tmpl.Execute(os.Stdout, v2.APIDescriptor); err != nil {
    48  		log.Fatalln(err)
    49  	}
    50  }
    51  
    52  // prettyGorillaMuxPath removes the regular expressions from a gorilla/mux
    53  // route string, making it suitable for documentation.
    54  func prettyGorillaMuxPath(s string) string {
    55  	// Stateful parser that removes regular expressions from gorilla
    56  	// routes. It correctly handles balanced bracket pairs.
    57  
    58  	var output string
    59  	var label string
    60  	var level int
    61  
    62  start:
    63  	if s[0] == '{' {
    64  		s = s[1:]
    65  		level++
    66  		goto capture
    67  	}
    68  
    69  	output += string(s[0])
    70  	s = s[1:]
    71  
    72  	goto end
    73  capture:
    74  	switch s[0] {
    75  	case '{':
    76  		level++
    77  	case '}':
    78  		level--
    79  
    80  		if level == 0 {
    81  			s = s[1:]
    82  			goto label
    83  		}
    84  	case ':':
    85  		s = s[1:]
    86  		goto skip
    87  	default:
    88  		label += string(s[0])
    89  	}
    90  	s = s[1:]
    91  	goto capture
    92  skip:
    93  	switch s[0] {
    94  	case '{':
    95  		level++
    96  	case '}':
    97  		level--
    98  	}
    99  	s = s[1:]
   100  
   101  	if level == 0 {
   102  		goto label
   103  	}
   104  
   105  	goto skip
   106  label:
   107  	if label != "" {
   108  		output += "<" + label + ">"
   109  		label = ""
   110  	}
   111  end:
   112  	if s != "" {
   113  		goto start
   114  	}
   115  
   116  	return output
   117  
   118  }