mirror of
				https://github.com/shokinn/hosts-go.git
				synced 2025-11-04 04:28:34 +00:00 
			
		
		
		
	feat: begin tui implementation
This commit is contained in:
		
							parent
							
								
									b81f11f711
								
							
						
					
					
						commit
						1b66db10e2
					
				
					 9 changed files with 200 additions and 154 deletions
				
			
		| 
						 | 
				
			
			@ -1,137 +1,22 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"hosts-go/internal/core"
 | 
			
		||||
	"hosts-go/internal/tui"
 | 
			
		||||
 | 
			
		||||
	tea "github.com/charmbracelet/bubbletea"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Println("hosts-go - Phase 1: Core Functionality (Parser)")
 | 
			
		||||
	fmt.Println("===============================================")
 | 
			
		||||
 | 
			
		||||
	// Demonstrate hosts file parsing with sample content
 | 
			
		||||
	sampleHostsContent := `# Sample hosts file content
 | 
			
		||||
127.0.0.1	localhost	# Local loopback
 | 
			
		||||
::1		ip6-localhost	# IPv6 loopback
 | 
			
		||||
192.168.1.100	dev.example.com	www.dev.example.com	api.dev.example.com	# Development server
 | 
			
		||||
# 10.0.0.50	staging.example.com	# Disabled staging server
 | 
			
		||||
203.0.113.10	prod.example.com	# Production server
 | 
			
		||||
 | 
			
		||||
# Another comment
 | 
			
		||||
::ffff:192.168.1.200	test.example.com	# Test server`
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Sample hosts file content:")
 | 
			
		||||
	fmt.Println(strings.Repeat("-", 50))
 | 
			
		||||
	fmt.Println(sampleHostsContent)
 | 
			
		||||
	fmt.Println(strings.Repeat("-", 50))
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
 | 
			
		||||
	// Parse the sample content
 | 
			
		||||
	lines := strings.Split(sampleHostsContent, "\n")
 | 
			
		||||
	hostsFile, warnings, err := core.ParseHostsContent(lines)
 | 
			
		||||
	hostsFile, _, err := core.ParseHostsFile("/etc/hosts")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalf("Failed to parse hosts content: %v", err)
 | 
			
		||||
		log.Fatalf("failed to parse hosts file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Display parsing results
 | 
			
		||||
	fmt.Printf("✅ Parsing successful!\n")
 | 
			
		||||
	fmt.Printf("   Total entries: %d\n", len(hostsFile.Entries))
 | 
			
		||||
	fmt.Printf("   Active entries: %d\n", len(hostsFile.ActiveEntries()))
 | 
			
		||||
	fmt.Printf("   Standalone comments: %d\n", len(hostsFile.Comments))
 | 
			
		||||
	fmt.Printf("   Warnings: %d\n", len(warnings))
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
 | 
			
		||||
	// Show warnings if any
 | 
			
		||||
	if len(warnings) > 0 {
 | 
			
		||||
		fmt.Println("Parsing warnings:")
 | 
			
		||||
		for _, warning := range warnings {
 | 
			
		||||
			fmt.Printf("  Line %d: %s\n", warning.Line, warning.Message)
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Println()
 | 
			
		||||
	p := tea.NewProgram(tui.NewModel(hostsFile))
 | 
			
		||||
	if err := p.Start(); err != nil {
 | 
			
		||||
		log.Fatalf("failed to start TUI: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Show standalone comments
 | 
			
		||||
	if len(hostsFile.Comments) > 0 {
 | 
			
		||||
		fmt.Println("Standalone comments found:")
 | 
			
		||||
		for i, comment := range hostsFile.Comments {
 | 
			
		||||
			fmt.Printf("%d. %s\n", i+1, comment)
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Println()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Show parsed entries
 | 
			
		||||
	fmt.Println("Parsed entries:")
 | 
			
		||||
	for i, entry := range hostsFile.Entries {
 | 
			
		||||
		status := "✅ Active"
 | 
			
		||||
		if !entry.Active {
 | 
			
		||||
			status = "❌ Disabled"
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Printf("%d. [%s] %s -> %s", i+1, status, entry.IP, entry.Hostname)
 | 
			
		||||
		if len(entry.Aliases) > 0 {
 | 
			
		||||
			fmt.Printf(" (aliases: %s)", strings.Join(entry.Aliases, ", "))
 | 
			
		||||
		}
 | 
			
		||||
		if entry.Comment != "" {
 | 
			
		||||
			fmt.Printf(" # %s", entry.Comment)
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Println()
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
 | 
			
		||||
	// Demonstrate intelligent formatting
 | 
			
		||||
	fmt.Println("Intelligent formatting output:")
 | 
			
		||||
	fmt.Println(strings.Repeat("-", 50))
 | 
			
		||||
	formattedLines := core.FormatHostsFile(hostsFile)
 | 
			
		||||
	for _, line := range formattedLines {
 | 
			
		||||
		fmt.Println(line)
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(strings.Repeat("-", 50))
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
 | 
			
		||||
	// Demonstrate formatting style detection
 | 
			
		||||
	fmt.Println("Formatting style detection:")
 | 
			
		||||
	style := core.DetectFormattingStyle(lines)
 | 
			
		||||
	if style.UseTabs {
 | 
			
		||||
		fmt.Printf("  Detected style: Tabs\n")
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Printf("  Detected style: Spaces (%d per tab)\n", style.SpacesPerTab)
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Printf("  Column widths: IP=%d, Host=%d\n", style.IPWidth, style.HostWidth)
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
 | 
			
		||||
	// Demonstrate search functionality
 | 
			
		||||
	fmt.Println("Search demonstrations:")
 | 
			
		||||
	if found := hostsFile.FindEntry("localhost"); found != nil {
 | 
			
		||||
		fmt.Printf("✅ Found 'localhost': %s -> %s\n", found.IP, found.Hostname)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if found := hostsFile.FindEntry("www.dev.example.com"); found != nil {
 | 
			
		||||
		fmt.Printf("✅ Found alias 'www.dev.example.com': %s -> %s\n", found.IP, found.Hostname)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if found := hostsFile.FindEntry("staging.example.com"); found != nil {
 | 
			
		||||
		status := "active"
 | 
			
		||||
		if !found.Active {
 | 
			
		||||
			status = "disabled"
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Printf("✅ Found 'staging.example.com': %s -> %s (%s)\n", found.IP, found.Hostname, status)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if found := hostsFile.FindEntry("notfound.com"); found == nil {
 | 
			
		||||
		fmt.Printf("❌ 'notfound.com' not found (as expected)\n")
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
 | 
			
		||||
	fmt.Println("🎉 Phase 1 Complete: Core Functionality (Parser)")
 | 
			
		||||
	fmt.Println("✅ Hosts file parsing with format preservation")
 | 
			
		||||
	fmt.Println("✅ Comment and disabled entry handling")
 | 
			
		||||
	fmt.Println("✅ Intelligent formatting with column alignment")
 | 
			
		||||
	fmt.Println("✅ Malformed line handling with warnings")
 | 
			
		||||
	fmt.Println("✅ Round-trip parsing (parse → format → parse)")
 | 
			
		||||
	fmt.Println("✅ Backup functionality")
 | 
			
		||||
	fmt.Println("✅ Search and entry management")
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
	fmt.Println("Ready for Phase 2: TUI Implementation!")
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue