Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/neilotoole/shelleditor

Invoke the shell EDITOR like 'kubectl edit' does
https://github.com/neilotoole/shelleditor

editor go golang kubectl nano shell textpad vi vim

Last synced: 8 days ago
JSON representation

Invoke the shell EDITOR like 'kubectl edit' does

Awesome Lists containing this project

README

        

# shelleditor
Invoke the shell `EDITOR` like `kubectl edit` does.

This code is lifted from [kubectl](https://github.com/kubernetes/kubectl/tree/master/pkg/cmd/util/editor).
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.

## Usage

Import via the normal mechanism.

```shell
go get -u github.com/neilotoole/shelleditor
```

Note that because `shellescape` supports the stdlib `slog` logger,
so it requires Go 1.21 or greater.

## Example program

There's an example program in [`cmd/shelleditor`](https://github.com/neilotoole/shelleditor/blob/master/cmd/shelleditor/main.go).

```shell
$ go install github.com/neilotoole/shelleditor/cmd/shelleditor
$ shelleditor hello.txt
```

It's very simple:

```go
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)
}
}
```

## Alternatives

- [th/go-editor](https://github.com/tj/go-editor)