Compare commits
No commits in common. "0048f4a7ceb6b502f821ea24cee0562c95efa7ee" and "972707e5fdbb322a9b442e78fe1de66f173304cb" have entirely different histories.
0048f4a7ce
...
972707e5fd
12 changed files with 126 additions and 202 deletions
|
|
@ -1,5 +1,13 @@
|
||||||
package gemlog
|
package gemlog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
// CouchDBConfig represents the CouchDB configuration section
|
// CouchDBConfig represents the CouchDB configuration section
|
||||||
type CouchDBConfig struct {
|
type CouchDBConfig struct {
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
|
|
@ -12,3 +20,25 @@ type CouchDBConfig struct {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
CouchDB CouchDBConfig `yaml:"couchdb"`
|
CouchDB CouchDBConfig `yaml:"couchdb"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadConfig reads and parses the YAML configuration file from ~/.config/gemlog-cli
|
||||||
|
func LoadConfig() (*Config, error) {
|
||||||
|
homeDir, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get user home directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
configPath := filepath.Join(homeDir, ".config", "gemlog-cli", "config.yml")
|
||||||
|
|
||||||
|
data, err := os.ReadFile(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read config file %s: %w", configPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var config Config
|
||||||
|
if err := yaml.Unmarshal(data, &config); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse YAML config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &config, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,21 @@
|
||||||
package gemlog
|
package gemlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GemlogEntry represents a single gemlog post entry
|
// GemlogEntry represents a single gemlog post entry
|
||||||
type GemlogEntry struct {
|
type GemlogEntry struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
Date time.Time `json:"date"`
|
Date time.Time `json:"date"`
|
||||||
// Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
Gemtxt string `json:"gemtxt"`
|
Gemtxt string `json:"gemtxt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GemlogListEntry is for showing a list of gemlog entries without the main Gemtxt
|
|
||||||
type GemlogListEntry struct {
|
type GemlogListEntry struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
|
|
@ -21,3 +23,25 @@ type GemlogListEntry struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Rev string `json:"rev"`
|
Rev string `json:"rev"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewUUID generates a new UUID v4 using crypto/rand
|
||||||
|
func NewUUID() (string, error) {
|
||||||
|
uuid := make([]byte, 16)
|
||||||
|
_, err := rand.Read(uuid)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to generate UUID: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set version (4) and variant bits according to RFC 4122
|
||||||
|
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
|
||||||
|
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant bits
|
||||||
|
|
||||||
|
// Format as standard UUID string: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
||||||
|
return fmt.Sprintf("%s-%s-%s-%s-%s",
|
||||||
|
hex.EncodeToString(uuid[0:4]),
|
||||||
|
hex.EncodeToString(uuid[4:6]),
|
||||||
|
hex.EncodeToString(uuid[6:8]),
|
||||||
|
hex.EncodeToString(uuid[8:10]),
|
||||||
|
hex.EncodeToString(uuid[10:16]),
|
||||||
|
), nil
|
||||||
|
}
|
||||||
|
|
|
||||||
38
gemlog/db.go
38
gemlog/db.go
|
|
@ -119,7 +119,7 @@ func ReadGemlogEntry(config *Config, id string) (GemlogEntry, error) {
|
||||||
Slug: rawData.Slug,
|
Slug: rawData.Slug,
|
||||||
Date: rawData.Date,
|
Date: rawData.Date,
|
||||||
Gemtxt: rawData.GemText,
|
Gemtxt: rawData.GemText,
|
||||||
// Tags: make([]string, 0),
|
Tags: make([]string, 0),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -151,42 +151,6 @@ func DeleteGemlogEntry(config *Config, id string, rev string) error {
|
||||||
return nil
|
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 {
|
func SaveGemlogEntry(config *Config, entry *GemlogEntry) error {
|
||||||
url := fmt.Sprintf("%s:%d/gemlog/", config.CouchDB.Host, config.CouchDB.Port)
|
url := fmt.Sprintf("%s:%d/gemlog/", config.CouchDB.Host, config.CouchDB.Port)
|
||||||
|
|
||||||
|
|
|
||||||
1
go.mod
1
go.mod
|
|
@ -6,7 +6,6 @@ require (
|
||||||
github.com/charmbracelet/bubbles v0.21.0
|
github.com/charmbracelet/bubbles v0.21.0
|
||||||
github.com/charmbracelet/bubbletea v1.3.10
|
github.com/charmbracelet/bubbletea v1.3.10
|
||||||
github.com/charmbracelet/lipgloss v1.1.0
|
github.com/charmbracelet/lipgloss v1.1.0
|
||||||
github.com/muesli/reflow v0.3.0
|
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
4
go.sum
4
go.sum
|
|
@ -22,18 +22,14 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
|
||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
|
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
|
||||||
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
|
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
|
||||||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
|
||||||
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
|
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
|
||||||
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
|
||||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
|
||||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||||
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
|
|
||||||
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
|
|
||||||
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
||||||
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
|
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
|
||||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
|
||||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
|
|
|
||||||
|
|
@ -118,9 +118,9 @@ func (m model) View() string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
var enableLogs bool = true
|
var enableLogs bool = false
|
||||||
|
|
||||||
func Run(config *gemlog.Config) {
|
func Run() {
|
||||||
if enableLogs {
|
if enableLogs {
|
||||||
f, err := tea.LogToFile("debug.log", "debug")
|
f, err := tea.LogToFile("debug.log", "debug")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -133,7 +133,12 @@ func Run(config *gemlog.Config) {
|
||||||
slog.SetDefault(logger)
|
slog.SetDefault(logger)
|
||||||
}
|
}
|
||||||
slog.Info("Starting gemlog cli")
|
slog.Info("Starting gemlog cli")
|
||||||
err := gemlog.CheckDBConnection(config)
|
config, err := gemlog.LoadConfig()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error loading config: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
err = gemlog.CheckDBConnection(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error checking db connection: %v", err)
|
fmt.Printf("Error checking db connection: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
gemlog "git.travisshears.com/travisshears/gemlog-cli/gemlog"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
|
||||||
|
|
||||||
// LoadConfig reads and parses the YAML configuration file from ~/.config/gemlog-cli
|
|
||||||
func LoadConfig() (*gemlog.Config, error) {
|
|
||||||
homeDir, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get user home directory: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
configPath := filepath.Join(homeDir, ".config", "gemlog-cli", "config.yml")
|
|
||||||
|
|
||||||
data, err := os.ReadFile(configPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to read config file %s: %w", configPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var config gemlog.Config
|
|
||||||
if err := yaml.Unmarshal(data, &config); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse YAML config: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &config, nil
|
|
||||||
}
|
|
||||||
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"github.com/charmbracelet/bubbles/viewport"
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/muesli/reflow/wordwrap"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type EntryPageModel struct {
|
type EntryPageModel struct {
|
||||||
|
|
@ -21,8 +20,13 @@ func initialEntryPageModel() EntryPageModel {
|
||||||
return EntryPageModel{}
|
return EntryPageModel{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func wrapGemtxt(gemtxt string, width int) string {
|
func transformEntryToContent(entry gemlog.GemlogEntry) string {
|
||||||
return wordwrap.String(gemtxt, width)
|
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) {
|
func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPageModel, tea.Cmd) {
|
||||||
|
|
@ -33,7 +37,7 @@ func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPag
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case GemLogLoaded:
|
case GemLogLoaded:
|
||||||
m.entry = msg.Log
|
m.entry = msg.Log
|
||||||
m.viewport.SetContent(wrapGemtxt(m.entry.Gemtxt, m.viewport.Width))
|
m.viewport.SetContent(transformEntryToContent(m.entry))
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
if !active {
|
if !active {
|
||||||
return m, nil
|
return m, nil
|
||||||
|
|
@ -46,7 +50,7 @@ func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPag
|
||||||
return m, cmd
|
return m, cmd
|
||||||
}
|
}
|
||||||
case tea.WindowSizeMsg:
|
case tea.WindowSizeMsg:
|
||||||
headerHeight := lipgloss.Height(m.headerView("loading slug..."))
|
headerHeight := lipgloss.Height(m.headerView())
|
||||||
footerHeight := lipgloss.Height(m.footerView())
|
footerHeight := lipgloss.Height(m.footerView())
|
||||||
verticalMarginHeight := headerHeight + footerHeight
|
verticalMarginHeight := headerHeight + footerHeight
|
||||||
|
|
||||||
|
|
@ -59,7 +63,7 @@ func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPag
|
||||||
|
|
||||||
m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)
|
m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)
|
||||||
m.viewport.YPosition = headerHeight
|
m.viewport.YPosition = headerHeight
|
||||||
m.viewport.SetContent(wrapGemtxt(m.entry.Gemtxt, msg.Width))
|
m.viewport.SetContent(transformEntryToContent(m.entry))
|
||||||
m.ready = true
|
m.ready = true
|
||||||
} else {
|
} else {
|
||||||
m.viewport.Width = msg.Width
|
m.viewport.Width = msg.Width
|
||||||
|
|
@ -79,7 +83,7 @@ func (m EntryPageModel) View() string {
|
||||||
return "\n Initializing..."
|
return "\n Initializing..."
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("%s\n%s\n%s", m.headerView(m.entry.Slug), m.viewport.View(), m.footerView())
|
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView())
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -96,8 +100,8 @@ var (
|
||||||
}()
|
}()
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m EntryPageModel) headerView(slug string) string {
|
func (m EntryPageModel) headerView() string {
|
||||||
title := titleStyle.Render(slug)
|
title := titleStyle.Render("Mr. Pager")
|
||||||
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title)))
|
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title)))
|
||||||
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
|
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,12 +52,7 @@ func (m EntryListPageModel) Update(msg tea.Msg, active bool, ctx *context) (Entr
|
||||||
id := m.entries[m.cursor].ID
|
id := m.entries[m.cursor].ID
|
||||||
rev := m.entries[m.cursor].Rev
|
rev := m.entries[m.cursor].Rev
|
||||||
switch m.actionToTake {
|
switch m.actionToTake {
|
||||||
case Edit:
|
// TODO: handle edit
|
||||||
editCmd := EditPostCMD(ctx.config, id, rev)
|
|
||||||
navCmd := func() tea.Msg {
|
|
||||||
return SwitchPages{Page: ActionList}
|
|
||||||
}
|
|
||||||
return m, tea.Sequence(editCmd, navCmd)
|
|
||||||
case Read:
|
case Read:
|
||||||
loadCmd := LoadGemlogCMD(ctx.config, id)
|
loadCmd := LoadGemlogCMD(ctx.config, id)
|
||||||
navCmd := func() tea.Msg {
|
navCmd := func() tea.Msg {
|
||||||
|
|
|
||||||
|
|
@ -12,84 +12,8 @@ import (
|
||||||
"gopkg.in/yaml.v3"
|
"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
|
// TODO: add edit command
|
||||||
func EditPostCMD(config *gemlog.Config, id string, rev string) tea.Cmd {
|
// func EditPostCMD(config *gemlog.Config) 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 {
|
func WritePostCMD(config *gemlog.Config) tea.Cmd {
|
||||||
return func() tea.Msg {
|
return func() tea.Msg {
|
||||||
|
|
@ -99,7 +23,15 @@ func WritePostCMD(config *gemlog.Config) tea.Cmd {
|
||||||
return ErrorMsg{fmt.Errorf("failed to create temporary file: %w", err)}
|
return ErrorMsg{fmt.Errorf("failed to create temporary file: %w", err)}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := tmpFile.Write([]byte(defaultTemplate)); err != nil {
|
// 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 {
|
||||||
tmpFile.Close()
|
tmpFile.Close()
|
||||||
os.Remove(tmpFile.Name())
|
os.Remove(tmpFile.Name())
|
||||||
return ErrorMsg{fmt.Errorf("failed to write initial content: %w", err)}
|
return ErrorMsg{fmt.Errorf("failed to write initial content: %w", err)}
|
||||||
|
|
@ -167,7 +99,12 @@ func parsePost(post string) (gemlog.GemlogEntry, error) {
|
||||||
// Parse frontmatter YAML
|
// Parse frontmatter YAML
|
||||||
frontmatterYAML := strings.Join(frontmatterLines, "\n")
|
frontmatterYAML := strings.Join(frontmatterLines, "\n")
|
||||||
|
|
||||||
var frontmatter Frontmatter
|
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 {
|
if err := yaml.Unmarshal([]byte(frontmatterYAML), &frontmatter); err != nil {
|
||||||
return gemlog.GemlogEntry{}, fmt.Errorf("failed to parse frontmatter: %w", err)
|
return gemlog.GemlogEntry{}, fmt.Errorf("failed to parse frontmatter: %w", err)
|
||||||
|
|
@ -180,25 +117,25 @@ func parsePost(post string) (gemlog.GemlogEntry, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse tags (comma-separated)
|
// Parse tags (comma-separated)
|
||||||
// var tags []string
|
var tags []string
|
||||||
// if frontmatter.Tags != "" {
|
if frontmatter.Tags != "" {
|
||||||
// tagParts := strings.Split(frontmatter.Tags, ",")
|
tagParts := strings.Split(frontmatter.Tags, ",")
|
||||||
// for _, tag := range tagParts {
|
for _, tag := range tagParts {
|
||||||
// trimmed := strings.TrimSpace(tag)
|
trimmed := strings.TrimSpace(tag)
|
||||||
// if trimmed != "" {
|
if trimmed != "" {
|
||||||
// tags = append(tags, trimmed)
|
tags = append(tags, trimmed)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// Join body lines
|
// Join body lines
|
||||||
body := strings.Join(bodyLines, "\n")
|
body := strings.Join(bodyLines, "\n")
|
||||||
|
|
||||||
return gemlog.GemlogEntry{
|
return gemlog.GemlogEntry{
|
||||||
Title: frontmatter.Title,
|
Title: frontmatter.Title,
|
||||||
Date: date,
|
Date: date,
|
||||||
Slug: frontmatter.Slug,
|
Slug: frontmatter.Slug,
|
||||||
// Tags: tags,
|
Tags: tags,
|
||||||
Gemtxt: body,
|
Gemtxt: body,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
main.go
12
main.go
|
|
@ -1,19 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
ui "git.travisshears.com/travisshears/gemlog-cli/internal/ui"
|
ui "git.travisshears.com/travisshears/gemlog-cli/internal/ui"
|
||||||
config "git.travisshears.com/travisshears/gemlog-cli/internal/ui/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
c, err := config.LoadConfig()
|
ui.Run()
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error loading config: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
ui.Run(c)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
templates/default_post.gmi
Normal file
12
templates/default_post.gmi
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
title: todo
|
||||||
|
date: 2025-12-31
|
||||||
|
slug: todo
|
||||||
|
tags: cat, dog
|
||||||
|
---
|
||||||
|
|
||||||
|
Example text
|
||||||
|
|
||||||
|
=> https://travisshears.com example link
|
||||||
|
|
||||||
|
* Example list item 1
|
||||||
|
* Example list item 2
|
||||||
Loading…
Add table
Add a link
Reference in a new issue