git-bug/commands/comment.go

81 lines
1.5 KiB
Go
Raw Normal View History

2018-07-15 10:26:11 +03:00
package commands
import (
"errors"
"fmt"
2018-08-13 19:32:11 +03:00
2018-07-15 10:26:11 +03:00
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/bug/operations"
"github.com/MichaelMure/git-bug/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 commentMessage == "" {
commentMessage, err = input.BugCommentEditorInput(repo)
if err == input.ErrEmptyMessage {
fmt.Println("Empty message, aborting.")
return nil
}
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
}
operations.Comment(b, author, commentMessage)
2018-07-15 10:26:11 +03:00
return b.Commit(repo)
2018-07-15 10:26:11 +03:00
}
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
}