77 "fmt"
88 "html/template"
99 "io"
10+ "io/ioutil"
1011 "net/http"
1112 "os"
1213 "path/filepath"
@@ -27,16 +28,14 @@ import (
2728var (
2829 flConfig = flag .String ("config" , "" , "path to config file" )
2930 flAPIDir = flag .String ("api-dir" , "" , "api directory (or import path), point this to pkg/apis" )
30- flAPIPrefix = flag .String ("api-prefix" , `github.com/knative/serving/pkg/apis/` , "match APIs with this package prefix" )
31+ flAPIPrefix = flag .String ("api-prefix" , "" , "(optional) match only APIs with this package prefix" )
32+
33+ flHTTPAddr = flag .String ("http-addr" , "" , "start an HTTP server on specified addr to view the result (e.g. :8080)" )
34+ flOutFile = flag .String ("out-file" , "" , "path to output file to save the result" )
3135
3236 tplDir string
3337)
3438
35- type externalPackage struct {
36- TypeMatchPrefix string `json:"typeMatchPrefix"`
37- DocsURLTemplate string `json:"docsURLTemplate"`
38- }
39-
4039type generatorConfig struct {
4140 // APIGroups maps package import paths to Kubernetes API Groups.
4241 APIGroups map [string ]string `json:"apiGroups"`
@@ -57,6 +56,11 @@ type generatorConfig struct {
5756 TypeDisplayNamePrefixOverrides map [string ]string `json:"typeDisplayNamePrefixOverrides"`
5857}
5958
59+ type externalPackage struct {
60+ TypeMatchPrefix string `json:"typeMatchPrefix"`
61+ DocsURLTemplate string `json:"docsURLTemplate"`
62+ }
63+
6064func init () {
6165 if err := resolveTemplateDir (); err != nil {
6266 panic (err )
@@ -75,6 +79,12 @@ func init() {
7579 if * flAPIPrefix == "" {
7680 panic ("-api-prefix not specified" )
7781 }
82+ if * flHTTPAddr == "" && * flOutFile == "" {
83+ panic ("-out-file or -http-addr must be specified" )
84+ }
85+ if * flHTTPAddr != "" && * flOutFile != "" {
86+ panic ("only -out-file or -http-addr can be specified" )
87+ }
7888}
7989
8090func resolveTemplateDir () error {
@@ -120,30 +130,50 @@ func main() {
120130 }
121131 }
122132
123- h := func (w http.ResponseWriter , r * http.Request ) {
124- now := time .Now ()
125- defer func () { klog .Infof ("request took %v" , time .Since (now )) }()
126-
133+ mkOutput := func () (string , error ) {
127134 var b bytes.Buffer
128135 err := render (& b , pkgs , config )
129136 if err != nil {
130- klog . Warningf ( " render error: %+v" , err )
137+ return "" , errors . Wrap ( err , "failed to render the result" )
131138 }
132139
133- // remove trailing whitespace from each html line for markdown rendering
140+ // remove trailing whitespace from each html line for markdown renderers
134141 s := regexp .MustCompile (`(?m)^\s+` ).ReplaceAllString (b .String (), "" )
142+ return s , nil
143+ }
135144
136- if _ , err := fmt .Fprint (w , s ); err != nil {
137- klog .Warningf ("response write error: %v" , err )
145+ if * flOutFile != "" {
146+ dir := filepath .Dir (* flOutFile )
147+ if err := os .MkdirAll (dir , 0755 ); err != nil {
148+ klog .Fatalf ("failed to create dir %s: %v" , dir , err )
149+ }
150+ s , err := mkOutput ()
151+ if err != nil {
152+ klog .Fatalf ("failed: %+v" , err )
153+ }
154+ if err := ioutil .WriteFile (* flOutFile , []byte (s ), 0644 ); err != nil {
155+ klog .Fatalf ("failed to write to out file: %v" , err )
138156 }
157+ klog .Infof ("written to %s" , * flOutFile )
139158 }
140- http .HandleFunc ("/" , h )
141- klog .Infof ("server listening" )
142- addr := "localhost:8080"
143- if envAddr := os .Getenv ("LISTEN_ADDR" ); envAddr != "" {
144- addr = envAddr
159+
160+ if * flHTTPAddr != "" {
161+ h := func (w http.ResponseWriter , r * http.Request ) {
162+ now := time .Now ()
163+ defer func () { klog .Infof ("request took %v" , time .Since (now )) }()
164+ s , err := mkOutput ()
165+ if err != nil {
166+ fmt .Fprintf (w , "error: %+v" , err )
167+ klog .Warningf ("failed: %+v" , err )
168+ }
169+ if _ , err := fmt .Fprint (w , s ); err != nil {
170+ klog .Warningf ("response write error: %v" , err )
171+ }
172+ }
173+ http .HandleFunc ("/" , h )
174+ klog .Infof ("server listening at %s" , * flHTTPAddr )
175+ klog .Fatal (http .ListenAndServe (* flHTTPAddr , nil ))
145176 }
146- klog .Fatal (http .ListenAndServe (addr , nil ))
147177}
148178
149179func groupName (pkg * types.Package ) string {
0 commit comments