glow/editor/editor.go
Carlos Alexandro Becker 54dd62a2a4
fix: handle running inside a snap (#527)
* fix: handle running inside a snap

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* test: fixes

* docs: improve error message

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: bashbunni <bunni@bashbunni.dev>
2023-08-28 09:05:12 -03:00

31 lines
774 B
Go

package editor
import (
"fmt"
"os"
"os/exec"
"strings"
)
const defaultEditor = "nano"
// Cmd returns a *exec.Cmd editing the given path with $EDITOR or nano if no
// $EDITOR is set.
func Cmd(path string) (*exec.Cmd, error) {
if os.Getenv("SNAP_REVISION") != "" {
return nil, fmt.Errorf("Did you install with Snap? Glow is sandboxed and unable to open an editor. Please install Glow with Go or another package manager to enable editing.")
}
editor, args := getEditor()
return exec.Command(editor, append(args, path)...), nil
}
func getEditor() (string, []string) {
editor := strings.Fields(os.Getenv("EDITOR"))
if len(editor) > 1 {
return editor[0], editor[1:]
}
if len(editor) == 1 {
return editor[0], []string{}
}
return defaultEditor, []string{}
}