input.go

  1// Originally taken from the git-appraise project
  2
  3package input
  4
  5import (
  6	"bufio"
  7	"bytes"
  8	"fmt"
  9	"io/ioutil"
 10	"os"
 11	"os/exec"
 12	"strings"
 13
 14	"github.com/MichaelMure/git-bug/repository"
 15	"github.com/pkg/errors"
 16)
 17
 18const messageFilename = "BUG_MESSAGE_EDITMSG"
 19
 20// ErrEmptyMessage is returned when the required message has not been entered
 21var ErrEmptyMessage = errors.New("empty message")
 22
 23// ErrEmptyMessage is returned when the required title has not been entered
 24var ErrEmptyTitle = errors.New("empty title")
 25
 26const bugTitleCommentTemplate = `%s%s
 27
 28# Please enter the title and comment message. The first non-empty line will be
 29# used as the title. Lines starting with '#' will be ignored.
 30# An empty title aborts the operation.
 31`
 32
 33// BugCreateEditorInput will open the default editor in the terminal with a
 34// template for the user to fill. The file is then processed to extract title
 35// and message.
 36func BugCreateEditorInput(repo repository.Repo, preTitle string, preMessage string) (string, string, error) {
 37	if preMessage != "" {
 38		preMessage = "\n\n" + preMessage
 39	}
 40
 41	template := fmt.Sprintf(bugTitleCommentTemplate, preTitle, preMessage)
 42
 43	raw, err := launchEditorWithTemplate(repo, messageFilename, template)
 44
 45	if err != nil {
 46		return "", "", err
 47	}
 48
 49	lines := strings.Split(raw, "\n")
 50
 51	var title string
 52	var buffer bytes.Buffer
 53	for _, line := range lines {
 54		if strings.HasPrefix(line, "#") {
 55			continue
 56		}
 57
 58		if title == "" {
 59			trimmed := strings.TrimSpace(line)
 60			if trimmed != "" {
 61				title = trimmed
 62			}
 63			continue
 64		}
 65
 66		buffer.WriteString(line)
 67		buffer.WriteString("\n")
 68	}
 69
 70	if title == "" {
 71		return "", "", ErrEmptyTitle
 72	}
 73
 74	message := strings.TrimSpace(buffer.String())
 75
 76	return title, message, nil
 77}
 78
 79const bugCommentTemplate = `
 80
 81# Please enter the comment message. Lines starting with '#' will be ignored,
 82# and an empty message aborts the operation.
 83`
 84
 85// BugCommentEditorInput will open the default editor in the terminal with a
 86// template for the user to fill. The file is then processed to extract a comment.
 87func BugCommentEditorInput(repo repository.Repo) (string, error) {
 88	raw, err := launchEditorWithTemplate(repo, messageFilename, bugCommentTemplate)
 89
 90	if err != nil {
 91		return "", err
 92	}
 93
 94	lines := strings.Split(raw, "\n")
 95
 96	var buffer bytes.Buffer
 97	for _, line := range lines {
 98		if strings.HasPrefix(line, "#") {
 99			continue
100		}
101		buffer.WriteString(line)
102		buffer.WriteString("\n")
103	}
104
105	message := strings.TrimSpace(buffer.String())
106
107	if message == "" {
108		return "", ErrEmptyMessage
109	}
110
111	return message, nil
112}
113
114const bugTitleTemplate = `%s
115
116# Please enter the new title. Only one line will used.
117# Lines starting with '#' will be ignored, and an empty title aborts the operation.
118`
119
120// BugTitleEditorInput will open the default editor in the terminal with a
121// template for the user to fill. The file is then processed to extract a title.
122func BugTitleEditorInput(repo repository.Repo, preTitle string) (string, error) {
123	template := fmt.Sprintf(bugTitleTemplate, preTitle)
124	raw, err := launchEditorWithTemplate(repo, messageFilename, template)
125
126	if err != nil {
127		return "", err
128	}
129
130	lines := strings.Split(raw, "\n")
131
132	var title string
133	for _, line := range lines {
134		if strings.HasPrefix(line, "#") {
135			continue
136		}
137		trimmed := strings.TrimSpace(line)
138		if trimmed == "" {
139			continue
140		}
141		title = trimmed
142		break
143	}
144
145	if title == "" {
146		return "", ErrEmptyTitle
147	}
148
149	return title, nil
150}
151
152// launchEditorWithTemplate will launch an editor as launchEditor do, but with a
153// provided template.
154func launchEditorWithTemplate(repo repository.Repo, fileName string, template string) (string, error) {
155	path := fmt.Sprintf("%s/.git/%s", repo.GetPath(), fileName)
156
157	err := ioutil.WriteFile(path, []byte(template), 0644)
158
159	if err != nil {
160		return "", err
161	}
162
163	return launchEditor(repo, fileName)
164}
165
166// launchEditor launches the default editor configured for the given repo. This
167// method blocks until the editor command has returned.
168//
169// The specified filename should be a temporary file and provided as a relative path
170// from the repo (e.g. "FILENAME" will be converted to ".git/FILENAME"). This file
171// will be deleted after the editor is closed and its contents have been read.
172//
173// This method returns the text that was read from the temporary file, or
174// an error if any step in the process failed.
175func launchEditor(repo repository.Repo, fileName string) (string, error) {
176	path := fmt.Sprintf("%s/.git/%s", repo.GetPath(), fileName)
177	defer os.Remove(path)
178
179	editor, err := repo.GetCoreEditor()
180	if err != nil {
181		return "", fmt.Errorf("Unable to detect default git editor: %v\n", err)
182	}
183
184	cmd, err := startInlineCommand(editor, path)
185	if err != nil {
186		// Running the editor directly did not work. This might mean that
187		// the editor string is not a path to an executable, but rather
188		// a shell command (e.g. "emacsclient --tty"). As such, we'll try
189		// to run the command through bash, and if that fails, try with sh
190		args := []string{"-c", fmt.Sprintf("%s %q", editor, path)}
191		cmd, err = startInlineCommand("bash", args...)
192		if err != nil {
193			cmd, err = startInlineCommand("sh", args...)
194		}
195	}
196	if err != nil {
197		return "", fmt.Errorf("Unable to start editor: %v\n", err)
198	}
199
200	if err := cmd.Wait(); err != nil {
201		return "", fmt.Errorf("Editing finished with error: %v\n", err)
202	}
203
204	output, err := ioutil.ReadFile(path)
205
206	if err != nil {
207		return "", fmt.Errorf("Error reading edited file: %v\n", err)
208	}
209
210	return string(output), err
211}
212
213// FromFile loads and returns the contents of a given file. If - is passed
214// through, much like git, it will read from stdin. This can be piped data,
215// unless there is a tty in which case the user will be prompted to enter a
216// message.
217func FromFile(fileName string) (string, error) {
218	if fileName == "-" {
219		stat, err := os.Stdin.Stat()
220		if err != nil {
221			return "", fmt.Errorf("Error reading from stdin: %v\n", err)
222		}
223		if (stat.Mode() & os.ModeCharDevice) == 0 {
224			// There is no tty. This will allow us to read piped data instead.
225			output, err := ioutil.ReadAll(os.Stdin)
226			if err != nil {
227				return "", fmt.Errorf("Error reading from stdin: %v\n", err)
228			}
229			return string(output), err
230		}
231
232		fmt.Printf("(reading comment from standard input)\n")
233		var output bytes.Buffer
234		s := bufio.NewScanner(os.Stdin)
235		for s.Scan() {
236			output.Write(s.Bytes())
237			output.WriteRune('\n')
238		}
239		return output.String(), nil
240	}
241
242	output, err := ioutil.ReadFile(fileName)
243	if err != nil {
244		return "", fmt.Errorf("Error reading file: %v\n", err)
245	}
246	return string(output), err
247}
248
249func startInlineCommand(command string, args ...string) (*exec.Cmd, error) {
250	cmd := exec.Command(command, args...)
251	cmd.Stdin = os.Stdin
252	cmd.Stdout = os.Stdout
253	cmd.Stderr = os.Stderr
254	err := cmd.Start()
255	return cmd, err
256}