Merge pull request #2869 from fuweid/ctr_make_error_clear_in_edit_subcommand

ctr/content: make editor flag is required
This commit is contained in:
Michael Crosby 2018-12-11 13:51:10 -05:00 committed by GitHub
commit 4ccff37c7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -290,6 +290,11 @@ var (
Name: "validate",
Usage: "validate the result against a format (json, mediatype, etc.)",
},
cli.StringFlag{
Name: "editor",
Usage: "select editor (vim, emacs, etc.)",
EnvVar: "EDITOR",
},
},
Action: func(context *cli.Context) error {
var (
@ -320,7 +325,7 @@ var (
}
defer ra.Close()
nrc, err := edit(content.NewReader(ra))
nrc, err := edit(context, content.NewReader(ra))
if err != nil {
return err
}
@ -505,7 +510,12 @@ var (
}
)
func edit(rd io.Reader) (io.ReadCloser, error) {
func edit(context *cli.Context, rd io.Reader) (io.ReadCloser, error) {
editor := context.String("editor")
if editor == "" {
return nil, fmt.Errorf("editor is required")
}
tmp, err := ioutil.TempFile(os.Getenv("XDG_RUNTIME_DIR"), "edit-")
if err != nil {
return nil, err
@ -516,7 +526,7 @@ func edit(rd io.Reader) (io.ReadCloser, error) {
return nil, err
}
cmd := exec.Command("sh", "-c", "$EDITOR "+tmp.Name())
cmd := exec.Command("sh", "-c", fmt.Sprintf("%s %s", editor, tmp.Name()))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout