Skip to content

osal.hostsfile #

Hosts File Manager

This module provides functionality to manage the system's hosts file (/etc/hosts) in a safe and structured way. It supports both Linux and macOS systems, automatically handling sudo permissions when required.

Features

  • Read and parse the system's hosts file
  • Add new host entries with IP and domain
  • Remove host entries by domain
  • Manage sections with comments
  • Remove or clear entire sections
  • Check for existing domains
  • Automatic sudo handling for macOS and Linux when needed

Usage

Create a file example.vsh:

#!/usr/bin/env -S v -n -w -gc none  -cc tcc -d use_openssl -enable-globals run

import freeflowuniverse.herolib.osal.hostsfile
import os

// Create a new instance by reading the hosts file
mut hosts := hostsfile.new() or {
    eprintln('Failed to read hosts file: ${err}')
    exit(1)
}

// Add a new host entry to a section
hosts.add_host('127.0.0.1', 'mysite.local', 'Development') or {
    eprintln('Failed to add host: ${err}')
    exit(1)
}

// Remove a host entry
hosts.remove_host('mysite.local') or {
    eprintln('Failed to remove host: ${err}')
    exit(1)
}

// Check if a domain exists
if hosts.exists('example.com') {
    println('Domain exists')
}

// Clear all entries in a section
hosts.clear_section('Development') or {
    eprintln('Failed to clear section: ${err}')
    exit(1)
}

// Remove an entire section
hosts.remove_section('Development') or {
    eprintln('Failed to remove section: ${err}')
    exit(1)
}

// Save changes back to the hosts file
// This will automatically use sudo when needed
hosts.save() or {
    eprintln('Failed to save hosts file: ${err}')
    exit(1)
}

File Structure

The hosts file is organized into sections marked by comments. For example:

# Development
127.0.0.1    localhost
127.0.0.1    mysite.local

# Production
192.168.1.100    prod.example.com

Error Handling

All functions that can fail return a Result type and should be handled appropriately:

hosts.add_host('127.0.0.1', 'mysite.local', 'Development') or {
    eprintln('Failed to add host: ${err}')
    exit(1)
}

Platform Support

  • Linux: Direct write with fallback to sudo if needed
  • macOS: Always uses sudo due to system restrictions

fn new #

fn new() !HostsFile

new creates a new HostsFile instance by reading the system's hosts file

struct Host #

struct Host {
pub mut:
	ip     string
	domain string
}

struct HostsFile #

@[heap]
struct HostsFile {
pub mut:
	sections []Section
}

fn (HostsFile) add_host #

fn (mut h HostsFile) add_host(ip string, domain string, section string) !

add_host adds a new host entry to the specified section

fn (HostsFile) remove_host #

fn (mut h HostsFile) remove_host(domain string) !

remove_host removes all entries for the specified domain

fn (HostsFile) exists #

fn (h &HostsFile) exists(domain string) bool

exists checks if a domain exists in any section

fn (HostsFile) save #

fn (h &HostsFile) save() !

save writes the hosts file back to disk

fn (HostsFile) remove_section #

fn (mut h HostsFile) remove_section(section_name string) !

remove_section removes an entire section and its hosts

fn (HostsFile) clear_section #

fn (mut h HostsFile) clear_section(section_name string) !

clear_section removes all hosts from a section but keeps the section

struct Section #

struct Section {
pub mut:
	name  string
	hosts []Host
}