Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/briancain/editor-cli

A simple Editor package to edit remote and local content, used by Golang CLIs
https://github.com/briancain/editor-cli

cli edit editor go golang package utility

Last synced: 13 days ago
JSON representation

A simple Editor package to edit remote and local content, used by Golang CLIs

Awesome Lists containing this project

README

        

# Editor CLI

A simple editor package for Golang CLIs. Similar to `kubectl patch`. Edit
content from a remote server, or content locally on disk, and overwrite the
results using your favorite editor.

![](img/example-edit.gif)

## Configuration

This package will respect the `$EDITOR` environment variable when launched. By
default if no env var is found, it will use `vim` to edit the requested file.

## Usage

This repo is meant to be used as a package for other Golang CLI projects.

It has two main functions you can use as a CLI author for editing files:

* Run(originalContent []byte, originalFilePath string)
+ `originalContent` is the content of the text to edit in bytes
+ `originalFilePath` is where the file originated, can be empty if remote. Used to determine the file ext for the tmp file.
* RunLocal(originalFilePath string)
+ `originalFilePath` is where the file originated, can be empty if remote. Used to determine the file ext for the tmp file.

The example below is a shortened version of `main.go` using the Run version:

```golang
contents, err := client.GetConfig(ctx, ...)
if err != nil { ... }

// Run the editor to let the user edit the contents in a tmp file
edited, _, err := Run(contents, filePathToEdit)
if err != nil {
fmt.Println("File editing error: ", err)
os.Exit(1)
}

// Take the locally edited content and send it back
if err := client.SetConfig(ctx, edited); err != nil {
...
}
```

### Examples

Use the example CLI to see how this package can work for you:

```shell
# Build the CLI with the Makefile in this repo
brian@localghost:editor-cli λ make
# Run the CLI against some examples
brian@localghost:editor-cli λ ./bin/editor-cli --file examples/test.txt
```