aboutsummaryrefslogtreecommitdiffstats
path: root/commands/command.go
blob: 30298d3fa7ce6b09b243a2ba4fbfba2bacc3b9d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Package commands contains the assorted sub commands supported by the git-bug tool.
package commands

import (
	"flag"
	"fmt"
	"github.com/MichaelMure/git-bug/repository"
)

const messageFilename = "BUG_MESSAGE_EDITMSG"

// Command represents the definition of a single command.
type Command struct {
	// Short description of the command
	Description string
	// Command line usage
	Usage string
	// Flag set of the command
	flagSet *flag.FlagSet
	// Execute the command
	RunMethod func(repository.Repo, []string) error
}

// Run executes a command, given its arguments.
//
// The args parameter is all of the command line args that followed the
// subcommand.
func (cmd *Command) Run(repo repository.Repo, args []string) error {
	return cmd.RunMethod(repo, args)
}

func (cmd *Command) PrintUsage(rootCommand string, cmdName string) {
	fmt.Printf("Usage: %s %s %s\n", rootCommand, cmdName, cmd.Usage)

	if cmd.flagSet != nil {
		fmt.Printf("\nOptions:\n")
		cmd.flagSet.PrintDefaults()
	}
}

// CommandMap defines all of the available (sub)commands.
var CommandMap map[string]*Command

// We use init() to avoid a cycle in the data initialization because of the "commands" command
func init() {
	CommandMap = map[string]*Command{
		"commands": commandsCmd,
		"comment":  commentCmd,
		"ls":       lsCmd,
		"new":      newCmd,
		"pull":     pullCmd,
		"push":     pushCmd,
	}
}