istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/collateral/cobra_noagent.go (about)

     1  //go:build !agent
     2  // +build !agent
     3  
     4  // Copyright Istio Authors
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //     http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package collateral
    19  
    20  import (
    21  	"github.com/spf13/cobra"
    22  	"github.com/spf13/cobra/doc"
    23  )
    24  
    25  // CobraCommand returns a Cobra command used to output a tool's collateral files (markdown docs, bash completion & man pages)
    26  // The root argument must be the root command for the tool.
    27  func CobraCommand(root *cobra.Command, meta Metadata) *cobra.Command {
    28  	hdr := &doc.GenManHeader{
    29  		Title:   meta.Title,
    30  		Section: meta.Section,
    31  		Manual:  meta.Manual,
    32  	}
    33  	c := Control{
    34  		OutputDir: ".",
    35  	}
    36  
    37  	var all bool
    38  
    39  	cmd := &cobra.Command{
    40  		Use:    "collateral",
    41  		Short:  "Generate collateral support files for this program",
    42  		Hidden: true,
    43  
    44  		RunE: func(cmd *cobra.Command, args []string) error {
    45  			if all {
    46  				c.EmitYAML = true
    47  				c.EmitBashCompletion = true
    48  				c.EmitZshCompletion = true
    49  				c.EmitManPages = true
    50  				c.EmitMarkdown = true
    51  				c.EmitHTMLFragmentWithFrontMatter = true
    52  				c.ManPageInfo = *hdr
    53  			}
    54  
    55  			return EmitCollateral(root, &c)
    56  		},
    57  	}
    58  
    59  	cmd.Flags().StringVarP(&c.OutputDir, "outputDir", "o", c.OutputDir, "Directory where to generate the collateral files")
    60  	cmd.Flags().BoolVarP(&all, "all", "", all, "Produce all supported collateral files")
    61  	cmd.Flags().BoolVarP(&c.EmitMarkdown, "markdown", "", c.EmitMarkdown, "Produce markdown documentation files")
    62  	cmd.Flags().BoolVarP(&c.EmitManPages, "man", "", c.EmitManPages, "Produce man pages")
    63  	cmd.Flags().BoolVarP(&c.EmitBashCompletion, "bash", "", c.EmitBashCompletion, "Produce bash completion files")
    64  	cmd.Flags().BoolVarP(&c.EmitZshCompletion, "zsh", "", c.EmitZshCompletion, "Produce zsh completion files")
    65  	cmd.Flags().BoolVarP(&c.EmitYAML, "yaml", "", c.EmitYAML, "Produce YAML documentation files")
    66  	cmd.Flags().BoolVarP(&c.EmitHTMLFragmentWithFrontMatter, "html_fragment_with_front_matter",
    67  		"", c.EmitHTMLFragmentWithFrontMatter, "Produce an HTML documentation file with Hugo/Jekyll-compatible front matter.")
    68  
    69  	return cmd
    70  }