Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/briancain/editor-cli
- Owner: briancain
- License: mit
- Created: 2023-11-27T16:54:09.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2023-11-27T19:18:56.000Z (12 months ago)
- Last Synced: 2024-10-04T19:43:46.746Z (about 1 month ago)
- Topics: cli, edit, editor, go, golang, package, utility
- Language: Go
- Homepage:
- Size: 6.7 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
```