Editor attempts to locate an editor that's appropriate for the environment.
()
| 9 | |
| 10 | // Editor attempts to locate an editor that's appropriate for the environment. |
| 11 | func Editor() (string, error) { |
| 12 | |
| 13 | // default to `notepad.exe` on Windows |
| 14 | if runtime.GOOS == "windows" { |
| 15 | return "notepad", nil |
| 16 | } |
| 17 | |
| 18 | // look for `nano` and `vim` on the `PATH` |
| 19 | def, _ := exec.LookPath("editor") // default `editor` wrapper |
| 20 | nano, _ := exec.LookPath("nano") |
| 21 | vim, _ := exec.LookPath("vim") |
| 22 | |
| 23 | // set editor priority |
| 24 | editors := []string{ |
| 25 | os.Getenv("VISUAL"), |
| 26 | os.Getenv("EDITOR"), |
| 27 | def, |
| 28 | nano, |
| 29 | vim, |
| 30 | } |
| 31 | |
| 32 | // return the first editor that was found per the priority above |
| 33 | for _, editor := range editors { |
| 34 | if editor != "" { |
| 35 | return editor, nil |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // return an error if no path is found |
| 40 | return "", fmt.Errorf("no editor set") |
| 41 | } |
no outgoing calls