get updating posts working
This commit is contained in:
parent
972707e5fd
commit
6b8ba76018
6 changed files with 148 additions and 53 deletions
|
|
@ -9,11 +9,11 @@ import (
|
|||
|
||||
// GemlogEntry represents a single gemlog post entry
|
||||
type GemlogEntry struct {
|
||||
Title string `json:"title"`
|
||||
Slug string `json:"slug"`
|
||||
Date time.Time `json:"date"`
|
||||
Tags []string `json:"tags"`
|
||||
Gemtxt string `json:"gemtxt"`
|
||||
Title string `json:"title"`
|
||||
Slug string `json:"slug"`
|
||||
Date time.Time `json:"date"`
|
||||
// Tags []string `json:"tags"`
|
||||
Gemtxt string `json:"gemtxt"`
|
||||
}
|
||||
|
||||
type GemlogListEntry struct {
|
||||
|
|
|
|||
38
gemlog/db.go
38
gemlog/db.go
|
|
@ -119,7 +119,7 @@ func ReadGemlogEntry(config *Config, id string) (GemlogEntry, error) {
|
|||
Slug: rawData.Slug,
|
||||
Date: rawData.Date,
|
||||
Gemtxt: rawData.GemText,
|
||||
Tags: make([]string, 0),
|
||||
// Tags: make([]string, 0),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -151,6 +151,42 @@ func DeleteGemlogEntry(config *Config, id string, rev string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func UpdateGemlogEntry(config *Config, entry *GemlogEntry, id string, rev string) error {
|
||||
url := fmt.Sprintf("%s:%d/gemlog/%s?rev=%s", config.CouchDB.Host, config.CouchDB.Port, id, rev)
|
||||
|
||||
// Marshal the entry struct to JSON
|
||||
jsonData, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal entry: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Add("authorization", genBasicAuthHeader(config.CouchDB.User, config.CouchDB.Password))
|
||||
req.Header.Add("content-type", "application/json")
|
||||
slog.Info("Sending request to update gemlog entry", "url", url, "data", string(jsonData))
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send request: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||
return fmt.Errorf("unexpected status code %d: %s", res.StatusCode, string(body))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveGemlogEntry(config *Config, entry *GemlogEntry) error {
|
||||
url := fmt.Sprintf("%s:%d/gemlog/", config.CouchDB.Host, config.CouchDB.Port)
|
||||
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ func (m model) View() string {
|
|||
return s
|
||||
}
|
||||
|
||||
var enableLogs bool = false
|
||||
var enableLogs bool = true
|
||||
|
||||
func Run() {
|
||||
if enableLogs {
|
||||
|
|
|
|||
|
|
@ -20,15 +20,6 @@ func initialEntryPageModel() EntryPageModel {
|
|||
return EntryPageModel{}
|
||||
}
|
||||
|
||||
func transformEntryToContent(entry gemlog.GemlogEntry) string {
|
||||
content := entry.Slug
|
||||
content += "\n\n"
|
||||
content += entry.Gemtxt
|
||||
content += "\n\n-------------------------\n"
|
||||
content += "\n\nPress h or left arrow to go back"
|
||||
return content
|
||||
}
|
||||
|
||||
func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPageModel, tea.Cmd) {
|
||||
var (
|
||||
cmd tea.Cmd
|
||||
|
|
@ -37,7 +28,7 @@ func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPag
|
|||
switch msg := msg.(type) {
|
||||
case GemLogLoaded:
|
||||
m.entry = msg.Log
|
||||
m.viewport.SetContent(transformEntryToContent(m.entry))
|
||||
m.viewport.SetContent(m.entry.Gemtxt)
|
||||
case tea.KeyMsg:
|
||||
if !active {
|
||||
return m, nil
|
||||
|
|
@ -50,7 +41,7 @@ func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPag
|
|||
return m, cmd
|
||||
}
|
||||
case tea.WindowSizeMsg:
|
||||
headerHeight := lipgloss.Height(m.headerView())
|
||||
headerHeight := lipgloss.Height(m.headerView("loading slug..."))
|
||||
footerHeight := lipgloss.Height(m.footerView())
|
||||
verticalMarginHeight := headerHeight + footerHeight
|
||||
|
||||
|
|
@ -63,7 +54,7 @@ func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPag
|
|||
|
||||
m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)
|
||||
m.viewport.YPosition = headerHeight
|
||||
m.viewport.SetContent(transformEntryToContent(m.entry))
|
||||
m.viewport.SetContent(m.entry.Gemtxt)
|
||||
m.ready = true
|
||||
} else {
|
||||
m.viewport.Width = msg.Width
|
||||
|
|
@ -83,7 +74,7 @@ func (m EntryPageModel) View() string {
|
|||
return "\n Initializing..."
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView())
|
||||
return fmt.Sprintf("%s\n%s\n%s", m.headerView(m.entry.Slug), m.viewport.View(), m.footerView())
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -100,8 +91,8 @@ var (
|
|||
}()
|
||||
)
|
||||
|
||||
func (m EntryPageModel) headerView() string {
|
||||
title := titleStyle.Render("Mr. Pager")
|
||||
func (m EntryPageModel) headerView(slug string) string {
|
||||
title := titleStyle.Render(slug)
|
||||
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title)))
|
||||
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,12 @@ func (m EntryListPageModel) Update(msg tea.Msg, active bool, ctx *context) (Entr
|
|||
id := m.entries[m.cursor].ID
|
||||
rev := m.entries[m.cursor].Rev
|
||||
switch m.actionToTake {
|
||||
// TODO: handle edit
|
||||
case Edit:
|
||||
editCmd := EditPostCMD(ctx.config, id, rev)
|
||||
navCmd := func() tea.Msg {
|
||||
return SwitchPages{Page: ActionList}
|
||||
}
|
||||
return m, tea.Sequence(editCmd, navCmd)
|
||||
case Read:
|
||||
loadCmd := LoadGemlogCMD(ctx.config, id)
|
||||
navCmd := func() tea.Msg {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue