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