github.com/dhax/go-base@v0.0.0-20231004214136-8be7e5c1972b/cmd/gendoc.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  
     8  	"github.com/dhax/go-base/api"
     9  	"github.com/go-chi/docgen"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var (
    14  	routes bool
    15  )
    16  
    17  // gendocCmd represents the gendoc command
    18  var gendocCmd = &cobra.Command{
    19  	Use:   "gendoc",
    20  	Short: "Generate project documentation",
    21  	Long: `A longer description that spans multiple lines and likely contains examples
    22  and usage of using your command. For example:
    23  
    24  Cobra is a CLI library for Go that empowers applications.
    25  This application is a tool to generate the needed files
    26  to quickly create a Cobra application.`,
    27  	Run: func(cmd *cobra.Command, args []string) {
    28  		if routes {
    29  			genRoutesDoc()
    30  		}
    31  	},
    32  }
    33  
    34  func init() {
    35  	RootCmd.AddCommand(gendocCmd)
    36  
    37  	// Here you will define your flags and configuration settings.
    38  
    39  	// Cobra supports Persistent Flags which will work for this command
    40  	// and all subcommands, e.g.:
    41  	// gendocCmd.PersistentFlags().String("foo", "", "A help for foo")
    42  
    43  	// Cobra supports local flags which will only run when this command
    44  	// is called directly, e.g.:
    45  	// gendocCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
    46  	gendocCmd.Flags().BoolVarP(&routes, "routes", "r", false, "create api routes markdown file")
    47  }
    48  
    49  func genRoutesDoc() {
    50  	api, _ := api.New(false)
    51  	fmt.Print("generating routes markdown file: ")
    52  	md := docgen.MarkdownRoutesDoc(api, docgen.MarkdownOpts{
    53  		ProjectPath: "github.com/dhax/go-base",
    54  		Intro:       "GoBase REST API.",
    55  	})
    56  	if err := os.WriteFile("routes.md", []byte(md), 0644); err != nil {
    57  		log.Println(err)
    58  		return
    59  	}
    60  	fmt.Println("OK")
    61  }