git-bug/commands/comment.go

78 lines
1.5 KiB
Go
Raw Normal View History

2018-07-15 10:26:11 +03:00
package commands
import (
"errors"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/bug/operations"
"github.com/MichaelMure/git-bug/commands/input"
"github.com/spf13/cobra"
2018-07-15 10:26:11 +03:00
)
var (
commentMessageFile string
commentMessage string
2018-07-15 10:26:11 +03:00
)
func runComment(cmd *cobra.Command, args []string) error {
2018-07-15 10:26:11 +03:00
var err error
if len(args) > 1 {
2018-07-17 20:28:37 +03:00
return errors.New("Only one bug id is supported")
}
if len(args) == 0 {
return errors.New("You must provide a bug id")
2018-07-15 10:26:11 +03:00
}
prefix := args[0]
if commentMessageFile != "" && commentMessage == "" {
commentMessage, err = input.FromFile(commentMessageFile)
2018-07-15 10:26:11 +03:00
if err != nil {
return err
}
}
if commentMessageFile == "" && commentMessage == "" {
commentMessage, err = input.LaunchEditor(repo, messageFilename)
2018-07-15 10:26:11 +03:00
if err != nil {
return err
}
}
author, err := bug.GetUser(repo)
if err != nil {
return err
}
b, err := bug.FindLocalBug(repo, prefix)
2018-07-15 10:26:11 +03:00
if err != nil {
return err
}
addCommentOp := operations.NewAddCommentOp(author, commentMessage)
2018-07-15 10:26:11 +03:00
b.Append(addCommentOp)
2018-07-15 10:26:11 +03:00
err = b.Commit(repo)
2018-07-15 10:26:11 +03:00
return err
}
var commentCmd = &cobra.Command{
Use: "comment <id> [<options>...]",
Short: "Add a new comment to a bug",
RunE: runComment,
}
func init() {
2018-07-20 16:46:14 +03:00
RootCmd.AddCommand(commentCmd)
commentCmd.Flags().StringVarP(&commentMessageFile, "file", "F", "",
"Take the message from the given file. Use - to read the message from the standard input",
)
commentCmd.Flags().StringVarP(&commentMessage, "message", "m", "",
"Provide the new message from the command line",
)
2018-07-15 10:26:11 +03:00
}