get callbacks working

This commit is contained in:
Travis Shears 2025-09-30 11:37:40 +02:00
parent b24cc4d427
commit 0c617c8185
3 changed files with 82 additions and 12 deletions

View file

@ -0,0 +1 @@
package gemlog

View file

@ -1 +1,47 @@
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
}