seperate dns retrieval into its own model
This commit is contained in:
parent
ef2dbcede5
commit
a6bcb9be38
5 changed files with 179 additions and 72 deletions
157
internal/ui/pages/dns/retrieve.go
Normal file
157
internal/ui/pages/dns/retrieve.go
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
package dns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/paginator"
|
||||
"github.com/charmbracelet/bubbles/spinner"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
|
||||
"git.juancwu.dev/juancwu/porkbacon/internal/porkbun"
|
||||
"git.juancwu.dev/juancwu/porkbacon/internal/ui/messages"
|
||||
"git.juancwu.dev/juancwu/porkbacon/internal/ui/utils"
|
||||
)
|
||||
|
||||
type RetrieveModel struct {
|
||||
client *porkbun.Client
|
||||
loading bool
|
||||
records []string
|
||||
|
||||
spinner spinner.Model
|
||||
paginator paginator.Model
|
||||
textinput textinput.Model
|
||||
|
||||
stderr string
|
||||
}
|
||||
|
||||
func NewRetrieveModel(client *porkbun.Client) RetrieveModel {
|
||||
p := paginator.New()
|
||||
p.Type = paginator.Dots
|
||||
p.PerPage = 1
|
||||
p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "235", Dark: "252"}).Render("•")
|
||||
p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "250", Dark: "238"}).Render("•")
|
||||
|
||||
s := spinner.New()
|
||||
s.Spinner = spinner.Dot
|
||||
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
||||
|
||||
ti := textinput.New()
|
||||
ti.Placeholder = "Enter domain"
|
||||
ti.Width = 80
|
||||
|
||||
return RetrieveModel{
|
||||
client: client,
|
||||
spinner: s,
|
||||
paginator: p,
|
||||
textinput: ti,
|
||||
}
|
||||
}
|
||||
|
||||
func (m RetrieveModel) Init() tea.Cmd {
|
||||
return tea.Batch(m.spinner.Tick, textinput.Blink)
|
||||
}
|
||||
|
||||
func (m RetrieveModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
var cmd tea.Cmd
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
if m.loading {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
if msg.String() == "esc" {
|
||||
hadRecords := len(m.records) > 0
|
||||
m.loading = false
|
||||
m.records = nil
|
||||
return m, func() tea.Msg {
|
||||
if hadRecords {
|
||||
return messages.DNSRetrieveMsg{}
|
||||
}
|
||||
|
||||
return messages.SwitchPageMsg{Page: messages.PageMenu}
|
||||
}
|
||||
}
|
||||
|
||||
if msg.String() == "enter" {
|
||||
m.loading = true
|
||||
m.records = nil
|
||||
return m, retrieveRecords(m.client, m.textinput.Value())
|
||||
}
|
||||
|
||||
if len(m.records) > 0 {
|
||||
m.paginator, cmd = m.paginator.Update(msg)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
m.textinput, cmd = m.textinput.Update(msg)
|
||||
return m, cmd
|
||||
|
||||
case messages.DNSRetrieveMsg:
|
||||
m.textinput.Reset()
|
||||
m.textinput.Focus()
|
||||
|
||||
case *porkbun.DNSRecordsResponse:
|
||||
m.loading = false
|
||||
for _, record := range msg.Records {
|
||||
m.records = append(m.records, renderRecord(&record))
|
||||
}
|
||||
m.paginator.SetTotalPages(len(m.records))
|
||||
m.paginator.Page = 0
|
||||
case messages.ErrorMsg:
|
||||
m.stderr = fmt.Sprintf("Error: %v", msg)
|
||||
}
|
||||
|
||||
if m.loading {
|
||||
m.spinner, cmd = m.spinner.Update(msg)
|
||||
return m, tea.Batch(cmd, m.spinner.Tick)
|
||||
}
|
||||
|
||||
return m, textinput.Blink
|
||||
}
|
||||
|
||||
func (m RetrieveModel) View() string {
|
||||
if m.stderr != "" {
|
||||
return fmt.Sprintf("%s\n\n(Press ctrl+c to quit)", m.stderr)
|
||||
}
|
||||
|
||||
if m.loading {
|
||||
return fmt.Sprintf("\n\n %s Loading... press ctl+c to quit\n\n", m.spinner.View())
|
||||
}
|
||||
|
||||
if len(m.records) > 0 {
|
||||
return fmt.Sprintf("%s\n\n%s\n\n(Press Esc to go back, arrows to navigate)", m.records[m.paginator.Page], m.paginator.View())
|
||||
}
|
||||
|
||||
return fmt.Sprintf(
|
||||
"Enter domain to retrieve records for:\n\n%s\n\n(esc to quit)",
|
||||
m.textinput.View(),
|
||||
)
|
||||
}
|
||||
|
||||
func retrieveRecords(client *porkbun.Client, domain string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
resp, err := client.RetrieveDNSRecords(domain)
|
||||
if err != nil {
|
||||
return messages.ErrorMsg(err)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
}
|
||||
|
||||
func renderRecord(item *porkbun.DNSRecord) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("ID: " + item.ID + "\n")
|
||||
b.WriteString("Name: " + item.Name + "\n")
|
||||
b.WriteString("Type: " + item.Type + "\n")
|
||||
b.WriteString(fmt.Sprintln("TTL:", item.TTL))
|
||||
b.WriteString(fmt.Sprintln("Priority:", item.Priority))
|
||||
b.WriteString("Content: ")
|
||||
b.WriteString(utils.WrapText(item.Content, 80))
|
||||
b.WriteString("\n")
|
||||
b.WriteString("Notes: " + item.Notes + "\n")
|
||||
return b.String()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue