aboutsummaryrefslogtreecommitdiffstats
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
parent9423e7fdb6f70afb37f567d86a97594b06d43eb1 (diff)
downloadgit-bug-d3893a5e04868fb99df28c8da62a0ee59d7e9064.tar.gz
rework the commands thing and add a "commands" command to list all commands
-rw-r--r--bug/bug.go2
-rw-r--r--commands/command.go54
-rw-r--r--commands/commands.go53
-rw-r--r--commands/comment.go16
-rw-r--r--commands/ls.go7
-rw-r--r--commands/new.go10
-rw-r--r--commands/pull.go7
-rw-r--r--commands/push.go7
8 files changed, 108 insertions, 48 deletions
diff --git a/bug/bug.go b/bug/bug.go
index f536716e..a82ee371 100644
--- a/bug/bug.go
+++ b/bug/bug.go
@@ -34,7 +34,7 @@ type Bug struct {
// Create a new Bug
func NewBug() (*Bug, error) {
- // TODO: replace with simple random bytes + hash
+ // TODO: replace with commit hash of (first commit + some random)
// Creating UUID Version 4
unique, err := uuid.ID4()
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,
+ }
+}
diff --git a/commands/commands.go b/commands/commands.go
index faca02ab..0a3c33ad 100644
--- a/commands/commands.go
+++ b/commands/commands.go
@@ -1,31 +1,44 @@
-// 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"
+var commandsFlagSet = flag.NewFlagSet("commands", flag.ExitOnError)
-// Command represents the definition of a single command.
-type Command struct {
- Usage func(string)
- RunMethod func(repository.Repo, []string) error
-}
+var (
+ commandsDesc = commandsFlagSet.Bool("pretty", false, "Output the command description as well as Markdown compatible comment")
+)
+
+func runCommands(repo repository.Repo, args []string) error {
+ commandsFlagSet.Parse(args)
+ args = commandsFlagSet.Args()
+
+ first := true
+
+ for name, cmd := range CommandMap {
+ if !first {
+ fmt.Println()
+ }
+
+ first = false
+
+ if *commandsDesc {
+ fmt.Printf("# %s\n", cmd.Description)
+ }
+
+ // TODO: the root name command ("git bug") should be passed from git-bug.go but well ...
+ fmt.Printf("%s %s %s\n", "git bug", name, cmd.Usage)
+ }
-// 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)
+ return nil
}
-// CommandMap defines all of the available (sub)commands.
-var CommandMap = map[string]*Command{
- "comment": commentCmd,
- "ls": lsCmd,
- "new": newCmd,
- "pull": pullCmd,
- "push": pushCmd,
+var commandsCmd = &Command{
+ Description: "Display available commands",
+ Usage: "[<option>...]",
+ flagSet: commandsFlagSet,
+ RunMethod: runCommands,
}
diff --git a/commands/comment.go b/commands/comment.go
index 27908793..f635139b 100644
--- a/commands/comment.go
+++ b/commands/comment.go
@@ -3,7 +3,6 @@ package commands
import (
"errors"
"flag"
- "fmt"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/bug/operations"
"github.com/MichaelMure/git-bug/commands/input"
@@ -50,24 +49,23 @@ func runComment(repo repository.Repo, args []string) error {
return err
}
- bug, err := bug.FindBug(repo, prefix)
+ b, err := bug.FindBug(repo, prefix)
if err != nil {
return err
}
addCommentOp := operations.NewAddCommentOp(author, *commentMessage)
- bug.Append(addCommentOp)
+ b.Append(addCommentOp)
- err = bug.Commit(repo)
+ err = b.Commit(repo)
return err
}
var commentCmd = &Command{
- Usage: func(arg0 string) {
- fmt.Printf("Usage: %s comment <id> [<option>...]\n\nOptions:\n", arg0)
- commentFlagSet.PrintDefaults()
- },
- RunMethod: runComment,
+ Description: "Add a new comment to a bug",
+ Usage: "[<options>...] <id>",
+ flagSet: commentFlagSet,
+ RunMethod: runComment,
}
diff --git a/commands/ls.go b/commands/ls.go
index 1e001ddb..dea2bba2 100644
--- a/commands/ls.go
+++ b/commands/ls.go
@@ -29,8 +29,7 @@ func runLsBug(repo repository.Repo, args []string) error {
}
var lsCmd = &Command{
- Usage: func(arg0 string) {
- fmt.Printf("Usage: %s ls\n", arg0)
- },
- RunMethod: runLsBug,
+ Description: "Display a summary of all bugs",
+ Usage: "",
+ RunMethod: runLsBug,
}
diff --git a/commands/new.go b/commands/new.go
index 2db95291..a2373515 100644
--- a/commands/new.go
+++ b/commands/new.go
@@ -3,7 +3,6 @@ package commands
import (
"errors"
"flag"
- "fmt"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/bug/operations"
"github.com/MichaelMure/git-bug/commands/input"
@@ -66,9 +65,8 @@ func runNewBug(repo repository.Repo, args []string) error {
}
var newCmd = &Command{
- Usage: func(arg0 string) {
- fmt.Printf("Usage: %s new <title> [<option>...]\n\nOptions:\n", arg0)
- newFlagSet.PrintDefaults()
- },
- RunMethod: runNewBug,
+ Description: "Create a new bug",
+ Usage: "[<option>...] <title>",
+ flagSet: newFlagSet,
+ RunMethod: runNewBug,
}
diff --git a/commands/pull.go b/commands/pull.go
index b408c285..dbff4fb1 100644
--- a/commands/pull.go
+++ b/commands/pull.go
@@ -25,8 +25,7 @@ func runPull(repo repository.Repo, args []string) error {
// showCmd defines the "push" subcommand.
var pullCmd = &Command{
- Usage: func(arg0 string) {
- fmt.Printf("Usage: %s pull [<remote>]\n", arg0)
- },
- RunMethod: runPull,
+ Description: "Pull bugs update from a git remote",
+ Usage: "[<remote>]",
+ RunMethod: runPull,
}
diff --git a/commands/push.go b/commands/push.go
index 416a61a1..acc3564a 100644
--- a/commands/push.go
+++ b/commands/push.go
@@ -25,8 +25,7 @@ func runPush(repo repository.Repo, args []string) error {
// showCmd defines the "push" subcommand.
var pushCmd = &Command{
- Usage: func(arg0 string) {
- fmt.Printf("Usage: %s push [<remote>]\n", arg0)
- },
- RunMethod: runPush,
+ Description: "Push bugs update to a git remote",
+ Usage: "[<remote>]",
+ RunMethod: runPush,
}