47 lines
995 B
Go
47 lines
995 B
Go
package gemlog
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
// WriteAction opens a shell editor and prints the output to console
|
|
func WriteAction() error {
|
|
// Get the editor from environment variable, default to vi
|
|
editor := os.Getenv("EDITOR")
|
|
if editor == "" {
|
|
editor = "vim"
|
|
}
|
|
|
|
// Create a temporary file
|
|
tmpFile, err := os.CreateTemp("", "gemlog-*.md")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create temporary file: %w", err)
|
|
}
|
|
defer os.Remove(tmpFile.Name()) // Clean up
|
|
|
|
tmpFile.Close()
|
|
|
|
// Open the editor with the temporary file
|
|
cmd := exec.Command(editor, tmpFile.Name())
|
|
cmd.Stdin = os.Stdin
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err = cmd.Run()
|
|
if err != nil {
|
|
return fmt.Errorf("editor command failed: %w", err)
|
|
}
|
|
|
|
// Read the contents of the file
|
|
content, err := os.ReadFile(tmpFile.Name())
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read file contents: %w", err)
|
|
}
|
|
|
|
// Print the contents to console
|
|
fmt.Print(string(content))
|
|
|
|
return nil
|
|
}
|