aboutsummaryrefslogtreecommitdiffstats
path: root/commands/command.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-16 15:21:21 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-16 15:21:21 +0200
commitd3893a5e04868fb99df28c8da62a0ee59d7e9064 (patch)
tree0f702ca72b82cd2429c4451f77baf99156525852 /commands/command.go
parent9423e7fdb6f70afb37f567d86a97594b06d43eb1 (diff)
downloadgit-bug-d3893a5e04868fb99df28c8da62a0ee59d7e9064.tar.gz
rework the commands thing and add a "commands" command to list all commands
Diffstat (limited to 'commands/command.go')
-rw-r--r--commands/command.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/commands/command.go b/commands/command.go
new file mode 100644
index 00000000..30298d3f
--- /dev/null
+++ b/commands/command.go
@@ -0,0 +1,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,
+ }
+}