get updating posts working

This commit is contained in:
Travis Shears 2025-10-05 14:22:11 +02:00
parent 972707e5fd
commit 6b8ba76018
6 changed files with 148 additions and 53 deletions

View file

@ -12,8 +12,84 @@ import (
"gopkg.in/yaml.v3"
)
type Frontmatter struct {
Title string `yaml:"title"`
Date string `yaml:"date"`
Slug string `yaml:"slug"`
// Tags []string `yaml:"tags"`
}
// TODO: add edit command
// func EditPostCMD(config *gemlog.Config) tea.Cmd {}
func EditPostCMD(config *gemlog.Config, id string, rev string) tea.Cmd {
return func() tea.Msg {
gemlogEntry, err := gemlog.ReadGemlogEntry(config, id)
if err != nil {
return ErrorMsg{fmt.Errorf("failed to read gemlog entry: %w", err)}
}
// Create a temporary file
tmpFile, err := os.CreateTemp("/tmp", "gemlog-*.md")
if err != nil {
return ErrorMsg{fmt.Errorf("failed to create temporary file: %w", err)}
}
frontmatter := Frontmatter{
Title: gemlogEntry.Title,
Date: gemlogEntry.Date.Format("2006-01-02"),
Slug: gemlogEntry.Slug,
// Tags: []string{}, // or nil if empty is ok
}
yamlData, err := yaml.Marshal(&frontmatter)
if err != nil {
return ErrorMsg{fmt.Errorf("failed to marshal frontmatter: %w", err)}
}
initialContent := append(yamlData, []byte("---\n"+gemlogEntry.Gemtxt)...)
if _, err := tmpFile.Write(initialContent); err != nil {
tmpFile.Close()
os.Remove(tmpFile.Name())
return ErrorMsg{fmt.Errorf("failed to write initial content: %w", err)}
}
tmpFile.Close()
// Get the editor from environment variable, default to vim
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vim"
}
// Create the command to open the editor with the temp file
c := exec.Command(editor, tmpFile.Name()) //nolint:gosec
// Return tea.ExecProcess which will suspend the TUI and run the editor
return tea.ExecProcess(c, func(err error) tea.Msg {
defer os.Remove(tmpFile.Name()) // Clean up the temp file
if err != nil {
return ErrorMsg{fmt.Errorf("editor command failed: %w", err)}
}
// Read the contents of the file after editing
content, readErr := os.ReadFile(tmpFile.Name())
if readErr != nil {
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 := gemlog.UpdateGemlogEntry(config, &gemlogEntry, id, rev); err != nil {
return ErrorMsg{fmt.Errorf("failed to save gemlog entry: %w", err)}
}
// Return success with the content
return Notification(fmt.Sprintf("Post updated: \ngemini://travisshears.com/gemlog/%s\n\n", gemlogEntry.Slug))
})()
}
}
func WritePostCMD(config *gemlog.Config) tea.Cmd {
return func() tea.Msg {
@ -23,15 +99,7 @@ func WritePostCMD(config *gemlog.Config) tea.Cmd {
return ErrorMsg{fmt.Errorf("failed to create temporary file: %w", err)}
}
// Load initial content from template file
initialContent, err := os.ReadFile("templates/default_post.gmi")
if err != nil {
tmpFile.Close()
os.Remove(tmpFile.Name())
return ErrorMsg{fmt.Errorf("failed to read template file: %w", err)}
}
if _, err := tmpFile.Write(initialContent); err != nil {
if _, err := tmpFile.Write([]byte(defaultTemplate)); err != nil {
tmpFile.Close()
os.Remove(tmpFile.Name())
return ErrorMsg{fmt.Errorf("failed to write initial content: %w", err)}
@ -99,12 +167,7 @@ func parsePost(post string) (gemlog.GemlogEntry, error) {
// 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"`
}
var frontmatter Frontmatter
if err := yaml.Unmarshal([]byte(frontmatterYAML), &frontmatter); err != nil {
return gemlog.GemlogEntry{}, fmt.Errorf("failed to parse frontmatter: %w", err)
@ -117,25 +180,25 @@ func parsePost(post string) (gemlog.GemlogEntry, error) {
}
// 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)
}
}
}
// 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)
// }
// }
// }
// Join body lines
body := strings.Join(bodyLines, "\n")
return gemlog.GemlogEntry{
Title: frontmatter.Title,
Date: date,
Slug: frontmatter.Slug,
Tags: tags,
Title: frontmatter.Title,
Date: date,
Slug: frontmatter.Slug,
// Tags: tags,
Gemtxt: body,
}, nil
}