...

Source file src/k8s.io/kubernetes/cmd/genswaggertypedocs/swagger_type_docs.go

Documentation: k8s.io/kubernetes/cmd/genswaggertypedocs

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  
    24  	kruntime "k8s.io/apimachinery/pkg/runtime"
    25  
    26  	flag "github.com/spf13/pflag"
    27  	"k8s.io/klog/v2"
    28  )
    29  
    30  var (
    31  	functionDest = flag.StringP("func-dest", "f", "-", "Output for swagger functions; '-' means stdout (default)")
    32  	typeSrc      = flag.StringP("type-src", "s", "", "From where we are going to read the types")
    33  	verify       = flag.BoolP("verify", "v", false, "Verifies if the given type-src file has documentation for every type")
    34  )
    35  
    36  func main() {
    37  	flag.Parse()
    38  
    39  	if *typeSrc == "" {
    40  		klog.Fatalf("Please define -s flag as it is the source file")
    41  	}
    42  
    43  	var funcOut io.Writer
    44  	if *functionDest == "-" {
    45  		funcOut = os.Stdout
    46  	} else {
    47  		file, err := os.Create(*functionDest)
    48  		if err != nil {
    49  			klog.Fatalf("Couldn't open %v: %v", *functionDest, err)
    50  		}
    51  		defer file.Close()
    52  		funcOut = file
    53  	}
    54  
    55  	docsForTypes := kruntime.ParseDocumentationFrom(*typeSrc)
    56  
    57  	if *verify {
    58  		rc, err := kruntime.VerifySwaggerDocsExist(docsForTypes, funcOut)
    59  		if err != nil {
    60  			fmt.Fprintf(os.Stderr, "Error in verification process: %s\n", err)
    61  		}
    62  		os.Exit(rc)
    63  	}
    64  
    65  	if len(docsForTypes) > 0 {
    66  		if err := kruntime.WriteSwaggerDocFunc(docsForTypes, funcOut); err != nil {
    67  			fmt.Fprintf(os.Stderr, "Error when writing swagger documentation functions: %s\n", err)
    68  			os.Exit(-1)
    69  		}
    70  	}
    71  }
    72  

View as plain text