Compare commits
No commits in common. "eb2191045ed1b2d418fcab8bbfb2942d9f0cd69f" and "09fb974bd28df20f9178527233b323b1a262d6a6" have entirely different histories.
eb2191045e
...
09fb974bd2
7 changed files with 10 additions and 189 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1 +0,0 @@
|
||||||
debug.log
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
meta {
|
|
||||||
name: List Gemlogs for CLI
|
|
||||||
type: http
|
|
||||||
seq: 3
|
|
||||||
}
|
|
||||||
|
|
||||||
get {
|
|
||||||
url: http://eisenhorn:5023/gemlog/_design/gemlog-cli/_view/list
|
|
||||||
body: json
|
|
||||||
auth: basic
|
|
||||||
}
|
|
||||||
|
|
||||||
auth:basic {
|
|
||||||
username: gemlog-cli
|
|
||||||
password: {{pw}}
|
|
||||||
}
|
|
||||||
|
|
||||||
settings {
|
|
||||||
encodeUrl: true
|
|
||||||
}
|
|
||||||
|
|
@ -2,4 +2,3 @@ package gemlog
|
||||||
|
|
||||||
type Notification string
|
type Notification string
|
||||||
type ErrorMsg struct{ err error }
|
type ErrorMsg struct{ err error }
|
||||||
type GemLogsLoaded struct{ Logs []GemlogListEntry }
|
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,6 @@ type GemlogEntry struct {
|
||||||
Gemtxt string `json:"gemtxt"`
|
Gemtxt string `json:"gemtxt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GemlogListEntry struct {
|
|
||||||
Title string `json:"title"`
|
|
||||||
Slug string `json:"slug"`
|
|
||||||
Date time.Time `json:"date"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewUUID generates a new UUID v4 using crypto/rand
|
// NewUUID generates a new UUID v4 using crypto/rand
|
||||||
func NewUUID() (string, error) {
|
func NewUUID() (string, error) {
|
||||||
uuid := make([]byte, 16)
|
uuid := make([]byte, 16)
|
||||||
|
|
|
||||||
91
gemlog/db.go
91
gemlog/db.go
|
|
@ -6,77 +6,9 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func genBasicAuthHeader(user, password string) string {
|
|
||||||
auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", user, password)))
|
|
||||||
return fmt.Sprintf("Basic %s", auth)
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Add("authorization", genBasicAuthHeader(config.CouchDB.User, config.CouchDB.Password))
|
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to send request: %w", err)
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
|
||||||
return nil, fmt.Errorf("unexpected status code %d: %s", res.StatusCode, string(body))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse CouchDB response
|
|
||||||
var couchResponse struct {
|
|
||||||
TotalRows int `json:"total_rows"`
|
|
||||||
Offset int `json:"offset"`
|
|
||||||
Rows []struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Key string `json:"key"`
|
|
||||||
Value struct {
|
|
||||||
Title string `json:"title"`
|
|
||||||
Slug string `json:"slug"`
|
|
||||||
Date time.Time `json:"date"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
} `json:"value"`
|
|
||||||
} `json:"rows"`
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.Unmarshal(body, &couchResponse); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract entries from response
|
|
||||||
entries := make([]GemlogListEntry, 0, len(couchResponse.Rows))
|
|
||||||
for _, row := range couchResponse.Rows {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
|
@ -91,7 +23,9 @@ func SaveGemlogEntry(config *Config, entry *GemlogEntry) error {
|
||||||
return fmt.Errorf("failed to create request: %w", err)
|
return fmt.Errorf("failed to create request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Add("authorization", genBasicAuthHeader(config.CouchDB.User, config.CouchDB.Password))
|
// Encode username:password for Basic Auth
|
||||||
|
auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", config.CouchDB.User, config.CouchDB.Password)))
|
||||||
|
req.Header.Add("authorization", fmt.Sprintf("Basic %s", auth))
|
||||||
req.Header.Add("content-type", "application/json")
|
req.Header.Add("content-type", "application/json")
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
|
@ -114,22 +48,3 @@ func SaveGemlogEntry(config *Config, entry *GemlogEntry) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckDBConnection(config *Config) error {
|
|
||||||
url := fmt.Sprintf("%s:%d/gemlog/", config.CouchDB.Host, config.CouchDB.Port)
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create request: %w", err)
|
|
||||||
}
|
|
||||||
req.Header.Add("authorization", genBasicAuthHeader(config.CouchDB.User, config.CouchDB.Password))
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to send request: %w", err)
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
|
|
||||||
if res.StatusCode == 200 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return fmt.Errorf("unexpected status code %d", res.StatusCode)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
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}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
61
main.go
61
main.go
|
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gemini_site/gemlog"
|
"gemini_site/gemlog"
|
||||||
"log/slog"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
|
@ -24,26 +23,10 @@ func TODOCmd() tea.Msg {
|
||||||
return gemlog.Notification("This action has not been implemented yet. Try another.")
|
return gemlog.Notification("This action has not been implemented yet. Try another.")
|
||||||
}
|
}
|
||||||
|
|
||||||
type Page string
|
|
||||||
|
|
||||||
const (
|
|
||||||
ActionList Page = "actionList"
|
|
||||||
EntryList Page = "entryList"
|
|
||||||
Entry Page = "entry"
|
|
||||||
)
|
|
||||||
|
|
||||||
type entryListPageModel struct {
|
|
||||||
entries []gemlog.GemlogListEntry
|
|
||||||
action Action
|
|
||||||
}
|
|
||||||
|
|
||||||
type uiState struct {
|
type uiState struct {
|
||||||
notification string
|
notification string
|
||||||
cursor int
|
cursor int
|
||||||
errorTxt string
|
errorTxt string
|
||||||
|
|
||||||
page Page
|
|
||||||
entryListPage entryListPageModel
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type context struct {
|
type context struct {
|
||||||
|
|
@ -58,10 +41,7 @@ type model struct {
|
||||||
func initialModel(config *gemlog.Config) model {
|
func initialModel(config *gemlog.Config) model {
|
||||||
return model{
|
return model{
|
||||||
ui: uiState{
|
ui: uiState{
|
||||||
page: ActionList,
|
// actions: []Action{Write, Read, Edit, Delete},
|
||||||
entryListPage: entryListPageModel{
|
|
||||||
entries: []gemlog.GemlogListEntry{},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
context: &context{
|
context: &context{
|
||||||
config: config,
|
config: config,
|
||||||
|
|
@ -79,9 +59,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
m.ui.errorTxt = fmt.Sprintf("%s\n\n", fmt.Errorf("%s", msg))
|
m.ui.errorTxt = fmt.Sprintf("%s\n\n", fmt.Errorf("%s", msg))
|
||||||
case gemlog.Notification:
|
case gemlog.Notification:
|
||||||
m.ui.notification = fmt.Sprintf("%s\n\n", string(msg))
|
m.ui.notification = fmt.Sprintf("%s\n\n", string(msg))
|
||||||
case gemlog.GemLogsLoaded:
|
|
||||||
m.ui.entryListPage.entries = msg.Logs
|
|
||||||
m.ui.cursor = 0
|
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "ctrl+c", "q":
|
case "ctrl+c", "q":
|
||||||
|
|
@ -100,8 +77,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
case Write:
|
case Write:
|
||||||
return m, gemlog.WritePostCMD(m.context.config)
|
return m, gemlog.WritePostCMD(m.context.config)
|
||||||
case Read:
|
case Read:
|
||||||
m.ui.page = EntryList
|
return m, TODOCmd
|
||||||
return m, gemlog.LoadGemlogCMD(m.context.config)
|
|
||||||
case Edit:
|
case Edit:
|
||||||
return m, TODOCmd
|
return m, TODOCmd
|
||||||
case Delete:
|
case Delete:
|
||||||
|
|
@ -124,26 +100,12 @@ func (m model) View() string {
|
||||||
s += "Welcome to gemlog cli!\n\nWhat post action would you like to take?\n\n"
|
s += "Welcome to gemlog cli!\n\nWhat post action would you like to take?\n\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.ui.page == ActionList {
|
for i, action := range actions {
|
||||||
for i, action := range actions {
|
cursor := " "
|
||||||
cursor := " "
|
if m.ui.cursor == i {
|
||||||
if m.ui.cursor == i {
|
cursor = ">"
|
||||||
cursor = ">"
|
|
||||||
}
|
|
||||||
s += fmt.Sprintf("%s %s\n", cursor, action)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.ui.page == EntryList {
|
|
||||||
slog.Info("rendering entry list", "count", len(m.ui.entryListPage.entries))
|
|
||||||
for i, entry := range m.ui.entryListPage.entries {
|
|
||||||
slog.Info("entry", "value", entry)
|
|
||||||
cursor := " "
|
|
||||||
if m.ui.cursor == i {
|
|
||||||
cursor = ">"
|
|
||||||
}
|
|
||||||
s += fmt.Sprintf("%s %s : %s\n", cursor, entry.Date, entry.Slug)
|
|
||||||
}
|
}
|
||||||
|
s += fmt.Sprintf("%s %s\n", cursor, action)
|
||||||
}
|
}
|
||||||
|
|
||||||
s += "\nPress q to quit.\n"
|
s += "\nPress q to quit.\n"
|
||||||
|
|
@ -152,21 +114,12 @@ func (m model) View() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
f, err := tea.LogToFile("debug.log", "debug")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("fatal:", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
slog.Info("Starting gemlog cli")
|
|
||||||
config, err := gemlog.LoadConfig()
|
config, err := gemlog.LoadConfig()
|
||||||
// gemlog.CheckDBConnection(config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error loading config: %v", err)
|
fmt.Printf("Error loading config: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check if we can reach db before starting program
|
|
||||||
p := tea.NewProgram(initialModel(config))
|
p := tea.NewProgram(initialModel(config))
|
||||||
if _, err := p.Run(); err != nil {
|
if _, err := p.Run(); err != nil {
|
||||||
fmt.Printf("Alas, there's been an error: %v", err)
|
fmt.Printf("Alas, there's been an error: %v", err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue