func updateIndexReSt(indexrst string, cmd *cobra.Command) string {
	indexrst += cmdFileName(cmd)
	for _, c := range cmd.Commands() {
		if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
			continue
		}
		indexrst = updateIndexReSt(indexrst, c)
	}
	return indexrst
}
Updates the index.rst file with the subcommand file names
package main
import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"github.com/spf13/cobra"
	"github.com/spf13/cobra/doc"
	"opendev.org/airship/airshipctl/cmd"
	"opendev.org/airship/airshipctl/pkg/fs"
)
var toctree = `####################
%s
####################
.. toctree::
   :maxdepth: 2
`
func main() {
	rootCmd := cmd.NewAirshipCTLCommand(os.Stdout)
	fs := fs.NewDocumentFs()
	dir := "./docs/source/cli"
	rootCmd.DisableAutoGenTag = true
	if err := genReSTRoot(rootCmd, dir, fs); err != nil {
		fmt.Fprintln(os.Stdout, err)
		os.Exit(1)
	}
	if err := genReST(rootCmd, dir, fs); err != nil {
		fmt.Fprintln(os.Stdout, err)
		os.Exit(1)
	}
}
func genReSTRoot(cmd *cobra.Command, dir string, fs fs.FileSystem) error {
	basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".rst"
	fileName := filepath.Join(dir, basename)
	f, err := fs.Create(fileName)
	if err != nil {
		return err
	}
	defer f.Close()
	if err := fs.WriteFile(fileName, []byte(filePrepender(fileName))); err != nil {
		return err
	}
	if err := doc.GenReSTCustom(cmd, f, linkHandler); err != nil {
		return err
	}
	return nil
}
func filePrepender(filename string) string {
	return ""
}
func linkHandler(name, ref string) string {
	return fmt.Sprintf(":ref:`%s <%s>`", name, ref)
}
func genReST(cmd *cobra.Command, dir string, fs fs.FileSystem) error {
	fileName := filepath.Join(dir, "index.rst")
	if _, err := fs.Create(fileName); err != nil {
		return err
	}
	rootIndexRst := fmt.Sprintf(toctree, "Commands") + cmdFileName(cmd)
	for _, c := range cmd.Commands() {
		names := strings.Split(c.CommandPath(), " ")
		cmdName := names[len(names)-1]
		cmdDir := filepath.Join(dir, cmdName)
		if err := checkAndCreateDir(cmdDir, fs); err != nil {
			return nil
		}
		if err := doc.GenReSTTreeCustom(c, cmdDir, filePrepender, linkHandler); err != nil {
			return err
		}
		if err := genIndexReST(c, cmdDir, cmdName, fs); err != nil {
			return err
		}
		rootIndexRst = rootIndexRst + "   " + cmdName + "/index\n"
	}
	if err := fs.WriteFile(fileName, []byte(rootIndexRst)); err != nil {
		return err
	}
	return nil
}
func checkAndCreateDir(dir string, fs fs.FileSystem) error {
	if !fs.Exists(dir) {
		if err := fs.Mkdir(dir); err != nil {
			return err
		}
	}
	if !fs.IsDir(dir) {
		return fmt.Errorf("expecting %s to be a directory", dir)
	}
	return nil
}
func genIndexReST(cmd *cobra.Command, cmdDir string, cmdName string, fs fs.FileSystem) error {
	cmdIndexFileName := filepath.Join(cmdDir, "index.rst")
	if _, err := fs.Create(cmdIndexFileName); err != nil {
		return err
	}
	cmdIndexRst := fmt.Sprintf(toctree, cmdName)
	cmdIndexRst = updateIndexReSt(cmdIndexRst, cmd)
	if err := fs.WriteFile(cmdIndexFileName, []byte(cmdIndexRst)); err != nil {
		return err
	}
	return nil
}