github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/tools/gendoc.go (about) 1 /* 2 Copyright 2018 Mirantis 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 tools 18 19 import ( 20 "errors" 21 "fmt" 22 "io" 23 24 "github.com/spf13/cobra" 25 26 "github.com/Mirantis/virtlet/pkg/config" 27 ) 28 29 // genDocCommand is used to generate markdown documentation for 30 // virtletctl commands. 31 type genDocCommand struct { 32 rootCmd *cobra.Command 33 out io.Writer 34 outDir string 35 config bool 36 } 37 38 // NewGenDocCmd returns a cobra.Command that generates markdown 39 // documentation for virtletctl commands. 40 func NewGenDocCmd(rootCmd *cobra.Command, out io.Writer) *cobra.Command { 41 gd := &genDocCommand{rootCmd: rootCmd, out: out} 42 cmd := &cobra.Command{ 43 Use: "gendoc output_dir", 44 Short: "Generate Markdown documentation for the commands", 45 Long: "This command produces documentation for the whole command tree, or the Virtlet configuration data.", 46 RunE: func(cmd *cobra.Command, args []string) error { 47 switch { 48 case !gd.config && len(args) != 1: 49 return errors.New("Must specify the output directory or --config") 50 case !gd.config: 51 gd.outDir = args[0] 52 case len(args) != 0: 53 return errors.New("Can't specify both the output directory and --config") 54 } 55 return gd.Run() 56 }, 57 } 58 cmd.Flags().BoolVar(&gd.config, "config", false, "Produce documentation for Virtlet config") 59 return cmd 60 } 61 62 // Run executes the command. 63 func (gd *genDocCommand) Run() error { 64 if gd.config { 65 fmt.Fprint(gd.out, config.GenerateDoc()) 66 } else { 67 return GenMarkdownTree(gd.rootCmd, gd.outDir) 68 } 69 return nil 70 }