-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
55 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func both(werr io.Writer, args []string) error { | ||
editor := os.Getenv("EDITOR") | ||
if editor == "" { | ||
return fmt.Errorf("$EDITOR must be set") | ||
} | ||
|
||
f, err := os.CreateTemp("", "pounce-") | ||
if err != nil { | ||
return fmt.Errorf("create temp file: %w", err) | ||
} | ||
|
||
fname := f.Name() | ||
|
||
defer os.Remove(fname) | ||
|
||
if err := collect(f, werr, args); err != nil { | ||
return fmt.Errorf("collect: %w", err) | ||
} | ||
|
||
if err := f.Close(); err != nil { | ||
return fmt.Errorf("close temp file: %w", err) | ||
} | ||
|
||
cmd := exec.Command(editor, fname) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
if err := cmd.Run(); err != nil { | ||
if err, ok := err.(*exec.ExitError); ok && err != nil { | ||
return fmt.Errorf("editor exited with code %d", err.ExitCode()) | ||
} | ||
|
||
return fmt.Errorf("run editor %q: %w", editor, err) | ||
} | ||
|
||
if f, err = os.Open(fname); err != nil { | ||
return fmt.Errorf("open temp file: %w", err) | ||
} | ||
|
||
if err := apply(f); err != nil { | ||
return fmt.Errorf("apply: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters