<commit_before>// Package dns ...
package dns

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

type DomainName struct {
	IP     string
	Domain string
}

var (
	hostsFile = detectHostsFile()
	newline   = detectNewlineChar()
)

// Entry generate the DNS entry to be added
func Entry(ip, name, env string) string {
	return fmt.Sprintf("%s     %s # dns added for '%s' by nanobox", ip, name, env)
}

// Exists ...
func Exists(entry string) bool {

	// open the hosts file for scanning...
	f, err := os.Open(hostsFile)
	if err != nil {
		return false
	}
	defer f.Close()

	// scan each line of the hosts file to see if there is a match for this
	// entry
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		if scanner.Text() == entry {
			return true
		}
	}

	return false
}

func List(filter string) []DomainName {

	// open the hosts file
	f, err := os.Open(hostsFile)
	if err != nil {
		return nil
	}
	defer f.Close()

	entries := []DomainName{}

	// scan each line of the hosts file to see if there is a match for this
	// entry
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		if strings.Contains(scanner.Text(), filter) {
			fields := strings.Fields(scanner.Text())
			if len(fields) >= 2 {
				entries = append(entries, DomainName{IP: fields[0], Domain: fields[1]})
			}
		}
	}

	return entries
}

// Add ...
func Add(entry string) error {

	// open hosts file
	f, err := os.OpenFile(hostsFile, os.O_RDWR|os.O_APPEND, 0644)
	if err != nil {
		return err
	}
	defer f.Close()

	// write the DNS entry to the file
	if _, err := f.WriteString(fmt.Sprintf("%s%s", entry, newline)); err != nil {
		return err
	}

	return nil
}

// Remove ...
func Remove(entry string) error {

	// "contents" will end up storing the entire contents of the file excluding the
	// entry that is trying to be removed
	var contents string
<commit_msg>make the util/dns package not wipe out your /etc/hosts file<commit_after>// Package dns ...
package dns

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

type DomainName struct {
	IP     string
	Domain string
}

var (
	hostsFile = detectHostsFile()
	newline   = detectNewlineChar()
)

// Entry generate the DNS entry to be added
func Entry(ip, name, env string) string {
	return fmt.Sprintf("%s     %s # dns added for '%s' by nanobox", ip, name, env)
}

// Exists ...
func Exists(entry string) bool {

	// open the hosts file for scanning...
	f, err := os.Open(hostsFile)
	if err != nil {
		return false
	}
	defer f.Close()

	// scan each line of the hosts file to see if there is a match for this
	// entry
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		if scanner.Text() == entry {
			return true
		}
	}

	return false
}

func List(filter string) []DomainName {

	// open the hosts file
	f, err := os.Open(hostsFile)
	if err != nil {
		return nil
	}
	defer f.Close()

	entries := []DomainName{}

	// scan each line of the hosts file to see if there is a match for this
	// entry
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		if strings.Contains(scanner.Text(), filter) {
			fields := strings.Fields(scanner.Text())
			if len(fields) >= 2 {
				entries = append(entries, DomainName{IP: fields[0], Domain: fields[1]})
			}
		}
	}

	return entries
}

// Add ...
func Add(entry string) error {
	if entry == "" {
		return nil
	}

	// open hosts file
	f, err := os.OpenFile(hostsFile, os.O_RDWR|os.O_APPEND, 0644)
	if err != nil {
		return err
	}
	defer f.Close()

	// write the DNS entry to the file
	if _, err := f.WriteString(fmt.Sprintf("%s%s", entry, newline)); err != nil {
		return err
	}

	return nil
}

// Remove ...
func Remove(entry string) error {
	if entry == "" {
		return nil
	}
	// "contents" will end up storing the entire contents of the file excluding the
	// entry that is trying to be removed
	var contents string

{'fraction_non_alphanumeric': 0.1316688567674113, 'fraction_numerical': 0.0036793692509855453, 'mean_word_length': 2.956340956340956, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3987438', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1319, 'n_words': 459}