pee/utils/editor.go

35 lines
521 B
Go
Raw Permalink Normal View History

2023-10-22 14:01:39 +05:30
package utils
import (
"os"
"os/exec"
)
2023-10-22 15:59:12 +05:30
func checkNvimExists() bool {
cmd := exec.Command("nvim", "--version")
err := cmd.Run()
return err == nil
}
2023-10-22 14:01:39 +05:30
func EditFile(filePath string, editorCommand string) error {
if editorCommand == "" {
2023-10-22 15:59:12 +05:30
if checkNvimExists() {
editorCommand = "nvim"
} else {
editorCommand = "vim"
}
2023-10-22 14:01:39 +05:30
}
cmd := exec.Command(editorCommand, filePath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}