add a "comment" command

Michael Muré created

Change summary

commands/commands.go |  9 +++--
commands/comment.go  | 73 ++++++++++++++++++++++++++++++++++++++++++++++
commands/ls.go       |  6 +-
commands/new.go      |  5 +-
commands/pull.go     |  4 +-
commands/push.go     |  4 +-
6 files changed, 87 insertions(+), 14 deletions(-)

Detailed changes

commands/commands.go 🔗

@@ -23,8 +23,9 @@ func (cmd *Command) Run(repo repository.Repo, args []string) error {
 
 // CommandMap defines all of the available (sub)commands.
 var CommandMap = map[string]*Command{
-	"ls":   lsCmd,
-	"new":  newCmd,
-	"pull": pullCmd,
-	"push": pushCmd,
+	"comment": commentCmd,
+	"ls":      lsCmd,
+	"new":     newCmd,
+	"pull":    pullCmd,
+	"push":    pushCmd,
 }

commands/comment.go 🔗

@@ -0,0 +1,73 @@
+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"
+	"github.com/MichaelMure/git-bug/repository"
+)
+
+var commentFlagSet = flag.NewFlagSet("comment", flag.ExitOnError)
+
+var (
+	commentMessageFile = commentFlagSet.String("F", "", "Take the message from the given file. Use - to read the message from the standard input")
+	commentMessage     = commentFlagSet.String("m", "", "Provide the new message from the command line")
+)
+
+func runComment(repo repository.Repo, args []string) error {
+	commentFlagSet.Parse(args)
+	args = commentFlagSet.Args()
+
+	var err error
+
+	if len(args) == 0 {
+		return errors.New("No bug id provided")
+	}
+	if len(args) > 1 {
+		return errors.New("Only accepting one bug id is supported")
+	}
+
+	prefix := args[0]
+
+	if *commentMessageFile != "" && *commentMessage == "" {
+		*commentMessage, err = input.FromFile(*commentMessageFile)
+		if err != nil {
+			return err
+		}
+	}
+	if *commentMessageFile == "" && *commentMessage == "" {
+		*commentMessage, err = input.LaunchEditor(repo, messageFilename)
+		if err != nil {
+			return err
+		}
+	}
+
+	author, err := bug.GetUser(repo)
+	if err != nil {
+		return err
+	}
+
+	bug, err := bug.FindBug(repo, prefix)
+	if err != nil {
+		return err
+	}
+
+	addCommentOp := operations.NewAddCommentOp(author, *commentMessage)
+
+	bug.Append(addCommentOp)
+
+	err = bug.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,
+}

commands/ls.go 🔗

@@ -6,7 +6,7 @@ import (
 	"github.com/MichaelMure/git-bug/repository"
 )
 
-func RunLsBug(repo repository.Repo, args []string) error {
+func runLsBug(repo repository.Repo, args []string) error {
 	refs, err := repo.ListRefs(b.BugsRefPattern)
 
 	if err != nil {
@@ -30,7 +30,7 @@ func RunLsBug(repo repository.Repo, args []string) error {
 
 var lsCmd = &Command{
 	Usage: func(arg0 string) {
-		fmt.Printf("Usage: %s\n", arg0)
+		fmt.Printf("Usage: %s ls\n", arg0)
 	},
-	RunMethod: RunLsBug,
+	RunMethod: runLsBug,
 }

commands/new.go 🔗

@@ -17,7 +17,7 @@ var (
 	newMessage     = newFlagSet.String("m", "", "Provide a message to describe the issue")
 )
 
-func RunNewBug(repo repository.Repo, args []string) error {
+func runNewBug(repo repository.Repo, args []string) error {
 	newFlagSet.Parse(args)
 	args = newFlagSet.Args()
 
@@ -45,7 +45,6 @@ func RunNewBug(repo repository.Repo, args []string) error {
 		}
 	}
 
-	// Note: this is very primitive for now
 	author, err := bug.GetUser(repo)
 	if err != nil {
 		return err
@@ -71,5 +70,5 @@ var newCmd = &Command{
 		fmt.Printf("Usage: %s new <title> [<option>...]\n\nOptions:\n", arg0)
 		newFlagSet.PrintDefaults()
 	},
-	RunMethod: RunNewBug,
+	RunMethod: runNewBug,
 }

commands/pull.go 🔗

@@ -7,7 +7,7 @@ import (
 	"github.com/MichaelMure/git-bug/repository"
 )
 
-func pull(repo repository.Repo, args []string) error {
+func runPull(repo repository.Repo, args []string) error {
 	if len(args) > 1 {
 		return errors.New("Only pulling from one remote at a time is supported")
 	}
@@ -28,5 +28,5 @@ var pullCmd = &Command{
 	Usage: func(arg0 string) {
 		fmt.Printf("Usage: %s pull [<remote>]\n", arg0)
 	},
-	RunMethod: pull,
+	RunMethod: runPull,
 }

commands/push.go 🔗

@@ -7,7 +7,7 @@ import (
 	"github.com/MichaelMure/git-bug/repository"
 )
 
-func push(repo repository.Repo, args []string) error {
+func runPush(repo repository.Repo, args []string) error {
 	if len(args) > 1 {
 		return errors.New("Only pushing to one remote at a time is supported")
 	}
@@ -28,5 +28,5 @@ var pushCmd = &Command{
 	Usage: func(arg0 string) {
 		fmt.Printf("Usage: %s push [<remote>]\n", arg0)
 	},
-	RunMethod: push,
+	RunMethod: runPush,
 }