diff --git a/README.md b/README.md index f16ae02..5ee1758 100644 --- a/README.md +++ b/README.md @@ -15,43 +15,23 @@ default if no env var is found, it will use `vim` to edit the requested file. This repo is meant to be used as a package for other Golang CLI projects. -The example below is a shortened version of `main.go`: +It has two main functions you can use as a CLI author for editing files: -```golang -// Read the original contents of the file -contents, err := os.ReadFile(filePathToEdit) -if err != nil { - fmt.Println("File reading error: ", err) - os.Exit(1) -} +* 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 // 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) } - -// You could also send this edited content back to a server to update it. - -// If changes, overwrite the original existing file -file, err := os.OpenFile(filePathToEdit, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) -if err != nil { - fmt.Println("Failed to open original file to update: ", err) - os.Exit(1) -} -defer file.Close() - -// Write the content to the file -estr := string(edited) -_, err = file.WriteString(estr) -if err != nil { - fmt.Println("Failed to write content to file: ", err) - os.Exit(1) -} - -fmt.Println(fmt.Sprintf("Successfully updated %q!", filePathToEdit)) -os.Exit(0) ``` ### Examples diff --git a/editor.go b/editor.go index 85b09ac..1a6522a 100644 --- a/editor.go +++ b/editor.go @@ -43,12 +43,13 @@ func setupDefaultEditorArgs() ([]string, error) { // RunLocal is like Run, but assumes the file to edit is locally saved on // disk rather than some remote content -func RunLocal(o []byte, of string) error { +func RunLocal(of string) error { contents, err := os.ReadFile(of) if err != nil { return err } + // Call run on the local file, given its contents edited, _, err := Run(contents, of) if err != nil { return err