104 lines
2.7 KiB
Go
104 lines
2.7 KiB
Go
package gemlog
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
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) {
|
|
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 {
|
|
Rev string `json:"rev"`
|
|
} `json:"value"`
|
|
Doc GemlogListEntry `json:"doc"`
|
|
} `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 {
|
|
entries = append(entries, row.Doc)
|
|
}
|
|
|
|
return entries, nil
|
|
}
|
|
|
|
func SaveGemlogEntry(config *Config, entry *GemlogEntry) error {
|
|
url := fmt.Sprintf("%s:%d/gemlog/", config.CouchDB.Host, config.CouchDB.Port)
|
|
|
|
// 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("POST", 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")
|
|
|
|
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))
|
|
}
|
|
|
|
// fmt.Println(res)
|
|
// fmt.Println(string(body))
|
|
|
|
return nil
|
|
}
|