/* * Copyright 2020 The Dragonfly Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package dependency import ( "fmt" "io/fs" "os" "github.com/spf13/cobra" "github.com/spf13/cobra/doc" ) // genDocCommand is used to implement 'doc' command. type genDocCommand struct { cmd *cobra.Command path string } func newDocCommand(name string) *cobra.Command { docCommand := new(genDocCommand) docCommand.cmd = &cobra.Command{ Use: "doc", Short: "generate documents", Long: fmt.Sprintf("generate markdown documents for cmd: %s .", name), Args: cobra.NoArgs, DisableAutoGenTag: true, SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { return docCommand.runDoc() }, } docCommand.bindFlags() return docCommand.cmd } // bindFlags binds flags for specific command. func (g *genDocCommand) bindFlags() { flagSet := g.cmd.Flags() flagSet.StringVar(&g.path, "path", "./", "destination dir of generated markdown documents") } func (g *genDocCommand) runDoc() error { _ = os.MkdirAll(g.path, fs.FileMode(0700)) file, err := os.Stat(g.path) if err != nil { return err } if !file.IsDir() { return fmt.Errorf("path %s is not dir, please check it", g.path) } return doc.GenMarkdownTree(g.cmd.Parent(), g.path) }