get write action working and entries saving to db
This commit is contained in:
parent
472c1b36a7
commit
09fb974bd2
5 changed files with 136 additions and 257 deletions
102
gemlog/write.go
102
gemlog/write.go
|
|
@ -4,11 +4,14 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func WritePostCMD() tea.Cmd {
|
||||
func WritePostCMD(config *Config) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
// Create a temporary file
|
||||
tmpFile, err := os.CreateTemp("/tmp", "gemlog-*.md")
|
||||
|
|
@ -54,50 +57,81 @@ func WritePostCMD() tea.Cmd {
|
|||
return ErrorMsg{fmt.Errorf("failed to read file contents: %w", readErr)}
|
||||
}
|
||||
|
||||
gemlogEntry, err := parsePost(string(content))
|
||||
if err != nil {
|
||||
return ErrorMsg{fmt.Errorf("failed to parse post: %w", err)}
|
||||
}
|
||||
if err := SaveGemlogEntry(config, &gemlogEntry); err != nil {
|
||||
return ErrorMsg{fmt.Errorf("failed to save gemlog entry: %w", err)}
|
||||
}
|
||||
|
||||
// Return success with the content
|
||||
return Notification(fmt.Sprintf("Post created with content: %s", string(content)))
|
||||
return Notification(fmt.Sprintf("Post created: \ngemini://travisshears.com/gemlog/%s\n\n", gemlogEntry.Slug))
|
||||
})()
|
||||
}
|
||||
}
|
||||
|
||||
// For messages that contain errors it's often handy to also implement the
|
||||
// error interface on the message.
|
||||
// func (e ErrorMsg) Error() string { return e.err.Error() }
|
||||
func parsePost(post string) (GemlogEntry, error) {
|
||||
// split post on new lines
|
||||
lines := strings.Split(post, "\n")
|
||||
|
||||
// WriteAction opens a shell editor and prints the output to console
|
||||
func write() (string, error) {
|
||||
// Get the editor from environment variable, default to vi
|
||||
editor := os.Getenv("EDITOR")
|
||||
if editor == "" {
|
||||
editor = "vim"
|
||||
// Find the separator line "---"
|
||||
separatorIndex := -1
|
||||
for i, line := range lines {
|
||||
if strings.TrimSpace(line) == "---" {
|
||||
separatorIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Create a temporary file
|
||||
tmpFile, err := os.CreateTemp("", "gemlog-*.md")
|
||||
if separatorIndex == -1 {
|
||||
return GemlogEntry{}, fmt.Errorf("no frontmatter separator '---' found")
|
||||
}
|
||||
|
||||
// Extract frontmatter and body
|
||||
frontmatterLines := lines[:separatorIndex]
|
||||
bodyLines := lines[separatorIndex+1:]
|
||||
|
||||
// Parse frontmatter YAML
|
||||
frontmatterYAML := strings.Join(frontmatterLines, "\n")
|
||||
|
||||
var frontmatter struct {
|
||||
Title string `yaml:"title"`
|
||||
Date string `yaml:"date"`
|
||||
Slug string `yaml:"slug"`
|
||||
Tags string `yaml:"tags"`
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal([]byte(frontmatterYAML), &frontmatter); err != nil {
|
||||
return GemlogEntry{}, fmt.Errorf("failed to parse frontmatter: %w", err)
|
||||
}
|
||||
|
||||
// Parse date
|
||||
date, err := time.Parse("2006-01-02", strings.TrimSpace(frontmatter.Date))
|
||||
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)
|
||||
return GemlogEntry{}, fmt.Errorf("failed to parse date: %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)
|
||||
// Parse tags (comma-separated)
|
||||
var tags []string
|
||||
if frontmatter.Tags != "" {
|
||||
tagParts := strings.Split(frontmatter.Tags, ",")
|
||||
for _, tag := range tagParts {
|
||||
trimmed := strings.TrimSpace(tag)
|
||||
if trimmed != "" {
|
||||
tags = append(tags, trimmed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
contentStr := string(content)
|
||||
return contentStr, nil
|
||||
// Join body lines
|
||||
body := strings.Join(bodyLines, "\n")
|
||||
|
||||
return GemlogEntry{
|
||||
Title: frontmatter.Title,
|
||||
Date: date,
|
||||
Slug: frontmatter.Slug,
|
||||
Tags: tags,
|
||||
Gemtxt: body,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue