graphql-engine/cli/commands/docs.go

55 lines
1.5 KiB
Go
Raw Normal View History

package commands
import (
"os"
"github.com/hasura/graphql-engine/cli"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
2019-01-28 16:55:28 +03:00
"github.com/spf13/viper"
)
2019-01-28 16:55:28 +03:00
// NewDocsCmd returns the docs command
func NewDocsCmd(ec *cli.ExecutionContext) *cobra.Command {
var docType, docDirectory string
docsCmd := &cobra.Command{
Use: "docs",
Short: "Generate CLI docs in various formats",
Hidden: true,
SilenceUsage: true,
2019-01-28 16:55:28 +03:00
PreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = viper.New()
return ec.Prepare()
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = os.MkdirAll(docDirectory, os.ModePerm)
if err != nil {
return errors.Wrap(err, "unable to create directory")
}
switch docType {
case "man":
err = doc.GenManTree(rootCmd, &doc.GenManHeader{Title: "HASURA", Section: "3"}, docDirectory)
case "md":
err = doc.GenMarkdownTree(rootCmd, docDirectory)
case "rest":
err = doc.GenReSTTree(rootCmd, docDirectory)
case "yaml":
err = doc.GenYamlTree(rootCmd, docDirectory)
default:
return errors.New("unknown type")
}
if err != nil {
return errors.Wrap(err, "generating docs failed")
}
ec.Logger.Infof("[%s] docs generated in [%s]", docType, docDirectory)
return nil
},
}
f := docsCmd.Flags()
f.StringVar(&docType, "type", "md", "type of documentation to generate (man, md, rest, yaml)")
f.StringVar(&docDirectory, "directory", "docs", "directory where docs should be generated")
return docsCmd
}