view editor.go @ 8:c775cee5aac2

fix error
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Mon, 07 Dec 2020 10:10:16 +0900
parents af840bc25791
children
line wrap: on
line source

package growsync

import (
	"os"
	"os/exec"
	"strings"

	"github.com/mattn/go-tty"
	"golang.org/x/xerrors"
)

func doEdit(mdfilePATH string) error {
	editor := os.Getenv("EDITOR")
	if editor == "" {
		editor = "vi"
	}
	tty, err := tty.Open()
	if err != nil {
		return xerrors.Errorf("[error] failed open tty %+w", err)
	}
	defer tty.Close()

	editorWithArgs := strings.Fields(editor)
	editorWithArgs = append(editorWithArgs, mdfilePATH)

	cmd := exec.Command(editorWithArgs[0], editorWithArgs[1:]...)
	cmd.Stdin = tty.Input()
	cmd.Stdout = tty.Output()
	cmd.Stderr = tty.Output()

	if err := cmd.Run(); err != nil {
		return xerrors.Errorf("[error] failed exec editor %+w", err)
	}

	return nil
}