Git Product home page Git Product logo

go-editor's Introduction

go-editor

Build Status Release codecov GoReportCard GoDoc

Allow your CLI users to edit arbitrary data in their preferred editor.

Just like editing messages in git commit or resources with kubectl edit.

Install

go get github.com/codyaray/go-editor

Usage

Existing File

The most basic usage is to prompt the user to edit an existing file. This may be useful to edit the application configuration or a system file, for example.

edit := editor.NewEditor()
err := edit.Launch("/etc/bashrc")

Arbitrary Data

Most of the time, the data you want your user to edit isn't in an local file. In these cases, if you can represent your data in a human editable format (txt, yaml, hcl, json, etc), then go-editor will enable the user to edit it.

Provide any io.Reader with the initial contents:

original := bytes.NewBufferString("something to be edited\n")

edit := editor.NewEditor()
edited, path, err := edit.LaunchTempFile("example", original)
defer os.Remove(path)
if err != nil {
    // handle it
}

The library leaves it up to you to cleanup the temp file.

This enables your CLI to validate the edited data and prompt the user to continue editing where they left off, rather than starting over. And if that's what you want...

Input Validation

If you would like to validate the edited data, use a ValidatingEditor instead. This will prompt the user to continue editing until validation succeeds or the edit is cancelled.

Simply create a schema and pass it to the editor:

schema := &mySchema{}
edit := editor.NewValidatingEditor(schema)

A schema is any object that implements the Schema interface. This interface has a single method, ValidateBytes([]byte) error.

You can see working examples in the examples directory.

Happy editing!

Acknowledgements

Thanks to these other projects and groups for pointing the way.

go-editor's People

Contributors

codyaray avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.