1// Copyright 2018 Istio Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package collateral
16
17import (
18	"github.com/spf13/cobra"
19	"github.com/spf13/cobra/doc"
20)
21
22// CobraCommand returns a Cobra command used to output a tool's collateral files (markdown docs, bash completion & man pages)
23// The root argument must be the root command for the tool.
24func CobraCommand(root *cobra.Command, hdr *doc.GenManHeader) *cobra.Command {
25	return CobraCommandWithFilter(root, hdr, Predicates{})
26}
27
28// CobraCommandWithFilter returns a Cobra command used to output a tool's collateral files (markdown docs, bash
29// completion & man pages). It allows passing in a set of predicates to filter out and remove items selectively.
30// The root argument must be the root command for the tool.
31func CobraCommandWithFilter(root *cobra.Command, hdr *doc.GenManHeader, p Predicates) *cobra.Command {
32	c := Control{
33		OutputDir:  ".",
34		Predicates: p,
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}
71