// Inspired by the git-appraise project // Package input contains helpers to use a text editor as an input for // various field of a bug package input import ( "bufio" "bytes" "fmt" "io/ioutil" "os" "os/exec" "strings" "github.com/MichaelMure/git-bug/repository" "github.com/pkg/errors" ) const messageFilename = "BUG_MESSAGE_EDITMSG" // ErrEmptyMessage is returned when the required message has not been entered var ErrEmptyMessage = errors.New("empty message") // ErrEmptyMessage is returned when the required title has not been entered var ErrEmptyTitle = errors.New("empty title") const bugTitleCommentTemplate = `%s%s # Please enter the title and comment message. The first non-empty line will be # used as the title. Lines starting with '#' will be ignored. # An empty title aborts the operation. ` // BugCreateEditorInput will open the default editor in the terminal with a // template for the user to fill. The file is then processed to extract title // and message. func BugCreateEditorInput(repo repository.RepoCommon, preTitle string, preMessage string) (string, string, error) { if preMessage != "" { preMessage = "\n\n" + preMessage } template := fmt.Sprintf(bugTitleCommentTemplate, preTitle, preMessage) raw, err := launchEditorWithTemplate(repo, messageFilename, template) if err != nil { return "", "", err } lines := strings.Split(raw, "\n") var title string var buffer bytes.Buffer for _, line := range lines { if strings.HasPrefix(line, "#") { continue } if title == "" { trimmed := strings.TrimSpace(line) if trimmed != "" { title = trimmed } continue } buffer.WriteString(line) buffer.WriteString("\n") } if title == "" { return "", "", ErrEmptyTitle } message := strings.TrimSpace(buffer.String()) return title, message, nil } const bugCommentTemplate = ` # Please enter the comment message. Lines starting with '#' will be ignored, # and an empty message aborts the operation. ` // BugCommentEditorInput will open the default editor in the terminal with a // template for the user to fill. The file is then processed to extract a comment. func BugCommentEditorInput(repo repository.RepoCommon) (string, error) { raw, err := launchEditorWithTemplate(repo, messageFilename, bugCommentTemplate) if err != nil { return "", err } lines := strings.Split(raw, "\n") var buffer bytes.Buffer for _, line := range lines { if strings.HasPrefix(line, "#") { continue } buffer.WriteString(line) buffer.WriteString("\n") } message := strings.TrimSpace(buffer.String()) if message == "" { return "", ErrEmptyMessage } return message, nil } const bugTitleTemplate = `%s # Please enter the new title. Only one line will used. # Lines starting with '#' will be ignored, and an empty title aborts the operation. ` // BugTitleEditorInput will open the default editor in the terminal with a // template for the user to fill. The file is then processed to extract a title. func BugTitleEditorInput(repo repository.RepoCommon, preTitle string) (string, error) { template := fmt.Sprintf(bugTitleTemplate, preTitle) raw, err := launchEditorWithTemplate(repo, messageFilename, template) if err != nil { return "", err } lines := strings.Split(raw, "\n") var title string for _, line := range lines { if strings.HasPrefix(line, "#") { continue } trimmed := strings.TrimSpace(line) if trimmed == "" { continue } title = trimmed break } if title == "" { return "", ErrEmptyTitle } return title, nil } const queryTemplate = `%s # Please edit the bug query. # Lines starting with '#' will be ignored, and an empty query aborts the operation. # # Example: status:open author:"rené descartes" sort:edit # # Valid filters are: # # - status:open, status:closed # - author: # - label: