input.go

 1package buginput
 2
 3import (
 4	"fmt"
 5	"strings"
 6
 7	"github.com/pkg/errors"
 8
 9	"github.com/git-bug/git-bug/commands/input"
10	"github.com/git-bug/git-bug/repository"
11)
12
13const messageFilename = "BOARD_EDITMSG"
14
15// ErrEmptyTitle is returned when the required title has not been entered
16var ErrEmptyTitle = errors.New("empty title")
17
18const boardTitleTemplate = `%s
19
20# Please enter the title of the draft board item. Only one line will used.
21# Lines starting with '#' will be ignored, and an empty title aborts the operation.
22`
23
24// BoardTitleEditorInput will open the default editor in the terminal with a
25// template for the user to fill. The file is then processed to extract the title.
26func BoardTitleEditorInput(repo repository.RepoCommonStorage, preTitle string) (string, error) {
27	template := fmt.Sprintf(boardTitleTemplate, preTitle)
28
29	raw, err := input.LaunchEditorWithTemplate(repo, messageFilename, template)
30	if err != nil {
31		return "", err
32	}
33
34	return processTitle(raw)
35}
36
37// BoardTitleFileInput read from either from a file or from the standard input
38// and extract a title.
39func BoardTitleFileInput(fileName string) (string, error) {
40	raw, err := input.FromFile(fileName)
41	if err != nil {
42		return "", err
43	}
44
45	return processTitle(raw)
46}
47
48func processTitle(raw string) (string, error) {
49	lines := strings.Split(raw, "\n")
50
51	var title string
52	for _, line := range lines {
53		if strings.HasPrefix(line, "#") {
54			continue
55		}
56		trimmed := strings.TrimSpace(line)
57		if trimmed == "" {
58			continue
59		}
60		title = trimmed
61		break
62	}
63
64	if title == "" {
65		return "", ErrEmptyTitle
66	}
67
68	return title, nil
69}