feat: Stage 1 scaffold — config, API client, placeholder TUI
Sets up the full project structure following the hactl/gokapi-tui architecture: strict Elm-pattern Bubbletea TUI, YAML config at ~/.config/terdut-tui/config.yaml, REST API client with Bearer auth, self-update via GitHub Releases, and a three-section tab placeholder (Alerts / Schedule / Users) that verifies server connectivity on startup.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
baseURL string
|
||||
httpClient *http.Client
|
||||
apiKey string
|
||||
}
|
||||
|
||||
func NewClient(baseURL, apiKey string) *Client {
|
||||
return &Client{
|
||||
baseURL: strings.TrimRight(baseURL, "/"),
|
||||
apiKey: apiKey,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) newRequest(method, path string) (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, c.baseURL+path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+c.apiKey)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *Client) do(req *http.Request, out any) error {
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
var e struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
_ = json.NewDecoder(resp.Body).Decode(&e)
|
||||
if e.Error != "" {
|
||||
return fmt.Errorf("server returned %d: %s", resp.StatusCode, e.Error)
|
||||
}
|
||||
return fmt.Errorf("server returned %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
if out != nil {
|
||||
return json.NewDecoder(resp.Body).Decode(out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// HealthCheck calls GET /healthz (unauthenticated path, no auth needed but we send it anyway).
|
||||
func (c *Client) HealthCheck() error {
|
||||
req, err := http.NewRequest(http.MethodGet, c.baseURL+"/healthz", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("health check failed: %s", resp.Status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const defaultRefreshInterval = 30 * time.Second
|
||||
|
||||
type Config struct {
|
||||
ServerURL string
|
||||
APIKey string
|
||||
RefreshInterval time.Duration
|
||||
}
|
||||
|
||||
type rawConfig struct {
|
||||
ServerURL string `yaml:"server_url"`
|
||||
APIKey string `yaml:"api_key"`
|
||||
RefreshInterval int `yaml:"refresh_interval,omitempty"` // seconds
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
dir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot determine config directory: %w", err)
|
||||
}
|
||||
|
||||
path := filepath.Join(dir, "terdut-tui", "config.yaml")
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("config file not found at %s\n\nCreate it with:\n server_url: https://terdut.example.com\n api_key: <your-api-key>", path)
|
||||
}
|
||||
return nil, fmt.Errorf("cannot read config file: %w", err)
|
||||
}
|
||||
|
||||
var raw rawConfig
|
||||
if err := yaml.Unmarshal(data, &raw); err != nil {
|
||||
return nil, fmt.Errorf("invalid config file: %w", err)
|
||||
}
|
||||
|
||||
if raw.ServerURL == "" {
|
||||
return nil, fmt.Errorf("config: 'server_url' is required")
|
||||
}
|
||||
if raw.APIKey == "" {
|
||||
return nil, fmt.Errorf("config: 'api_key' is required")
|
||||
}
|
||||
|
||||
interval := defaultRefreshInterval
|
||||
if raw.RefreshInterval > 0 {
|
||||
interval = time.Duration(raw.RefreshInterval) * time.Second
|
||||
}
|
||||
|
||||
return &Config{
|
||||
ServerURL: raw.ServerURL,
|
||||
APIKey: raw.APIKey,
|
||||
RefreshInterval: interval,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package tui
|
||||
|
||||
import "github.com/charmbracelet/bubbles/key"
|
||||
|
||||
type keyMap struct {
|
||||
Up key.Binding
|
||||
Down key.Binding
|
||||
Left key.Binding
|
||||
Right key.Binding
|
||||
Tab key.Binding
|
||||
Enter key.Binding
|
||||
Escape key.Binding
|
||||
Refresh key.Binding
|
||||
Filter key.Binding
|
||||
Quit key.Binding
|
||||
}
|
||||
|
||||
var keys = keyMap{
|
||||
Up: key.NewBinding(key.WithKeys("up", "k"), key.WithHelp("↑/k", "up")),
|
||||
Down: key.NewBinding(key.WithKeys("down", "j"), key.WithHelp("↓/j", "down")),
|
||||
Left: key.NewBinding(key.WithKeys("left", "h"), key.WithHelp("◀/h", "prev")),
|
||||
Right: key.NewBinding(key.WithKeys("right", "l"), key.WithHelp("▶/l", "next")),
|
||||
Tab: key.NewBinding(key.WithKeys("tab"), key.WithHelp("tab", "switch section")),
|
||||
Enter: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "select")),
|
||||
Escape: key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "back")),
|
||||
Refresh: key.NewBinding(key.WithKeys("r"), key.WithHelp("r", "refresh")),
|
||||
Filter: key.NewBinding(key.WithKeys("f"), key.WithHelp("f", "filter")),
|
||||
Quit: key.NewBinding(key.WithKeys("q", "ctrl+c"), key.WithHelp("q", "quit")),
|
||||
}
|
||||
|
||||
func (k keyMap) ShortHelp() []key.Binding {
|
||||
return []key.Binding{k.Up, k.Down, k.Tab, k.Refresh, k.Quit}
|
||||
}
|
||||
|
||||
func (k keyMap) FullHelp() [][]key.Binding {
|
||||
return [][]key.Binding{
|
||||
{k.Up, k.Down, k.Left, k.Right},
|
||||
{k.Tab, k.Enter, k.Escape},
|
||||
{k.Refresh, k.Filter, k.Quit},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/bubbles/help"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/yeniklas/terdut-tui/internal/api"
|
||||
)
|
||||
|
||||
type section int
|
||||
|
||||
const (
|
||||
sectionAlerts section = iota
|
||||
sectionSchedule
|
||||
sectionUsers
|
||||
)
|
||||
|
||||
type mode int
|
||||
|
||||
const (
|
||||
modeDashboard mode = iota
|
||||
modeDetail
|
||||
modeSchedule
|
||||
modeUsers
|
||||
)
|
||||
|
||||
// tea.Msg types
|
||||
|
||||
type connectedMsg struct{ serverURL string }
|
||||
type connectErrMsg struct{ err error }
|
||||
type tickMsg time.Time
|
||||
type clearStatusMsg struct{}
|
||||
|
||||
// Model holds all UI state.
|
||||
type Model struct {
|
||||
client *api.Client
|
||||
serverURL string
|
||||
refreshInterval time.Duration
|
||||
|
||||
activeSection section
|
||||
mode mode
|
||||
width int
|
||||
height int
|
||||
|
||||
connected bool
|
||||
err error
|
||||
statusMsg string
|
||||
|
||||
help help.Model
|
||||
keys keyMap
|
||||
}
|
||||
|
||||
func NewModel(client *api.Client, serverURL string, refreshInterval time.Duration) Model {
|
||||
return Model{
|
||||
client: client,
|
||||
serverURL: serverURL,
|
||||
refreshInterval: refreshInterval,
|
||||
activeSection: sectionAlerts,
|
||||
mode: modeDashboard,
|
||||
help: help.New(),
|
||||
keys: keys,
|
||||
}
|
||||
}
|
||||
|
||||
func (m Model) Init() tea.Cmd {
|
||||
return connectCmd(m.client)
|
||||
}
|
||||
|
||||
func connectCmd(client *api.Client) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
if err := client.HealthCheck(); err != nil {
|
||||
return connectErrMsg{err}
|
||||
}
|
||||
return connectedMsg{}
|
||||
}
|
||||
}
|
||||
|
||||
func tickCmd(interval time.Duration) tea.Cmd {
|
||||
return tea.Tick(interval, func(t time.Time) tea.Msg {
|
||||
return tickMsg(t)
|
||||
})
|
||||
}
|
||||
|
||||
func clearStatusCmd() tea.Cmd {
|
||||
return tea.Tick(3*time.Second, func(time.Time) tea.Msg {
|
||||
return clearStatusMsg{}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package tui
|
||||
|
||||
import "github.com/charmbracelet/lipgloss"
|
||||
|
||||
var (
|
||||
colorPrimary = lipgloss.Color("69") // blue
|
||||
colorMuted = lipgloss.Color("240") // gray
|
||||
colorFiring = lipgloss.Color("196") // red
|
||||
colorResolved = lipgloss.Color("70") // green
|
||||
colorAccent = lipgloss.Color("214") // orange
|
||||
|
||||
styleHeader = lipgloss.NewStyle().
|
||||
Bold(true).
|
||||
Foreground(colorPrimary).
|
||||
Padding(0, 1)
|
||||
|
||||
styleTabActive = lipgloss.NewStyle().
|
||||
Bold(true).
|
||||
Foreground(lipgloss.Color("0")).
|
||||
Background(colorPrimary).
|
||||
Padding(0, 2)
|
||||
|
||||
styleTabInactive = lipgloss.NewStyle().
|
||||
Foreground(colorMuted).
|
||||
Padding(0, 2)
|
||||
|
||||
styleFooter = lipgloss.NewStyle().
|
||||
Foreground(colorMuted)
|
||||
|
||||
styleStatus = lipgloss.NewStyle().
|
||||
Foreground(colorAccent).
|
||||
Bold(true)
|
||||
|
||||
styleError = lipgloss.NewStyle().
|
||||
Foreground(colorFiring).
|
||||
Bold(true)
|
||||
|
||||
styleFiring = lipgloss.NewStyle().Foreground(colorFiring).Bold(true)
|
||||
styleResolved = lipgloss.NewStyle().Foreground(colorResolved)
|
||||
styleMuted = lipgloss.NewStyle().Foreground(colorMuted)
|
||||
)
|
||||
@@ -0,0 +1,54 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.WindowSizeMsg:
|
||||
m.width = msg.Width
|
||||
m.height = msg.Height
|
||||
return m, nil
|
||||
|
||||
case connectedMsg:
|
||||
m.connected = true
|
||||
m.err = nil
|
||||
m.statusMsg = "Connected to " + m.serverURL
|
||||
return m, tea.Batch(tickCmd(m.refreshInterval), clearStatusCmd())
|
||||
|
||||
case connectErrMsg:
|
||||
m.connected = false
|
||||
m.err = msg.err
|
||||
return m, nil
|
||||
|
||||
case tickMsg:
|
||||
return m, tickCmd(m.refreshInterval)
|
||||
|
||||
case clearStatusMsg:
|
||||
m.statusMsg = ""
|
||||
return m, nil
|
||||
|
||||
case tea.KeyMsg:
|
||||
return m.handleKey(msg)
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m Model) handleKey(msg tea.KeyMsg) (Model, tea.Cmd) {
|
||||
switch {
|
||||
case msg.String() == "q" || msg.String() == "ctrl+c":
|
||||
return m, tea.Quit
|
||||
|
||||
case msg.String() == "tab":
|
||||
m.activeSection = (m.activeSection + 1) % 3
|
||||
return m, nil
|
||||
|
||||
case msg.String() == "r":
|
||||
m.statusMsg = "Refreshing…"
|
||||
return m, tea.Batch(connectCmd(m.client), clearStatusCmd())
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
var sectionNames = []string{"Alerts", "Schedule", "Users"}
|
||||
|
||||
func (m Model) View() string {
|
||||
if m.width == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
header := m.renderHeader()
|
||||
tabs := m.renderTabs()
|
||||
body := m.renderBody()
|
||||
footer := m.renderFooter()
|
||||
|
||||
return lipgloss.JoinVertical(lipgloss.Left, header, tabs, body, footer)
|
||||
}
|
||||
|
||||
func (m Model) renderHeader() string {
|
||||
title := styleHeader.Render("terdut-tui")
|
||||
right := ""
|
||||
if m.serverURL != "" {
|
||||
right = styleMuted.Render(m.serverURL)
|
||||
}
|
||||
|
||||
gap := m.width - lipgloss.Width(title) - lipgloss.Width(right)
|
||||
if gap < 0 {
|
||||
gap = 0
|
||||
}
|
||||
return title + strings.Repeat(" ", gap) + right
|
||||
}
|
||||
|
||||
func (m Model) renderTabs() string {
|
||||
var tabs []string
|
||||
for i, name := range sectionNames {
|
||||
if section(i) == m.activeSection {
|
||||
tabs = append(tabs, styleTabActive.Render(name))
|
||||
} else {
|
||||
tabs = append(tabs, styleTabInactive.Render(name))
|
||||
}
|
||||
}
|
||||
line := strings.Repeat("─", m.width)
|
||||
return strings.Join(tabs, "") + "\n" + styleMuted.Render(line)
|
||||
}
|
||||
|
||||
func (m Model) renderBody() string {
|
||||
bodyHeight := m.height - 5 // header + tabs + separator + footer + help
|
||||
if bodyHeight < 1 {
|
||||
bodyHeight = 1
|
||||
}
|
||||
|
||||
var content string
|
||||
if m.err != nil {
|
||||
content = styleError.Render(fmt.Sprintf("Error: %v", m.err))
|
||||
} else if !m.connected {
|
||||
content = styleMuted.Render("Connecting…")
|
||||
} else if m.statusMsg != "" {
|
||||
content = styleStatus.Render(m.statusMsg)
|
||||
} else {
|
||||
switch m.activeSection {
|
||||
case sectionAlerts:
|
||||
content = styleMuted.Render("Alert dashboard — coming in Stage 2")
|
||||
case sectionSchedule:
|
||||
content = styleMuted.Render("On-call schedule — coming in Stage 4")
|
||||
case sectionUsers:
|
||||
content = styleMuted.Render("User management — coming in Stage 5")
|
||||
}
|
||||
}
|
||||
|
||||
lines := strings.Split(content, "\n")
|
||||
padding := (bodyHeight - len(lines)) / 2
|
||||
if padding < 0 {
|
||||
padding = 0
|
||||
}
|
||||
return strings.Repeat("\n", padding) + content
|
||||
}
|
||||
|
||||
func (m Model) renderFooter() string {
|
||||
helpView := m.help.ShortHelpView(m.keys.ShortHelp())
|
||||
return "\n" + styleFooter.Render(helpView)
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const releaseAPI = "https://api.github.com/repos/yeniklas/terdut-tui/releases/latest"
|
||||
|
||||
type release struct {
|
||||
TagName string `json:"tag_name"`
|
||||
Assets []asset `json:"assets"`
|
||||
}
|
||||
|
||||
type asset struct {
|
||||
Name string `json:"name"`
|
||||
BrowserDownloadURL string `json:"browser_download_url"`
|
||||
}
|
||||
|
||||
func Run(currentVersion string) error {
|
||||
if currentVersion == "dev" {
|
||||
fmt.Println("cannot self-update a dev build")
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Println("Checking for updates…")
|
||||
rel, err := fetchLatest()
|
||||
if err != nil {
|
||||
return fmt.Errorf("fetch release info: %w", err)
|
||||
}
|
||||
|
||||
if rel.TagName == currentVersion {
|
||||
fmt.Printf("terdut-tui is already up to date (%s)\n", currentVersion)
|
||||
return nil
|
||||
}
|
||||
|
||||
assetName := fmt.Sprintf("terdut-tui-%s-%s-%s", rel.TagName, runtime.GOOS, runtime.GOARCH)
|
||||
var downloadURL string
|
||||
for _, a := range rel.Assets {
|
||||
if a.Name == assetName {
|
||||
downloadURL = a.BrowserDownloadURL
|
||||
break
|
||||
}
|
||||
}
|
||||
if downloadURL == "" {
|
||||
var names []string
|
||||
for _, a := range rel.Assets {
|
||||
names = append(names, a.Name)
|
||||
}
|
||||
return fmt.Errorf("no binary found for %s/%s in release %s\navailable: %s",
|
||||
runtime.GOOS, runtime.GOARCH, rel.TagName, strings.Join(names, ", "))
|
||||
}
|
||||
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve binary path: %w", err)
|
||||
}
|
||||
exe, err = filepath.EvalSymlinks(exe)
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve symlink: %w", err)
|
||||
}
|
||||
dir := filepath.Dir(exe)
|
||||
|
||||
probe, err := os.CreateTemp(dir, ".terdut-tui-update-*")
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot write to %s: %w\nre-run with appropriate permissions", dir, err)
|
||||
}
|
||||
probe.Close()
|
||||
os.Remove(probe.Name())
|
||||
|
||||
fmt.Printf("Update terdut-tui %s → %s? [y/N] ", currentVersion, rel.TagName)
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
answer, _ := reader.ReadString('\n')
|
||||
if strings.ToLower(strings.TrimSpace(answer)) != "y" {
|
||||
fmt.Println("Update cancelled.")
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Printf("Downloading %s…\n", assetName)
|
||||
tmp, err := os.CreateTemp(dir, ".terdut-tui-update-*")
|
||||
if err != nil {
|
||||
return fmt.Errorf("create temp file: %w", err)
|
||||
}
|
||||
tmpName := tmp.Name()
|
||||
defer func() {
|
||||
tmp.Close()
|
||||
os.Remove(tmpName)
|
||||
}()
|
||||
|
||||
resp, err := http.Get(downloadURL) //nolint:gosec
|
||||
if err != nil {
|
||||
return fmt.Errorf("download: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("download: unexpected status %s", resp.Status)
|
||||
}
|
||||
|
||||
if _, err := io.Copy(tmp, resp.Body); err != nil {
|
||||
return fmt.Errorf("write binary: %w", err)
|
||||
}
|
||||
tmp.Close()
|
||||
|
||||
if err := os.Chmod(tmpName, 0755); err != nil {
|
||||
return fmt.Errorf("chmod: %w", err)
|
||||
}
|
||||
|
||||
if err := os.Rename(tmpName, exe); err != nil {
|
||||
return fmt.Errorf("replace binary: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Updated to %s.\n", rel.TagName)
|
||||
return nil
|
||||
}
|
||||
|
||||
func fetchLatest() (*release, error) {
|
||||
req, err := http.NewRequest(http.MethodGet, releaseAPI, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "application/vnd.github+json")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("GitHub API returned %s", resp.Status)
|
||||
}
|
||||
|
||||
var rel release
|
||||
if err := json.NewDecoder(resp.Body).Decode(&rel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rel, nil
|
||||
}
|
||||
Reference in New Issue
Block a user