code.cestus.io/tools/fabricator@v0.4.3/pkg/cmd/help/help.go (about)

     1  package help
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cestus.io/tools/fabricator/internal/pkg/util"
     7  	"code.cestus.io/tools/fabricator/pkg/fabricator"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  type options struct {
    12  	fabricator.IOStreams
    13  	cmd *cobra.Command
    14  }
    15  
    16  // NewOptions returns initialized Options
    17  func NewOptions(ioStreams fabricator.IOStreams) *options {
    18  	return &options{
    19  		IOStreams: ioStreams,
    20  	}
    21  }
    22  
    23  // Run executes help command of the root command
    24  func (o *options) Run() error {
    25  	if o.cmd.Root() == nil {
    26  		return errors.New("not a subcommand")
    27  	}
    28  	return o.cmd.Root().Help()
    29  }
    30  
    31  // NewHelpCommand creaters a new help command
    32  func NewHelpCommand(ioStreams fabricator.IOStreams) *cobra.Command {
    33  	o := NewOptions(ioStreams)
    34  	cmd := &cobra.Command{
    35  		Use:     "help",
    36  		Short:   "prints help",
    37  		Long:    "prints help",
    38  		Example: "",
    39  		Run: func(cmd *cobra.Command, args []string) {
    40  			util.CheckErr(o.Run())
    41  		},
    42  	}
    43  	o.cmd = cmd
    44  	return cmd
    45  }