MCPcopy Create free account
hub / github.com/basecamp/hey-cli / Open

Function Open

internal/editor/editor.go:12–45  ·  view source on GitHub ↗

Open launches $EDITOR with a temp file containing initialContent, waits for the editor to close, and returns the edited content.

(initialContent string)

Source from the content-addressed store, hash-verified

10// Open launches $EDITOR with a temp file containing initialContent,
11// waits for the editor to close, and returns the edited content.
12func Open(initialContent string) (string, error) {
13 editor := os.Getenv("EDITOR")
14 if editor == "" {
15 editor = "vi"
16 }
17
18 tmpFile, err := os.CreateTemp("", "hey-*.txt")
19 if err != nil {
20 return "", fmt.Errorf("could not create temp file: %w", err)
21 }
22 defer os.Remove(tmpFile.Name()) //nolint:gosec // G703: path from os.CreateTemp
23
24 if _, err = tmpFile.WriteString(initialContent); err != nil {
25 _ = tmpFile.Close()
26 return "", fmt.Errorf("could not write temp file: %w", err)
27 }
28 _ = tmpFile.Close()
29
30 cmd := exec.CommandContext(context.Background(), editor, tmpFile.Name()) //nolint:gosec // G204: intentional — launches user's $EDITOR
31 cmd.Stdin = os.Stdin
32 cmd.Stdout = os.Stdout
33 cmd.Stderr = os.Stderr
34
35 if err = cmd.Run(); err != nil {
36 return "", fmt.Errorf("editor exited with error: %w", err)
37 }
38
39 data, err := os.ReadFile(tmpFile.Name()) //nolint:gosec // G703: path from os.CreateTemp
40 if err != nil {
41 return "", fmt.Errorf("could not read edited file: %w", err)
42 }
43
44 return string(data), nil
45}

Callers 3

runMethod · 0.92
runMethod · 0.92
runMethod · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected