Implement single DNS entry update feature: add functionality to manually refresh DNS for the selected entry, including key binding and validation checks.

This commit is contained in:
Philip Henning 2025-08-18 14:13:03 +02:00
parent 26b4080631
commit 7f09c56aa2
3 changed files with 90 additions and 2 deletions

View file

@ -1,10 +1,36 @@
# Active Context
## Current Status: DNS Resolution Bug Fixed - COMPLETED! 🎉
## Current Status: Single DNS Update Feature Added - COMPLETED! 🎉
**Last Updated:** 2025-01-18 13:53 CET
**Last Updated:** 2025-01-18 14:11 CET
## Recent Achievement
Successfully added a new single DNS update feature to the hosts TUI application! Users can now press 'r' to update the DNS resolution for just the currently selected entry, providing more granular control over DNS updates.
### New Feature Implementation
Added manual single DNS entry update functionality with key binding "r":
**Key Features:**
- **Single Entry Focus:** Updates only the currently selected entry instead of all DNS entries
- **Smart Validation:** Checks if the selected entry has a DNS name before attempting resolution
- **User Feedback:** Provides clear status messages for success, failure, and validation errors
- **Consistent Behavior:** Uses the same DNS resolution logic as the bulk refresh (Shift+R)
**Technical Implementation:**
- Added `Binding("r", "update_single_dns", "Update DNS", show=False, id="left:update_single_dns")` to keybindings
- Implemented `action_update_single_dns()` method in `src/hosts/tui/app.py`
- Follows established patterns from `action_refresh_dns()` for consistency
- Properly updates both `ip_address` and `resolved_ip` fields to ensure hosts file gets updated
- Saves changes automatically after successful DNS resolution
**User Experience:**
- **Before:** Users had to refresh all DNS entries at once (potentially slow with many entries)
- **After:** Users can quickly update individual entries as needed
- **Validation:** Clear error message if selected entry has no DNS name: "❌ Selected entry has no DNS name to resolve"
- **Progress:** Shows "🔄 Resolving DNS for {dns_name}..." during resolution
- **Results:** Success shows "✅ DNS updated: {dns_name} → {resolved_ip}" or detailed error messages
### Previous Achievement: DNS Resolution Bug Fixed
Successfully identified and fixed a critical DNS resolution bug in the hosts TUI application! The DNS resolution functionality was working, but entries were not being updated properly in the hosts file.
### Problem Analysis

View file

@ -785,6 +785,67 @@ class HostsManagerApp(App):
self.run_worker(refresh_dns(), exclusive=False)
self.update_status("🔄 Starting DNS resolution...")
def action_update_single_dns(self) -> None:
"""Manually refresh DNS resolution for the currently selected entry."""
if not self.hosts_file.entries:
self.update_status("No entries available")
return
if self.selected_entry_index >= len(self.hosts_file.entries):
self.update_status("Invalid entry selected")
return
entry = self.hosts_file.entries[self.selected_entry_index]
# Check if the entry has a DNS name to resolve
if not hasattr(entry, 'dns_name') or not entry.dns_name:
self.update_status("❌ Selected entry has no DNS name to resolve")
return
async def update_single_dns():
try:
dns_name = entry.dns_name
# Resolve the DNS name
resolution = await self.dns_service.resolve_entry_async(dns_name)
# Apply resolution results to entry fields
entry.last_resolved = resolution.resolved_at
entry.dns_resolution_status = resolution.status.value
if resolution.is_success():
# Update both resolved_ip and ip_address for the hosts file
entry.ip_address = resolution.resolved_ip
entry.resolved_ip = resolution.resolved_ip
# Save hosts file with updated DNS information
save_success, save_message = self.manager.save_hosts_file(self.hosts_file)
if not save_success:
self.update_status(f"❌ DNS resolution completed but save failed: {save_message}")
return
# Update the UI
self.table_handler.populate_entries_table()
self.details_handler.update_entry_details()
self.update_status(f"✅ DNS updated: {dns_name}{resolution.resolved_ip}")
else:
# Resolution failed, save the status update
save_success, save_message = self.manager.save_hosts_file(self.hosts_file)
if save_success:
# Update the UI to show failed status
self.table_handler.populate_entries_table()
self.details_handler.update_entry_details()
error_msg = resolution.error_message or "Unknown error"
self.update_status(f"❌ DNS resolution failed for {dns_name}: {error_msg}")
except Exception as e:
self.update_status(f"❌ DNS resolution error: {e}")
# Run DNS resolution in background
self.run_worker(update_single_dns(), exclusive=False)
self.update_status(f"🔄 Resolving DNS for {entry.dns_name}...")
def action_show_filters(self) -> None:
"""Show advanced filtering modal."""

View file

@ -45,6 +45,7 @@ HOSTS_MANAGER_BINDINGS = [
Binding("ctrl+z", "undo", "Undo", show=False, id="left:undo"),
Binding("ctrl+y", "redo", "Redo", show=False, id="left:redo"),
Binding("R", "refresh_dns", "Refresh DNS", show=False, id="left:refresh_dns"),
Binding("r", "update_single_dns", "Update DNS", show=False, id="left:update_single_dns"),
Binding("escape", "exit_edit_entry", "Exit edit mode", show=False),
Binding("tab", "next_field", "Next field", show=False),
Binding("shift+tab", "prev_field", "Previous field", show=False),