MCPcopy Index your code
hub / github.com/git-bug/git-bug / LaunchEditor

Function LaunchEditor

commands/input/input.go:41–82  ·  view source on GitHub ↗

LaunchEditor launches the default editor configured for the given repo. This method blocks until the editor command has returned. The specified filename should be a temporary file and provided as a relative path from the repo (e.g. "FILENAME" will be converted to "[ /].git/git-bug/FILENAME

(repo repository.RepoCommonStorage, fileName string)

Source from the content-addressed store, hash-verified

39// This method returns the text that was read from the temporary file, or
40// an error if any step in the process failed.
41func LaunchEditor(repo repository.RepoCommonStorage, fileName string) (string, error) {
42 defer repo.LocalStorage().Remove(fileName)
43
44 editor, err := repo.GetCoreEditor()
45 if err != nil {
46 return "", fmt.Errorf("Unable to detect default git editor: %v\n", err)
47 }
48
49 repo.LocalStorage().Root()
50
51 // bypass the interface but that's ok: we need that because we are communicating
52 // the absolute path to an external program
53 path := filepath.Join(repo.LocalStorage().Root(), fileName)
54
55 cmd, err := startInlineCommand(editor, path)
56 if err != nil {
57 // Running the editor directly did not work. This might mean that
58 // the editor string is not a path to an executable, but rather
59 // a shell command (e.g. "emacsclient --tty"). As such, we'll try
60 // to run the command through bash, and if that fails, try with sh
61 args := []string{"-c", fmt.Sprintf("%s %q", editor, path)}
62 cmd, err = startInlineCommand("bash", args...)
63 if err != nil {
64 cmd, err = startInlineCommand("sh", args...)
65 }
66 }
67 if err != nil {
68 return "", fmt.Errorf("Unable to start editor: %v\n", err)
69 }
70
71 if err := cmd.Wait(); err != nil {
72 return "", fmt.Errorf("Editing finished with error: %v\n", err)
73 }
74
75 output, err := os.ReadFile(path)
76
77 if err != nil {
78 return "", fmt.Errorf("Error reading edited file: %v\n", err)
79 }
80
81 return string(output), err
82}
83
84// FromFile loads and returns the contents of a given file. If - is passed
85// through, much like git, it will read from stdin. This can be piped data,

Callers 1

LaunchEditorWithTemplateFunction · 0.85

Calls 6

startInlineCommandFunction · 0.85
ErrorfMethod · 0.80
WaitMethod · 0.80
RemoveMethod · 0.65
LocalStorageMethod · 0.65
GetCoreEditorMethod · 0.65

Tested by

no test coverage detected