github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/sealer/cmd/gen_doc.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 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 15 package cmd 16 17 import ( 18 "fmt" 19 "os" 20 21 "github.com/spf13/cobra" 22 "github.com/spf13/cobra/doc" 23 ) 24 25 // GenDocCommand is used to implement 'gen-doc' command. 26 type GenDocCommand struct { 27 cmd *cobra.Command 28 29 // path is the destination path of generated markdown documents. 30 path string 31 } 32 33 func init() { 34 genDocCommand := &GenDocCommand{} 35 genDocCommand.cmd = &cobra.Command{ 36 Use: "gen-doc", 37 Short: "generate document for sealer CLI with MarkDown format", 38 Args: cobra.NoArgs, 39 SilenceErrors: true, 40 SilenceUsage: true, 41 RunE: func(cmd *cobra.Command, args []string) error { 42 return genDocCommand.runGenDoc() 43 }, 44 } 45 genDocCommand.addFlags() 46 rootCmd.AddCommand(genDocCommand.cmd) 47 } 48 49 // addFlags adds flags for specific command. 50 func (g *GenDocCommand) addFlags() { 51 flagSet := g.cmd.Flags() 52 53 flagSet.StringVarP(&g.path, "path", "p", "/tmp", "destination path of generated markdown documents") 54 } 55 56 func (g *GenDocCommand) runGenDoc() error { 57 if _, err := os.Stat(g.path); err != nil { 58 if os.IsNotExist(err) { 59 return fmt.Errorf("path %s does not exits, please check your gen-doc input flag --path", g.path) 60 } 61 return err 62 } 63 return doc.GenMarkdownTree(g.cmd.Parent(), g.path) 64 }