github.com/jenkins-x/jx/v2@v2.1.155/cmd/codegen/app/generate_docs.go (about)

     1  package app
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/jenkins-x/jx/v2/cmd/codegen/generator"
     8  	"github.com/jenkins-x/jx/v2/cmd/codegen/util"
     9  	"github.com/pkg/errors"
    10  
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  // GenerateDocsOptions contains the options for the create client docs command
    15  type GenerateDocsOptions struct {
    16  	GenerateOptions
    17  	ReferenceDocsVersion string
    18  }
    19  
    20  var (
    21  	createClientDocsLong = `This command code generates clients docs (Swagger,OpenAPI and HTML) for
    22  	the specified custom resources.
    23   
    24  `
    25  
    26  	createClientDocsExample = `
    27  # lets generate client docs
    28  codegen docs
    29  
    30  # You will normally want to add a target to your Makefile that looks like
    31  generate-clients-docs:
    32  	codegen docs
    33  
    34  # and then call
    35  make generate-clients-docs
    36  `
    37  )
    38  
    39  // NewCreateDocsCmd creates apidocs for CRDs
    40  func NewCreateDocsCmd(genOpts GenerateOptions) *cobra.Command {
    41  	o := &GenerateDocsOptions{
    42  		GenerateOptions: genOpts,
    43  	}
    44  
    45  	cobraCmd := &cobra.Command{
    46  		Use:     "docs",
    47  		Short:   "Creates client docs for Custom Resources",
    48  		Long:    createClientDocsLong,
    49  		Example: createClientDocsExample,
    50  
    51  		Run: func(c *cobra.Command, args []string) {
    52  			o.Cmd = c
    53  			o.Args = args
    54  			err := run(o)
    55  			util.CheckErr(err)
    56  		},
    57  	}
    58  
    59  	wd, err := os.Getwd()
    60  	if err != nil {
    61  		util.AppLogger().Warnf("error getting working directory for %v\n", err)
    62  	}
    63  
    64  	cobraCmd.Flags().StringVarP(&o.InputBase, optionInputBase, "", wd,
    65  		"Input base (root of module), by default the current working directory")
    66  	cobraCmd.Flags().StringVarP(&o.OutputBase, optionOutputBase, "o", filepath.Join(wd, "docs/apidocs"),
    67  		"output base directory, by default the <current working directory>/docs/apidocs")
    68  	cobraCmd.Flags().BoolVarP(&o.Global, global, "", false, "use the users GOPATH")
    69  	return cobraCmd
    70  }
    71  
    72  func run(o *GenerateDocsOptions) error {
    73  	var err error
    74  	if o.OutputBase == "" {
    75  		return util.MissingOption(optionOutputBase)
    76  	}
    77  	util.AppLogger().Infof("generating docs to %s\n", o.OutputBase)
    78  
    79  	cleanupFunc := func() {}
    80  	gopath := util.GoPath()
    81  	if !o.Global {
    82  		gopath, err = util.IsolatedGoPath()
    83  		if err != nil {
    84  			return errors.Wrapf(err, "getting isolated gopath")
    85  		}
    86  		cleanupFunc, err = util.BackupGoModAndGoSum()
    87  		if err != nil {
    88  			return errors.Wrapf(err, "backing up go.mod and go.sum")
    89  		}
    90  	}
    91  	defer cleanupFunc()
    92  	err = generator.InstallGenAPIDocs(o.GeneratorVersion, gopath)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	referenceDocsRepo, err := generator.DetermineSourceLocation(o.InputBase, gopath)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	err = generator.GenerateAPIDocs(o.OutputBase, gopath)
   103  	if err != nil {
   104  		return err
   105  	}
   106  	err = generator.AssembleAPIDocsStatic(referenceDocsRepo, o.OutputBase)
   107  	if err != nil {
   108  		return err
   109  	}
   110  	err = generator.AssembleAPIDocs(o.OutputBase, filepath.Join(o.OutputBase, "site"))
   111  	if err != nil {
   112  		return err
   113  	}
   114  	return nil
   115  }