github.com/getgauge/gauge@v1.6.9/cmd/man.go (about)

     1  //go:build linux || darwin
     2  
     3  /*----------------------------------------------------------------
     4   *  Copyright (c) ThoughtWorks, Inc.
     5   *  Licensed under the Apache License, Version 2.0
     6   *  See LICENSE in the project root for license information.
     7   *----------------------------------------------------------------*/
     8  
     9  package cmd
    10  
    11  import (
    12  	"strings"
    13  
    14  	"os"
    15  
    16  	"path/filepath"
    17  
    18  	"github.com/getgauge/common"
    19  	"github.com/getgauge/gauge/logger"
    20  	"github.com/getgauge/gauge/version"
    21  	"github.com/spf13/cobra"
    22  	"github.com/spf13/cobra/doc"
    23  )
    24  
    25  const manDir = "man"
    26  
    27  var (
    28  	manCmd = &cobra.Command{
    29  		Use:     "man [flags]",
    30  		Short:   "Generate man pages",
    31  		Long:    `Generate man pages.`,
    32  		Example: `  gauge man`,
    33  		Run: func(cmd *cobra.Command, args []string) {
    34  			out, err := getDefaultPath()
    35  			if err != nil {
    36  				logger.Fatal(true, "Cannot find the gauge home directory.")
    37  			}
    38  			if err := genManPages(out); err != nil {
    39  				logger.Fatal(true, err.Error())
    40  			}
    41  		},
    42  		DisableAutoGenTag: true,
    43  	}
    44  )
    45  
    46  func init() {
    47  	GaugeCmd.AddCommand(manCmd)
    48  }
    49  
    50  func getDefaultPath() (string, error) {
    51  	p, err := common.GetGaugeHomeDirectory()
    52  	if err != nil {
    53  		return "", err
    54  	}
    55  	return filepath.Join(p, manDir, "man1"), nil
    56  }
    57  
    58  func genManPages(out string) error {
    59  	if err := os.MkdirAll(out, common.NewDirectoryPermissions); err != nil {
    60  		return err
    61  	}
    62  	if err := doc.GenManTreeFromOpts(setupCmd(), doc.GenManTreeOptions{
    63  		Header: &doc.GenManHeader{
    64  			Title:   "GAUGE",
    65  			Section: "1",
    66  			Manual:  "GAUGE MANUAL",
    67  			Source:  "GAUGE " + version.CurrentGaugeVersion.String(),
    68  		},
    69  		Path:             out,
    70  		CommandSeparator: "-",
    71  	}); err != nil {
    72  		return err
    73  	}
    74  	p := strings.TrimSuffix(out, filepath.Base(out))
    75  	logger.Infof(true, "To view gauge man pages, add `%s` to `MANPATH` environment variable.", p)
    76  	return nil
    77  }
    78  
    79  func setupCmd() *cobra.Command {
    80  	GaugeCmd.Short = "A light-weight cross-platform test automation tool"
    81  	GaugeCmd.Long = "Gauge is a light-weight cross-platform test automation tool with the ability to author test cases in the business language."
    82  	return GaugeCmd
    83  }