feat: add safe multi-account routing and prepare public release
This commit is contained in:
71 files changed
+2906
-120
No files matched your search
+382
-8
@@ -29,6 +29,8 @@ type Backend interface {
|
||||
Jobs(context.Context, int) ([]domain.Job, error)
|
||||
Audit(context.Context, int) ([]domain.AuditEvent, error)
|
||||
AddCloudflareCredential(context.Context, app.AddCredentialInput) (domain.Credential, error)
|
||||
DeleteCredential(context.Context, string) error
|
||||
SetZonePreferredCredential(context.Context, string, string) (domain.Zone, error)
|
||||
SyncAll(context.Context) ([]domain.Job, error)
|
||||
CreateDNSRecord(context.Context, string, domain.DNSRecord) (domain.DNSRecord, error)
|
||||
PatchDNSRecord(context.Context, string, string, domain.DNSRecord) (domain.DNSRecord, error)
|
||||
@@ -123,9 +125,29 @@ type Model struct {
|
||||
unlockFocus int
|
||||
unlockInputs []textinput.Model
|
||||
|
||||
addCredential bool
|
||||
credentialFocus int
|
||||
credentialInputs []textinput.Model
|
||||
addCredential bool
|
||||
credentialFocus int
|
||||
credentialInputs []textinput.Model
|
||||
removeCredential bool
|
||||
removeCredentialInput textinput.Model
|
||||
removeCredentialID string
|
||||
removeCredentialExpected string
|
||||
removeCredentialKind domain.CredentialKind
|
||||
removeCredentialAccountHint string
|
||||
removeCredentialZoneIDs []string
|
||||
removeCredentialZoneNames []string
|
||||
|
||||
preferCredential bool
|
||||
preferCredentialFocus int
|
||||
preferCredentialInputs []textinput.Model
|
||||
preferZoneID string
|
||||
preferZoneExpectedName string
|
||||
preferZoneProvider string
|
||||
preferZoneAccountID string
|
||||
preferOriginalCredentialID string
|
||||
preferResolvedCredentialID string
|
||||
preferResolvedCredentialLabel string
|
||||
preferCredentialSelection string
|
||||
|
||||
editRecord bool
|
||||
recordEditing bool
|
||||
@@ -342,6 +364,12 @@ func (m *Model) Update(message tea.Msg) (tea.Model, tea.Cmd) {
|
||||
if m.addCredential {
|
||||
return m.updateCredentialForm(message)
|
||||
}
|
||||
if m.removeCredential {
|
||||
return m.updateRemoveCredentialForm(message)
|
||||
}
|
||||
if m.preferCredential {
|
||||
return m.updatePreferredCredentialForm(message)
|
||||
}
|
||||
if m.editRecord {
|
||||
return m.updateRecordForm(message)
|
||||
}
|
||||
@@ -406,6 +434,11 @@ func (m *Model) Update(message tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.openCredentialForm()
|
||||
return m, textinput.Blink
|
||||
}
|
||||
case "p":
|
||||
if m.screen == screenZones && len(m.filteredZones()) > 0 {
|
||||
m.openPreferredCredentialForm()
|
||||
return m, textinput.Blink
|
||||
}
|
||||
case "n":
|
||||
if m.screen == screenDNS && len(m.zones) > 0 {
|
||||
m.openRecordForm(false)
|
||||
@@ -436,6 +469,10 @@ func (m *Model) Update(message tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, textinput.Blink
|
||||
}
|
||||
case "d":
|
||||
if m.screen == screenAccounts && len(m.filteredCredentials()) > 0 {
|
||||
m.openRemoveCredentialForm()
|
||||
return m, textinput.Blink
|
||||
}
|
||||
if m.screen == screenDNS && len(m.filteredRecords()) > 0 {
|
||||
m.confirmDelete = true
|
||||
}
|
||||
@@ -792,6 +829,313 @@ func (m *Model) clearCredentialForm() {
|
||||
m.addCredential = false
|
||||
}
|
||||
|
||||
func (m *Model) openRemoveCredentialForm() {
|
||||
credentials := m.filteredCredentials()
|
||||
if len(credentials) == 0 {
|
||||
return
|
||||
}
|
||||
credential := credentials[min(m.rowOffset, len(credentials)-1)]
|
||||
m.removeCredentialZoneIDs = nil
|
||||
m.removeCredentialZoneNames = nil
|
||||
input := textinput.New()
|
||||
input.Prompt = ""
|
||||
input.Placeholder = "Type the exact connection label"
|
||||
input.SetWidth(52)
|
||||
input.Focus()
|
||||
m.removeCredentialInput = input
|
||||
m.removeCredentialID = credential.ID
|
||||
m.removeCredentialExpected = credential.Label
|
||||
m.removeCredentialKind = credential.Kind
|
||||
m.removeCredentialAccountHint = credential.AccountHint
|
||||
for _, zone := range m.zones {
|
||||
if zone.PreferredCredentialID == credential.ID {
|
||||
m.removeCredentialZoneIDs = append(m.removeCredentialZoneIDs, zone.ID)
|
||||
m.removeCredentialZoneNames = append(m.removeCredentialZoneNames, zone.Name)
|
||||
}
|
||||
}
|
||||
m.removeCredential = true
|
||||
m.err = nil
|
||||
}
|
||||
|
||||
func (m *Model) updateRemoveCredentialForm(message tea.Msg) (tea.Model, tea.Cmd) {
|
||||
if key, ok := message.(tea.KeyPressMsg); ok {
|
||||
switch key.String() {
|
||||
case "esc":
|
||||
m.clearRemoveCredentialForm()
|
||||
return m, nil
|
||||
case "enter":
|
||||
if m.mutationFormBlocked() {
|
||||
return m, nil
|
||||
}
|
||||
credential, ok := m.credentialByID(m.removeCredentialID)
|
||||
if !ok || credential.Label != m.removeCredentialExpected {
|
||||
m.err = errors.New("the selected connection changed; cancel and select it again")
|
||||
return m, nil
|
||||
}
|
||||
if !m.removeCredentialRoutesUnchanged() {
|
||||
m.err = errors.New("the affected zone routes changed; cancel and review the removal again")
|
||||
return m, nil
|
||||
}
|
||||
if m.removeCredentialInput.Value() != m.removeCredentialExpected {
|
||||
m.err = fmt.Errorf("type %q exactly to confirm removal", m.removeCredentialExpected)
|
||||
return m, nil
|
||||
}
|
||||
credentialID := m.removeCredentialID
|
||||
label := m.removeCredentialExpected
|
||||
m.clearRemoveCredentialForm()
|
||||
m.busy = true
|
||||
return m, func() tea.Msg {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
|
||||
defer cancel()
|
||||
err := m.backend.DeleteCredential(ctx, credentialID)
|
||||
return operationMsg{message: "Removed Cloudflare connection " + label, err: err, reload: true}
|
||||
}
|
||||
}
|
||||
}
|
||||
var cmd tea.Cmd
|
||||
m.removeCredentialInput, cmd = m.removeCredentialInput.Update(message)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
func (m *Model) clearRemoveCredentialForm() {
|
||||
m.removeCredentialInput.SetValue("")
|
||||
m.removeCredentialInput.Blur()
|
||||
m.removeCredential = false
|
||||
m.removeCredentialID = ""
|
||||
m.removeCredentialExpected = ""
|
||||
m.removeCredentialKind = ""
|
||||
m.removeCredentialAccountHint = ""
|
||||
m.removeCredentialZoneIDs = nil
|
||||
m.removeCredentialZoneNames = nil
|
||||
}
|
||||
|
||||
func (m *Model) removeCredentialRoutesUnchanged() bool {
|
||||
if len(m.removeCredentialZoneIDs) != len(m.removeCredentialZoneNames) {
|
||||
return false
|
||||
}
|
||||
expected := make(map[string]string, len(m.removeCredentialZoneIDs))
|
||||
for index, zoneID := range m.removeCredentialZoneIDs {
|
||||
expected[zoneID] = m.removeCredentialZoneNames[index]
|
||||
}
|
||||
seen := 0
|
||||
for _, zone := range m.zones {
|
||||
if zone.PreferredCredentialID != m.removeCredentialID {
|
||||
continue
|
||||
}
|
||||
name, ok := expected[zone.ID]
|
||||
if !ok || name != zone.Name {
|
||||
return false
|
||||
}
|
||||
seen++
|
||||
}
|
||||
return seen == len(expected)
|
||||
}
|
||||
|
||||
func (m *Model) openPreferredCredentialForm() {
|
||||
zone, ok := m.selectedZone()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
newInput := func(placeholder string) textinput.Model {
|
||||
input := textinput.New()
|
||||
input.Prompt = ""
|
||||
input.Placeholder = placeholder
|
||||
input.SetWidth(54)
|
||||
return input
|
||||
}
|
||||
m.preferCredentialInputs = []textinput.Model{
|
||||
newInput("Credential ID or unique connection label"),
|
||||
newInput("Type the exact zone name"),
|
||||
}
|
||||
m.preferCredentialInputs[0].Focus()
|
||||
m.preferCredentialFocus = 0
|
||||
m.preferZoneID = zone.ID
|
||||
m.preferZoneExpectedName = zone.Name
|
||||
m.preferZoneProvider = zone.Provider
|
||||
m.preferZoneAccountID = zone.AccountID
|
||||
m.preferOriginalCredentialID = zone.PreferredCredentialID
|
||||
m.preferResolvedCredentialID = ""
|
||||
m.preferResolvedCredentialLabel = ""
|
||||
m.preferCredentialSelection = ""
|
||||
m.preferCredential = true
|
||||
m.err = nil
|
||||
}
|
||||
|
||||
func (m *Model) updatePreferredCredentialForm(message tea.Msg) (tea.Model, tea.Cmd) {
|
||||
if key, ok := message.(tea.KeyPressMsg); ok {
|
||||
switch key.String() {
|
||||
case "esc":
|
||||
m.clearPreferredCredentialForm()
|
||||
return m, nil
|
||||
case "tab", "down":
|
||||
if m.preferCredentialFocus == 0 && !m.capturePreferredCredential() {
|
||||
return m, nil
|
||||
}
|
||||
m.focusPreferredCredential(1)
|
||||
return m, nil
|
||||
case "shift+tab", "up":
|
||||
if m.preferCredentialFocus == 0 && !m.capturePreferredCredential() {
|
||||
return m, nil
|
||||
}
|
||||
m.focusPreferredCredential(-1)
|
||||
return m, nil
|
||||
case "enter":
|
||||
if m.preferCredentialFocus < len(m.preferCredentialInputs)-1 {
|
||||
if !m.capturePreferredCredential() {
|
||||
return m, nil
|
||||
}
|
||||
m.focusPreferredCredential(1)
|
||||
return m, nil
|
||||
}
|
||||
if m.mutationFormBlocked() {
|
||||
return m, nil
|
||||
}
|
||||
zone, ok := m.zoneByID(m.preferZoneID)
|
||||
if !ok || zone.Name != m.preferZoneExpectedName || zone.Provider != m.preferZoneProvider || zone.AccountID != m.preferZoneAccountID || zone.PreferredCredentialID != m.preferOriginalCredentialID {
|
||||
m.err = errors.New("the selected zone or its route changed; cancel and select it again")
|
||||
return m, nil
|
||||
}
|
||||
if m.preferCredentialInputs[1].Value() != m.preferZoneExpectedName {
|
||||
m.err = fmt.Errorf("type %q exactly to confirm the routing change", m.preferZoneExpectedName)
|
||||
return m, nil
|
||||
}
|
||||
if m.preferResolvedCredentialID == "" || strings.TrimSpace(m.preferCredentialInputs[0].Value()) != m.preferCredentialSelection {
|
||||
m.err = errors.New("select the connection again before confirming")
|
||||
return m, nil
|
||||
}
|
||||
credential, ok := m.credentialByID(m.preferResolvedCredentialID)
|
||||
if !ok || credential.Label != m.preferResolvedCredentialLabel {
|
||||
m.err = errors.New("the selected connection changed; select it again")
|
||||
return m, nil
|
||||
}
|
||||
if _, err := validatePreferredCredential(credential, zone); err != nil {
|
||||
m.err = err
|
||||
return m, nil
|
||||
}
|
||||
zoneID := m.preferZoneID
|
||||
credentialID := credential.ID
|
||||
zoneName := m.preferZoneExpectedName
|
||||
m.clearPreferredCredentialForm()
|
||||
m.busy = true
|
||||
return m, func() tea.Msg {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
_, err := m.backend.SetZonePreferredCredential(ctx, zoneID, credentialID)
|
||||
return operationMsg{message: "Preferred connection updated for " + zoneName, err: err, reload: true}
|
||||
}
|
||||
}
|
||||
}
|
||||
var cmd tea.Cmd
|
||||
m.preferCredentialInputs[m.preferCredentialFocus], cmd = m.preferCredentialInputs[m.preferCredentialFocus].Update(message)
|
||||
if m.preferCredentialFocus == 0 {
|
||||
m.preferResolvedCredentialID = ""
|
||||
m.preferResolvedCredentialLabel = ""
|
||||
m.preferCredentialSelection = ""
|
||||
}
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
func (m *Model) capturePreferredCredential() bool {
|
||||
zone, ok := m.zoneByID(m.preferZoneID)
|
||||
if !ok || zone.Name != m.preferZoneExpectedName || zone.Provider != m.preferZoneProvider || zone.AccountID != m.preferZoneAccountID || zone.PreferredCredentialID != m.preferOriginalCredentialID {
|
||||
m.err = errors.New("the selected zone or its route changed; cancel and select it again")
|
||||
return false
|
||||
}
|
||||
selection := strings.TrimSpace(m.preferCredentialInputs[0].Value())
|
||||
credential, err := m.resolvePreferredCredential(selection, zone)
|
||||
if err != nil {
|
||||
m.err = err
|
||||
return false
|
||||
}
|
||||
m.preferResolvedCredentialID = credential.ID
|
||||
m.preferResolvedCredentialLabel = credential.Label
|
||||
m.preferCredentialSelection = selection
|
||||
m.err = nil
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *Model) focusPreferredCredential(delta int) {
|
||||
m.preferCredentialInputs[m.preferCredentialFocus].Blur()
|
||||
m.preferCredentialFocus = (m.preferCredentialFocus + delta + len(m.preferCredentialInputs)) % len(m.preferCredentialInputs)
|
||||
m.preferCredentialInputs[m.preferCredentialFocus].Focus()
|
||||
}
|
||||
|
||||
func (m *Model) clearPreferredCredentialForm() {
|
||||
for i := range m.preferCredentialInputs {
|
||||
m.preferCredentialInputs[i].SetValue("")
|
||||
m.preferCredentialInputs[i].Blur()
|
||||
}
|
||||
m.preferCredential = false
|
||||
m.preferCredentialFocus = 0
|
||||
m.preferZoneID = ""
|
||||
m.preferZoneExpectedName = ""
|
||||
m.preferZoneProvider = ""
|
||||
m.preferZoneAccountID = ""
|
||||
m.preferOriginalCredentialID = ""
|
||||
m.preferResolvedCredentialID = ""
|
||||
m.preferResolvedCredentialLabel = ""
|
||||
m.preferCredentialSelection = ""
|
||||
}
|
||||
|
||||
func (m *Model) resolvePreferredCredential(value string, zone domain.Zone) (domain.Credential, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return domain.Credential{}, errors.New("a credential ID or connection label is required")
|
||||
}
|
||||
for _, credential := range m.credentials {
|
||||
if credential.ID == value {
|
||||
return validatePreferredCredential(credential, zone)
|
||||
}
|
||||
}
|
||||
matches := make([]domain.Credential, 0, 1)
|
||||
for _, credential := range m.credentials {
|
||||
if strings.EqualFold(strings.TrimSpace(credential.Label), value) {
|
||||
matches = append(matches, credential)
|
||||
}
|
||||
}
|
||||
switch len(matches) {
|
||||
case 0:
|
||||
return domain.Credential{}, fmt.Errorf("connection %q is not synchronized; use an ID shown in Accounts", value)
|
||||
case 1:
|
||||
return validatePreferredCredential(matches[0], zone)
|
||||
default:
|
||||
return domain.Credential{}, fmt.Errorf("connection label %q is ambiguous across %d credentials; use the credential ID", value, len(matches))
|
||||
}
|
||||
}
|
||||
|
||||
func validatePreferredCredential(credential domain.Credential, zone domain.Zone) (domain.Credential, error) {
|
||||
if credential.Provider != zone.Provider {
|
||||
return domain.Credential{}, fmt.Errorf("connection %q uses provider %q, not %q", credential.Label, credential.Provider, zone.Provider)
|
||||
}
|
||||
if credential.Status != domain.CredentialValid {
|
||||
return domain.Credential{}, fmt.Errorf("connection %q is %s; synchronize or replace it before routing", credential.Label, credential.Status)
|
||||
}
|
||||
return credential, nil
|
||||
}
|
||||
|
||||
func (m *Model) credentialByID(id string) (domain.Credential, bool) {
|
||||
for _, credential := range m.credentials {
|
||||
if credential.ID == id {
|
||||
return credential, true
|
||||
}
|
||||
}
|
||||
return domain.Credential{}, false
|
||||
}
|
||||
|
||||
func (m *Model) mutationFormBlocked() bool {
|
||||
switch {
|
||||
case m.recoveryBlocked:
|
||||
m.toast, m.toastAt = "Crash recovery must succeed before mutations are enabled", time.Now()
|
||||
case m.busy:
|
||||
m.toast, m.toastAt = "An operation is already running", time.Now()
|
||||
case m.loading:
|
||||
m.toast, m.toastAt = "Loading the latest zone state", time.Now()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *Model) openRecordForm(editing bool) {
|
||||
newInput := func(placeholder string) textinput.Model {
|
||||
input := textinput.New()
|
||||
@@ -1383,12 +1727,14 @@ func (m *Model) isMutationKey(key string) bool {
|
||||
return true
|
||||
case "a":
|
||||
return m.screen == screenDashboard || m.screen == screenAccounts
|
||||
case "p":
|
||||
return m.screen == screenZones
|
||||
case "n":
|
||||
return m.screen == screenDNS || m.screen == screenCertificates
|
||||
case "i", "x", "u":
|
||||
return m.screen == screenCertificates
|
||||
case "d":
|
||||
return m.screen == screenDNS || m.screen == screenCertificates
|
||||
return m.screen == screenAccounts || m.screen == screenDNS || m.screen == screenCertificates
|
||||
case "e":
|
||||
return m.screen == screenDNS
|
||||
case "s", "m", "t", "v", "h":
|
||||
@@ -1493,8 +1839,25 @@ func (m *Model) updateOverlay(message tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.openCredentialForm()
|
||||
return m, textinput.Blink
|
||||
}
|
||||
m.busy = true
|
||||
return m, syncCmd(m.backend)
|
||||
switch selected - len(screens) {
|
||||
case 1:
|
||||
m.busy = true
|
||||
return m, syncCmd(m.backend)
|
||||
case 2:
|
||||
if m.screen != screenAccounts || len(m.filteredCredentials()) == 0 {
|
||||
m.toast, m.toastAt = "Select a connection in Accounts first", time.Now()
|
||||
return m, nil
|
||||
}
|
||||
m.openRemoveCredentialForm()
|
||||
return m, textinput.Blink
|
||||
case 3:
|
||||
if m.screen != screenZones || len(m.filteredZones()) == 0 {
|
||||
m.toast, m.toastAt = "Select a zone in Zones first", time.Now()
|
||||
return m, nil
|
||||
}
|
||||
m.openPreferredCredentialForm()
|
||||
return m, textinput.Blink
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
@@ -1737,6 +2100,12 @@ func (m *Model) resizeInputs() {
|
||||
for i := range m.credentialInputs {
|
||||
m.credentialInputs[i].SetWidth(width)
|
||||
}
|
||||
if m.removeCredential {
|
||||
m.removeCredentialInput.SetWidth(width)
|
||||
}
|
||||
for i := range m.preferCredentialInputs {
|
||||
m.preferCredentialInputs[i].SetWidth(width)
|
||||
}
|
||||
for i := range m.recordInputs {
|
||||
m.recordInputs[i].SetWidth(width)
|
||||
}
|
||||
@@ -1778,9 +2147,14 @@ func max(a, b int) int {
|
||||
}
|
||||
|
||||
func paletteCommands() []string {
|
||||
result := make([]string, 0, len(screens)+2)
|
||||
result := make([]string, 0, len(screens)+4)
|
||||
for _, item := range screens {
|
||||
result = append(result, "Go to "+item.name)
|
||||
}
|
||||
return append(result, "Add Cloudflare account", "Synchronize everything")
|
||||
return append(result,
|
||||
"Add Cloudflare account",
|
||||
"Synchronize everything",
|
||||
"Remove selected Cloudflare connection/token",
|
||||
"Set selected zone preferred connection",
|
||||
)
|
||||
}
|
||||
+278
-15
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
tea "charm.land/bubbletea/v2"
|
||||
"charm.land/lipgloss/v2"
|
||||
|
||||
"github.com/MeghdadFadaee/domainops/internal/app"
|
||||
"github.com/MeghdadFadaee/domainops/internal/domain"
|
||||
@@ -713,6 +714,254 @@ func TestLoadingCommandPaletteRejectsMutationEntries(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveCredentialCapturesExactIDAndDisclosesAffectedRoutes(t *testing.T) {
|
||||
backend := &fakeBackend{}
|
||||
model := New(backend, Options{})
|
||||
model.screen = screenAccounts
|
||||
model.credentials = []domain.Credential{
|
||||
{ID: "credential-a", Label: "Primary", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid},
|
||||
{ID: "credential-b", Label: "Retiring", Provider: domain.ProviderCloudflare, Kind: domain.CredentialAccountToken, AccountHint: "account-main", Status: domain.CredentialValid},
|
||||
}
|
||||
model.zones = []domain.Zone{
|
||||
{ID: "zone-a", Name: "example.com", PreferredCredentialID: "credential-b"},
|
||||
{ID: "zone-b", Name: "example.net", PreferredCredentialID: "credential-b"},
|
||||
}
|
||||
model.rowOffset = 1
|
||||
|
||||
_, blink := model.Update(tea.KeyPressMsg{Code: 'd', Text: "d"})
|
||||
if blink == nil || !model.removeCredential || model.removeCredentialID != "credential-b" || model.removeCredentialInput.Value() != "" {
|
||||
t.Fatalf("remove form = open:%v id:%q input:%q cmd:%v", model.removeCredential, model.removeCredentialID, model.removeCredentialInput.Value(), blink != nil)
|
||||
}
|
||||
view := model.View().Content
|
||||
for _, expected := range []string{"Retiring", "credential-b", "account_token", "account-main", "2 preferred zone route", "example.com", "example.net", "reassigned"} {
|
||||
if !strings.Contains(view, expected) {
|
||||
t.Fatalf("remove form does not disclose %q: %q", expected, view)
|
||||
}
|
||||
}
|
||||
|
||||
// A refresh/reorder after opening must not redirect removal to the new row.
|
||||
_, _ = model.Update(loadMsg{credentials: []domain.Credential{
|
||||
{ID: "credential-b", Label: "Retiring", Provider: domain.ProviderCloudflare, Kind: domain.CredentialAccountToken, AccountHint: "account-main", Status: domain.CredentialValid},
|
||||
{ID: "credential-a", Label: "Primary", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid},
|
||||
}, zones: model.zones})
|
||||
model.removeCredentialInput.SetValue("Retiring")
|
||||
_, cmd := model.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
if cmd == nil || model.removeCredential || !model.busy {
|
||||
t.Fatalf("confirmed removal = cmd:%v open:%v busy:%v", cmd != nil, model.removeCredential, model.busy)
|
||||
}
|
||||
message, ok := cmd().(operationMsg)
|
||||
if !ok || !message.reload || message.err != nil {
|
||||
t.Fatalf("remove result = %#v", message)
|
||||
}
|
||||
if len(backend.deletedCredentials) != 1 || backend.deletedCredentials[0] != "credential-b" {
|
||||
t.Fatalf("deleted credential IDs = %v", backend.deletedCredentials)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveCredentialCancellationAndStaleConnectionAreSafe(t *testing.T) {
|
||||
backend := &fakeBackend{}
|
||||
model := New(backend, Options{})
|
||||
model.screen = screenAccounts
|
||||
model.credentials = []domain.Credential{{ID: "credential", Label: "Production", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid}}
|
||||
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: 'd', Text: "d"})
|
||||
model.removeCredentialInput.SetValue("Production")
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: tea.KeyEscape})
|
||||
if model.removeCredential || model.removeCredentialID != "" || model.removeCredentialExpected != "" || model.removeCredentialInput.Value() != "" {
|
||||
t.Fatalf("cancel retained remove state: open=%v id=%q label=%q input=%q", model.removeCredential, model.removeCredentialID, model.removeCredentialExpected, model.removeCredentialInput.Value())
|
||||
}
|
||||
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: 'd', Text: "d"})
|
||||
model.removeCredentialInput.SetValue("Production")
|
||||
model.credentials = nil
|
||||
_, cmd := model.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
if cmd != nil || model.err == nil || len(backend.deletedCredentials) != 0 {
|
||||
t.Fatalf("stale remove mutated: cmd=%v err=%v calls=%v", cmd != nil, model.err, backend.deletedCredentials)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveCredentialRejectsChangedAffectedRoutes(t *testing.T) {
|
||||
backend := &fakeBackend{}
|
||||
model := New(backend, Options{})
|
||||
model.screen = screenAccounts
|
||||
model.credentials = []domain.Credential{{ID: "credential", Label: "Production", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid}}
|
||||
model.zones = []domain.Zone{{ID: "zone-a", Name: "example.com", PreferredCredentialID: "credential"}}
|
||||
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: 'd', Text: "d"})
|
||||
model.removeCredentialInput.SetValue("Production")
|
||||
model.zones = append(model.zones, domain.Zone{ID: "zone-b", Name: "example.net", PreferredCredentialID: "credential"})
|
||||
_, cmd := model.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
if cmd != nil || model.err == nil || !strings.Contains(model.err.Error(), "affected zone routes changed") || len(backend.deletedCredentials) != 0 {
|
||||
t.Fatalf("changed route disclosure mutated: cmd=%v err=%v calls=%v", cmd != nil, model.err, backend.deletedCredentials)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveCredentialSurfacesFailClosedReplacementError(t *testing.T) {
|
||||
backend := &fakeBackend{deleteCredentialErr: errors.New("zone example.com has no proven replacement")}
|
||||
model := New(backend, Options{})
|
||||
model.screen = screenAccounts
|
||||
model.credentials = []domain.Credential{{ID: "credential", Label: "Production", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid}}
|
||||
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: 'd', Text: "d"})
|
||||
model.removeCredentialInput.SetValue("Production")
|
||||
_, cmd := model.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
if cmd == nil {
|
||||
t.Fatal("confirmed removal did not call backend")
|
||||
}
|
||||
_, reload := model.Update(cmd())
|
||||
if reload == nil || model.err == nil || !strings.Contains(model.err.Error(), "no proven replacement") {
|
||||
t.Fatalf("replacement failure = reload:%v err:%v", reload != nil, model.err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreferredCredentialRejectsAmbiguousLabelAndInvalidCandidates(t *testing.T) {
|
||||
model := New(&fakeBackend{}, Options{})
|
||||
zone := domain.Zone{ID: "zone", Name: "example.com", Provider: domain.ProviderCloudflare}
|
||||
model.screen = screenZones
|
||||
model.zones = []domain.Zone{zone}
|
||||
model.credentials = []domain.Credential{
|
||||
{ID: "credential-a", Label: "Production", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid},
|
||||
{ID: "credential-b", Label: "production", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid},
|
||||
{ID: "credential-invalid", Label: "Invalid", Provider: domain.ProviderCloudflare, Status: domain.CredentialInvalid},
|
||||
{ID: "credential-other", Label: "Other", Provider: "other", Status: domain.CredentialValid},
|
||||
}
|
||||
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: 'p', Text: "p"})
|
||||
if model.preferCredentialInputs[0].Value() != "" || model.preferCredentialInputs[1].Value() != "" {
|
||||
t.Fatalf("preference confirmation was prefilled: %#v", model.preferCredentialInputs)
|
||||
}
|
||||
model.preferCredentialInputs[0].SetValue("Production")
|
||||
_, cmd := model.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
if cmd != nil || model.preferCredentialFocus != 0 || model.err == nil || !strings.Contains(model.err.Error(), "ambiguous") {
|
||||
t.Fatalf("ambiguous label = cmd:%v focus:%d err:%v", cmd != nil, model.preferCredentialFocus, model.err)
|
||||
}
|
||||
for _, test := range []struct {
|
||||
value string
|
||||
want string
|
||||
}{{"credential-invalid", "invalid"}, {"credential-other", "provider"}} {
|
||||
model.preferCredentialInputs[0].SetValue(test.value)
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
if model.err == nil || !strings.Contains(model.err.Error(), test.want) {
|
||||
t.Fatalf("selector %q error = %v, want %q", test.value, model.err, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreferredCredentialCapturesIDAcrossRefreshAndCallsExactZone(t *testing.T) {
|
||||
backend := &fakeBackend{}
|
||||
model := New(backend, Options{})
|
||||
model.screen = screenZones
|
||||
model.zones = []domain.Zone{{ID: "zone-id", Name: "example.com", Provider: domain.ProviderCloudflare, PreferredCredentialID: "credential-old"}}
|
||||
model.credentials = []domain.Credential{
|
||||
{ID: "credential-old", Label: "Old", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid},
|
||||
{ID: "credential-selected", Label: "Replacement", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid},
|
||||
}
|
||||
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: 'p', Text: "p"})
|
||||
model.preferCredentialInputs[0].SetValue("Replacement")
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
if model.preferCredentialFocus != 1 || model.preferResolvedCredentialID != "credential-selected" {
|
||||
t.Fatalf("captured credential = focus:%d id:%q err:%v", model.preferCredentialFocus, model.preferResolvedCredentialID, model.err)
|
||||
}
|
||||
|
||||
// The label becomes ambiguous after capture. Final confirmation must retain
|
||||
// the immutable ID instead of resolving the label to a different row.
|
||||
_, _ = model.Update(loadMsg{zones: model.zones, credentials: []domain.Credential{
|
||||
{ID: "credential-new", Label: "Replacement", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid},
|
||||
{ID: "credential-selected", Label: "Replacement", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid},
|
||||
{ID: "credential-old", Label: "Old", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid},
|
||||
}})
|
||||
model.preferCredentialInputs[1].SetValue("example.com")
|
||||
_, cmd := model.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
if cmd == nil || model.preferCredential || !model.busy {
|
||||
t.Fatalf("preference submit = cmd:%v open:%v busy:%v err:%v", cmd != nil, model.preferCredential, model.busy, model.err)
|
||||
}
|
||||
message, ok := cmd().(operationMsg)
|
||||
if !ok || !message.reload || message.err != nil {
|
||||
t.Fatalf("preference result = %#v", message)
|
||||
}
|
||||
if fmt.Sprint(backend.preferredZones) != "[zone-id]" || fmt.Sprint(backend.preferredCredentials) != "[credential-selected]" {
|
||||
t.Fatalf("preferred calls = zones:%v credentials:%v", backend.preferredZones, backend.preferredCredentials)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreferredCredentialFormFitsMinimumTerminalAndShowsRoutingIdentity(t *testing.T) {
|
||||
model := New(&fakeBackend{}, Options{})
|
||||
_, _ = model.Update(tea.WindowSizeMsg{Width: 64, Height: 18})
|
||||
model.screen = screenZones
|
||||
model.accounts = []domain.RemoteAccount{{ID: "account-id", Name: "Primary"}}
|
||||
model.zones = []domain.Zone{{ID: "zone-id", Name: "example.com", Provider: domain.ProviderCloudflare, AccountID: "account-id", PreferredCredentialID: "credential-current"}}
|
||||
model.credentials = []domain.Credential{
|
||||
{ID: "credential-current", Label: "Current", Provider: domain.ProviderCloudflare, Kind: domain.CredentialUserToken, Status: domain.CredentialValid, Capabilities: []string{"dns:read", "dns:write"}},
|
||||
{ID: "credential-next", Label: "Next", Provider: domain.ProviderCloudflare, Kind: domain.CredentialAccountToken, AccountHint: "account-id", Status: domain.CredentialValid, Capabilities: []string{"dns:read"}},
|
||||
}
|
||||
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: 'p', Text: "p"})
|
||||
view := model.View().Content
|
||||
if height := lipgloss.Height(view); height > 18 {
|
||||
t.Fatalf("minimum-terminal preference modal height = %d\n%s", height, view)
|
||||
}
|
||||
for _, expected := range []string{"example.com", "zone-id", "Primary", "account-id", "credential-current", "Current", "current"} {
|
||||
if !strings.Contains(view, expected) {
|
||||
t.Fatalf("preference form does not show %q: %q", expected, view)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreferredCredentialCancellationAndStaleRouteAreSafe(t *testing.T) {
|
||||
backend := &fakeBackend{}
|
||||
model := New(backend, Options{})
|
||||
model.screen = screenZones
|
||||
model.zones = []domain.Zone{{ID: "zone", Name: "example.com", Provider: domain.ProviderCloudflare, PreferredCredentialID: "old"}}
|
||||
model.credentials = []domain.Credential{{ID: "new", Label: "New", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid}}
|
||||
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: 'p', Text: "p"})
|
||||
model.preferCredentialInputs[0].SetValue("new")
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: tea.KeyEscape})
|
||||
if model.preferCredential || model.preferZoneID != "" || model.preferResolvedCredentialID != "" || len(model.preferCredentialInputs) != 2 || model.preferCredentialInputs[0].Value() != "" {
|
||||
t.Fatalf("cancel retained preference state: %#v", model)
|
||||
}
|
||||
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: 'p', Text: "p"})
|
||||
model.preferCredentialInputs[0].SetValue("new")
|
||||
_, _ = model.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
model.zones[0].PreferredCredentialID = "changed-elsewhere"
|
||||
model.preferCredentialInputs[1].SetValue("example.com")
|
||||
_, cmd := model.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
if cmd != nil || model.err == nil || !strings.Contains(model.err.Error(), "route changed") || len(backend.preferredZones) != 0 {
|
||||
t.Fatalf("stale route mutated: cmd=%v err=%v calls=%v", cmd != nil, model.err, backend.preferredZones)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountAndZoneMutationsRespectRecoveryBusyAndLoadingGuards(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
screen screen
|
||||
key rune
|
||||
blocked func(*Model)
|
||||
}{
|
||||
{"remove recovery", screenAccounts, 'd', func(m *Model) { m.recoveryBlocked = true }},
|
||||
{"remove busy", screenAccounts, 'd', func(m *Model) { m.busy = true }},
|
||||
{"remove loading", screenAccounts, 'd', func(m *Model) { m.loading = true }},
|
||||
{"prefer recovery", screenZones, 'p', func(m *Model) { m.recoveryBlocked = true }},
|
||||
{"prefer busy", screenZones, 'p', func(m *Model) { m.busy = true }},
|
||||
{"prefer loading", screenZones, 'p', func(m *Model) { m.loading = true }},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
model := New(&fakeBackend{}, Options{})
|
||||
model.screen = test.screen
|
||||
model.credentials = []domain.Credential{{ID: "credential", Label: "Production", Provider: domain.ProviderCloudflare, Status: domain.CredentialValid}}
|
||||
model.zones = []domain.Zone{{ID: "zone", Name: "example.com", Provider: domain.ProviderCloudflare}}
|
||||
test.blocked(model)
|
||||
_, cmd := model.Update(tea.KeyPressMsg{Code: test.key, Text: string(test.key)})
|
||||
if cmd != nil || model.removeCredential || model.preferCredential || model.toast == "" {
|
||||
t.Fatalf("blocked mutation = cmd:%v remove:%v prefer:%v toast:%q", cmd != nil, model.removeCredential, model.preferCredential, model.toast)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFirstRunVaultRequiresConfirmation(t *testing.T) {
|
||||
backend := &fakeBackend{locked: true}
|
||||
model := New(backend, Options{FirstRun: true})
|
||||
@@ -754,21 +1003,26 @@ func TestExistingVaultAcceptsLegacyShortPassword(t *testing.T) {
|
||||
}
|
||||
|
||||
type fakeBackend struct {
|
||||
locked bool
|
||||
unlockCalls int
|
||||
unlockNilCalls int
|
||||
unlockErr error
|
||||
dnsZones []string
|
||||
zones []domain.Zone
|
||||
deleteCalls int
|
||||
tlsSettings map[string]domain.EdgeTLSSettings
|
||||
tlsErr error
|
||||
tlsZones []string
|
||||
updateTLSZones []string
|
||||
updateTLSDrafts []domain.EdgeTLSSettings
|
||||
issueRequests []app.IssueZonesRequest
|
||||
revokeLineages []string
|
||||
revokeConfirmations []string
|
||||
locked bool
|
||||
unlockCalls int
|
||||
unlockNilCalls int
|
||||
unlockErr error
|
||||
dnsZones []string
|
||||
zones []domain.Zone
|
||||
deleteCalls int
|
||||
deletedCredentials []string
|
||||
deleteCredentialErr error
|
||||
preferredZones []string
|
||||
preferredCredentials []string
|
||||
preferredCredentialErr error
|
||||
tlsSettings map[string]domain.EdgeTLSSettings
|
||||
tlsErr error
|
||||
tlsZones []string
|
||||
updateTLSZones []string
|
||||
updateTLSDrafts []domain.EdgeTLSSettings
|
||||
issueRequests []app.IssueZonesRequest
|
||||
revokeLineages []string
|
||||
revokeConfirmations []string
|
||||
}
|
||||
|
||||
func (f *fakeBackend) VaultLocked() bool { return f.locked }
|
||||
@@ -804,6 +1058,15 @@ func (f *fakeBackend) Audit(context.Context, int) ([]domain.AuditEvent, error) {
|
||||
func (f *fakeBackend) AddCloudflareCredential(context.Context, app.AddCredentialInput) (domain.Credential, error) {
|
||||
return domain.Credential{}, nil
|
||||
}
|
||||
func (f *fakeBackend) DeleteCredential(_ context.Context, credentialID string) error {
|
||||
f.deletedCredentials = append(f.deletedCredentials, credentialID)
|
||||
return f.deleteCredentialErr
|
||||
}
|
||||
func (f *fakeBackend) SetZonePreferredCredential(_ context.Context, zoneID, credentialID string) (domain.Zone, error) {
|
||||
f.preferredZones = append(f.preferredZones, zoneID)
|
||||
f.preferredCredentials = append(f.preferredCredentials, credentialID)
|
||||
return domain.Zone{ID: zoneID, PreferredCredentialID: credentialID}, f.preferredCredentialErr
|
||||
}
|
||||
func (f *fakeBackend) SyncAll(context.Context) ([]domain.Job, error) { return nil, nil }
|
||||
func (f *fakeBackend) CreateDNSRecord(context.Context, string, domain.DNSRecord) (domain.DNSRecord, error) {
|
||||
return domain.DNSRecord{}, nil
|
||||
|
||||
+133
-4
@@ -29,6 +29,12 @@ func (m *Model) render() string {
|
||||
if m.addCredential {
|
||||
return m.renderCredentialForm()
|
||||
}
|
||||
if m.removeCredential {
|
||||
return m.renderRemoveCredentialForm()
|
||||
}
|
||||
if m.preferCredential {
|
||||
return m.renderPreferredCredentialForm()
|
||||
}
|
||||
if m.editRecord {
|
||||
return m.renderRecordForm()
|
||||
}
|
||||
@@ -87,6 +93,12 @@ func (m *Model) renderHeader() string {
|
||||
func (m *Model) renderFooter() string {
|
||||
left := m.theme.key.Render("?") + " help " + m.theme.key.Render("Ctrl+K") + " commands " + m.theme.key.Render("/") + " filter"
|
||||
right := m.theme.key.Render("r") + " sync " + m.theme.key.Render("q") + " quit"
|
||||
switch m.screen {
|
||||
case screenAccounts:
|
||||
right = m.theme.key.Render("a") + " add " + m.theme.key.Render("d") + " remove " + m.theme.key.Render("q") + " quit"
|
||||
case screenZones:
|
||||
right = m.theme.key.Render("p") + " set route " + m.theme.key.Render("Enter") + " DNS " + m.theme.key.Render("q") + " quit"
|
||||
}
|
||||
if m.recoveryBlocked {
|
||||
right = m.theme.key.Render("R") + " retry recovery " + m.theme.key.Render("q") + " quit"
|
||||
}
|
||||
@@ -221,7 +233,7 @@ func (m *Model) renderAccounts(width, height int) string {
|
||||
if len(values) == 0 {
|
||||
return m.emptyState("No Cloudflare accounts", "Press a to add a scoped API token.", width, height)
|
||||
}
|
||||
header := row(width, []cell{{"LABEL", 28}, {"TOKEN", 18}, {"STATUS", 14}, {"CAPABILITIES", 40}})
|
||||
header := row(width, []cell{{"LABEL", 24}, {"CREDENTIAL ID", 28}, {"TOKEN", 16}, {"STATUS", 12}, {"CAPABILITIES", 30}})
|
||||
rows := []string{m.theme.mutedText.Render(header)}
|
||||
reserved := 0
|
||||
if len(m.accounts) > 0 {
|
||||
@@ -232,7 +244,7 @@ func (m *Model) renderAccounts(width, height int) string {
|
||||
for i := start; i < len(values) && i < start+limit; i++ {
|
||||
value := values[i]
|
||||
status := string(value.Status)
|
||||
line := row(width, []cell{{value.Label, 28}, {string(value.Kind), 18}, {status, 14}, {strings.Join(value.Capabilities, ", "), 40}})
|
||||
line := row(width, []cell{{value.Label, 24}, {value.ID, 28}, {string(value.Kind), 16}, {status, 12}, {strings.Join(value.Capabilities, ", "), 30}})
|
||||
if i == m.rowOffset {
|
||||
line = m.theme.selected.Width(width - 2).Render(line)
|
||||
}
|
||||
@@ -245,7 +257,7 @@ func (m *Model) renderAccounts(width, height int) string {
|
||||
}
|
||||
rows = append(rows, "", m.theme.panelTitle.Render(fmt.Sprintf("REMOTE ACCOUNTS · %d", len(m.accounts))), m.theme.mutedText.Render(truncate(strings.Join(names, " · "), max(1, width-6))))
|
||||
}
|
||||
return m.panel("CLOUDFLARE CONNECTIONS · a add · r sync", strings.Join(rows, "\n"), width, height)
|
||||
return m.panel("CLOUDFLARE CONNECTIONS · a add · d safely remove · r sync", strings.Join(rows, "\n"), width, height)
|
||||
}
|
||||
|
||||
func (m *Model) renderZones(width, height int) string {
|
||||
@@ -265,7 +277,7 @@ func (m *Model) renderZones(width, height int) string {
|
||||
}
|
||||
rows = append(rows, line)
|
||||
}
|
||||
return m.panel("ZONES · Enter open DNS · j/k select", strings.Join(rows, "\n"), width, height)
|
||||
return m.panel("ZONES · Enter open DNS · p set preferred connection · j/k select", strings.Join(rows, "\n"), width, height)
|
||||
}
|
||||
|
||||
func (m *Model) accountLabel(id string) string {
|
||||
@@ -527,6 +539,122 @@ func (m *Model) renderCredentialForm() string {
|
||||
return m.theme.app.Width(m.width).Height(m.height).Render(lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, modal))
|
||||
}
|
||||
|
||||
func (m *Model) renderRemoveCredentialForm() string {
|
||||
contentWidth := max(20, min(70, m.width-16))
|
||||
affected := "No currently preferred zone routes use this connection."
|
||||
if len(m.removeCredentialZoneIDs) > 0 {
|
||||
names := m.removeCredentialZoneNames
|
||||
shown := names
|
||||
if len(shown) > 4 {
|
||||
shown = shown[:4]
|
||||
}
|
||||
affected = fmt.Sprintf("%d preferred zone route(s): %s", len(names), strings.Join(shown, ", "))
|
||||
if len(names) > len(shown) {
|
||||
affected += fmt.Sprintf(" … and %d more", len(names)-len(shown))
|
||||
}
|
||||
}
|
||||
hint := m.removeCredentialAccountHint
|
||||
if hint == "" {
|
||||
hint = "all discovered accounts"
|
||||
}
|
||||
lines := []string{
|
||||
m.theme.brand.Render("REMOVE CLOUDFLARE CONNECTION / TOKEN"),
|
||||
m.theme.mutedText.Render(truncate("Connection: "+m.removeCredentialExpected+" · "+m.removeCredentialID, contentWidth)),
|
||||
m.theme.mutedText.Render(truncate(fmt.Sprintf("Token: %s · account scope: %s", m.removeCredentialKind, hint), contentWidth)),
|
||||
m.theme.mutedText.Render(truncate(affected, contentWidth)),
|
||||
m.theme.statusWarn.Render("Routes may be reassigned after live verification."),
|
||||
m.theme.statusWarn.Render("Fails closed unless every route has a proven replacement."),
|
||||
m.theme.panelTitle.Render("Exact connection label"),
|
||||
m.removeCredentialInput.View(),
|
||||
}
|
||||
if m.err != nil {
|
||||
lines = append(lines, m.theme.statusBad.Render(truncate("! "+m.err.Error(), contentWidth)))
|
||||
}
|
||||
lines = append(lines, m.theme.mutedText.Render("Enter to remove · Esc to cancel"))
|
||||
modal := m.theme.modal.Width(min(78, m.width-8)).Render(strings.Join(lines, "\n"))
|
||||
return m.theme.app.Width(m.width).Height(m.height).Render(lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, modal))
|
||||
}
|
||||
|
||||
func (m *Model) renderPreferredCredentialForm() string {
|
||||
contentWidth := max(20, m.width-16)
|
||||
current := "automatic / none"
|
||||
if m.preferOriginalCredentialID != "" {
|
||||
current = m.credentialLabel(m.preferOriginalCredentialID) + " · " + m.preferOriginalCredentialID
|
||||
}
|
||||
account := m.accountLabel(m.preferZoneAccountID)
|
||||
if account == "" {
|
||||
account = "unknown"
|
||||
}
|
||||
selectorLabel := m.theme.mutedText.Render("Credential ID or unique label")
|
||||
confirmLabel := m.theme.mutedText.Render("Confirm exact zone name")
|
||||
if m.preferCredentialFocus == 0 {
|
||||
selectorLabel = m.theme.panelTitle.Render("Credential ID or unique label")
|
||||
} else {
|
||||
confirmLabel = m.theme.panelTitle.Render("Confirm exact zone name")
|
||||
}
|
||||
lines := []string{
|
||||
m.theme.brand.Render("SET PREFERRED ZONE CONNECTION"),
|
||||
m.theme.mutedText.Render(truncate("Zone: "+m.preferZoneExpectedName+" · "+m.preferZoneID, contentWidth)),
|
||||
m.theme.mutedText.Render(truncate("Account: "+account+" · "+m.preferZoneAccountID, contentWidth)),
|
||||
m.theme.mutedText.Render(truncate("Current route: "+current, contentWidth)),
|
||||
selectorLabel,
|
||||
m.preferCredentialInputs[0].View(),
|
||||
confirmLabel,
|
||||
m.preferCredentialInputs[1].View(),
|
||||
m.theme.mutedText.Render("Connections · DNS read verified on apply:"),
|
||||
}
|
||||
validCandidates := 0
|
||||
for _, credential := range m.credentials {
|
||||
if credential.Provider == m.preferZoneProvider && credential.Status == domain.CredentialValid {
|
||||
validCandidates++
|
||||
}
|
||||
}
|
||||
candidateLimit := max(1, min(6, (m.height-16)/2))
|
||||
shown := 0
|
||||
for _, credential := range m.credentials {
|
||||
if credential.Provider != m.preferZoneProvider || credential.Status != domain.CredentialValid {
|
||||
continue
|
||||
}
|
||||
marker := " "
|
||||
if credential.ID == m.preferOriginalCredentialID {
|
||||
marker = "*"
|
||||
}
|
||||
hint := credential.AccountHint
|
||||
if hint == "" {
|
||||
hint = "all discovered accounts"
|
||||
}
|
||||
writeState := "dns:write observed before"
|
||||
if !containsString(credential.Capabilities, "dns:write") {
|
||||
writeState = "WRITE UNPROVEN"
|
||||
}
|
||||
labelWidth := max(1, contentWidth-len(credential.ID)-5)
|
||||
candidate := fmt.Sprintf("%s %s · %s", marker, credential.ID, truncate(credential.Label, labelWidth))
|
||||
details := fmt.Sprintf(" %s · %s · %s · %s", writeState, credential.Kind, hint, strings.Join(credential.Capabilities, ","))
|
||||
lines = append(lines, m.theme.mutedText.Render(truncate(candidate, contentWidth)))
|
||||
if writeState == "WRITE UNPROVEN" {
|
||||
lines = append(lines, m.theme.statusWarn.Render(truncate(details, contentWidth)))
|
||||
} else {
|
||||
lines = append(lines, m.theme.mutedText.Render(truncate(details, contentWidth)))
|
||||
}
|
||||
shown++
|
||||
if shown == candidateLimit {
|
||||
break
|
||||
}
|
||||
}
|
||||
if shown == 0 {
|
||||
lines = append(lines, m.theme.statusWarn.Render(" No valid synchronized connection matches this zone provider."))
|
||||
} else if validCandidates > shown {
|
||||
lines = append(lines, m.theme.mutedText.Render(truncate(fmt.Sprintf(" … and %d more; full IDs are visible in Accounts", validCandidates-shown), contentWidth)))
|
||||
}
|
||||
lines = append(lines, m.theme.mutedText.Render("* current · write proven only by a real mutation"))
|
||||
if m.err != nil {
|
||||
lines = append(lines, m.theme.statusBad.Render(truncate("! "+m.err.Error(), contentWidth)))
|
||||
}
|
||||
lines = append(lines, m.theme.mutedText.Render("Tab move · Enter verify/apply · Esc cancel"))
|
||||
modal := m.theme.modal.Width(min(84, m.width-8)).Render(strings.Join(lines, "\n"))
|
||||
return m.theme.app.Width(m.width).Height(m.height).Render(lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, modal))
|
||||
}
|
||||
|
||||
func (m *Model) renderRecordForm() string {
|
||||
labels := []string{"Record type", "Name", "Content", "TTL", "Priority"}
|
||||
title := "CREATE DNS RECORD"
|
||||
@@ -661,6 +789,7 @@ func (m *Model) renderHelp() string {
|
||||
m.theme.key.Render("1…7") + " Navigate sections", m.theme.key.Render("j / k") + " Move selection",
|
||||
m.theme.key.Render("/") + " Filter current view", m.theme.key.Render("Ctrl+K") + " Command palette",
|
||||
m.theme.key.Render("r") + " Synchronize", m.theme.key.Render("a") + " Add Cloudflare connection",
|
||||
m.theme.key.Render("d") + " Safely remove selected connection (Accounts)", m.theme.key.Render("p") + " Set preferred zone connection (Zones)",
|
||||
m.theme.key.Render("n / e / d") + " New, edit, delete DNS record", m.theme.key.Render("?") + " Close this help",
|
||||
m.theme.key.Render("n / u / i / x / d") + " Issue, renew due, import, export, revoke certificate",
|
||||
"", m.theme.mutedText.Render("All operations are keyboard-accessible and safe over SSH."),
|
||||
|
||||
Reference in new issue
Block a user