| 15 | ) |
| 16 | |
| 17 | func genDocsCmd() *cobra.Command { |
| 18 | genDocsCmd := &cobra.Command{ |
| 19 | Use: "gen-docs <path>", |
| 20 | Short: "[Internal] Generate documentation for the CLI", |
| 21 | Long: "[Internal] Generates the documentation for the CLI's Cobra commands. " + |
| 22 | "Docs are placed in the directory specified by <path>.", |
| 23 | Hidden: true, |
| 24 | Args: cobra.ExactArgs(1), |
| 25 | RunE: func(cmd *cobra.Command, args []string) error { |
| 26 | wd, err := os.Getwd() |
| 27 | if err != nil { |
| 28 | return errors.WithStack(err) |
| 29 | } |
| 30 | docsPath := filepath.Join(wd, args[0] /* relative path */) |
| 31 | |
| 32 | // We clear out the existing directory so that the doc-pages for |
| 33 | // commands that have been deleted in the CLI will also be removed |
| 34 | // after we re-generate the docs below |
| 35 | if err := fileutil.ClearDir(docsPath); err != nil { |
| 36 | return err |
| 37 | } |
| 38 | |
| 39 | rootCmd := cmd |
| 40 | for rootCmd.HasParent() { |
| 41 | rootCmd = rootCmd.Parent() |
| 42 | } |
| 43 | |
| 44 | // Removes the line in the generated docs of the form: |
| 45 | // ###### Auto generated by spf13/cobra on 18-Jul-2022 |
| 46 | rootCmd.DisableAutoGenTag = true |
| 47 | |
| 48 | return errors.WithStack(doc.GenMarkdownTree(rootCmd, docsPath)) |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | return genDocsCmd |
| 53 | } |