Invoke the shell EDITOR like kubectl edit does.
This code is lifted from kubectl. Thanks lads.
Why not import the kubectl code directly? It has tons of dependencies that are not needed
for this simple task. The codebase has been edited to import fewer packages,
and those that are imported are mostly copied to the /pkg dir.
Import via the normal mechanism.
go get -u github.com/neilotoole/shelleditorNote that because shellescape supports the stdlib slog logger,
so it requires Go 1.21 or greater.
There's an example program in cmd/shelleditor.
$ go install github.com/neilotoole/shelleditor/cmd/shelleditor
$ shelleditor hello.txtIt's very simple:
package main
import (
"fmt"
"log/slog"
"os"
"github.com/neilotoole/shelleditor"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "Usage: shelleditor PATH")
os.Exit(1)
}
// Set logger... you can usually ignore this. When not
// set, log output is discarded.
shelleditor.SetLogger(slog.Default())
ed := shelleditor.NewDefaultEditor("EDITOR")
if err := ed.Launch(os.Args[1]); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}