get listing working
This commit is contained in:
parent
59b46d4902
commit
ccf39d829a
6 changed files with 680 additions and 11 deletions
|
|
@ -2,3 +2,4 @@ package gemlog
|
|||
|
||||
type Notification string
|
||||
type ErrorMsg struct{ err error }
|
||||
type GemLogsLoaded struct{ Logs []GemlogListEntry }
|
||||
|
|
|
|||
20
gemlog/db.go
20
gemlog/db.go
|
|
@ -6,7 +6,9 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func genBasicAuthHeader(user, password string) string {
|
||||
|
|
@ -14,7 +16,8 @@ func genBasicAuthHeader(user, password string) string {
|
|||
return fmt.Sprintf("Basic %s", auth)
|
||||
}
|
||||
|
||||
func ListGemLogs(config *Config) ([]GemlogListEntry, error) {
|
||||
func listGemLogs(config *Config) ([]GemlogListEntry, error) {
|
||||
slog.Info("Listing gemlogs from couchdb")
|
||||
url := fmt.Sprintf("%s:%d/gemlog/_design/gemlog-cli/_view/list", config.CouchDB.Host, config.CouchDB.Port)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
|
|
@ -46,9 +49,11 @@ func ListGemLogs(config *Config) ([]GemlogListEntry, error) {
|
|||
ID string `json:"id"`
|
||||
Key string `json:"key"`
|
||||
Value struct {
|
||||
Rev string `json:"rev"`
|
||||
Title string `json:"title"`
|
||||
Slug string `json:"slug"`
|
||||
Date time.Time `json:"date"`
|
||||
ID string `json:"id"`
|
||||
} `json:"value"`
|
||||
Doc GemlogListEntry `json:"doc"`
|
||||
} `json:"rows"`
|
||||
}
|
||||
|
||||
|
|
@ -59,9 +64,16 @@ func ListGemLogs(config *Config) ([]GemlogListEntry, error) {
|
|||
// Extract entries from response
|
||||
entries := make([]GemlogListEntry, 0, len(couchResponse.Rows))
|
||||
for _, row := range couchResponse.Rows {
|
||||
entries = append(entries, row.Doc)
|
||||
entry := GemlogListEntry{
|
||||
ID: row.ID,
|
||||
Title: row.Value.Title,
|
||||
Slug: row.Value.Slug,
|
||||
Date: row.Value.Date,
|
||||
}
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
|
||||
slog.Info("Found gemlogs", "count", len(entries))
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
18
gemlog/list.go
Normal file
18
gemlog/list.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package gemlog
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func LoadGemlogCMD(config *Config) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
logs, err := listGemLogs(config)
|
||||
slog.Info("Loaded gemlogs", "count", len(logs))
|
||||
if err != nil {
|
||||
return ErrorMsg{err}
|
||||
}
|
||||
return GemLogsLoaded{logs}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue