command.go

 1// Package commands contains the assorted sub commands supported by the git-bug tool.
 2package commands
 3
 4import (
 5	"flag"
 6	"fmt"
 7	"github.com/MichaelMure/git-bug/repository"
 8)
 9
10const messageFilename = "BUG_MESSAGE_EDITMSG"
11
12// Command represents the definition of a single command.
13type Command struct {
14	// Short description of the command
15	Description string
16	// Command line usage
17	Usage string
18	// Flag set of the command
19	flagSet *flag.FlagSet
20	// Execute the command
21	RunMethod func(repository.Repo, []string) error
22}
23
24// Run executes a command, given its arguments.
25//
26// The args parameter is all of the command line args that followed the
27// subcommand.
28func (cmd *Command) Run(repo repository.Repo, args []string) error {
29	return cmd.RunMethod(repo, args)
30}
31
32func (cmd *Command) PrintUsage(rootCommand string, cmdName string) {
33	fmt.Printf("Usage: %s %s %s\n", rootCommand, cmdName, cmd.Usage)
34
35	if cmd.flagSet != nil {
36		fmt.Printf("\nOptions:\n")
37		cmd.flagSet.PrintDefaults()
38	}
39}
40
41// CommandMap defines all of the available (sub)commands.
42var CommandMap map[string]*Command
43
44// We use init() to avoid a cycle in the data initialization because of the "commands" command
45func init() {
46	CommandMap = map[string]*Command{
47		"commands": commandsCmd,
48		"comment":  commentCmd,
49		"ls":       lsCmd,
50		"new":      newCmd,
51		"pull":     pullCmd,
52		"push":     pushCmd,
53	}
54}