go-hostpital

module
v0.1.0-beta2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2024 License: MIT

README

Go Reference

go-hostpital

  • hostpital is a simple library written in go to maintain and manage hosts files.
  • hostpital es una sencilla librería escrita en go para mantener y gestionar archivos hosts.
  • hostpital は、hosts ファイルを維持・管理するための go で書かれたシンプルなライブラリです。

Usage

go get "github.com/KEINOS/go-hostpital"
import "github.com/KEINOS/go-hostpital/hostpital"

func ExampleValidator() {
    // Validator with default settings
    validator := hostpital.NewValidator()

    validator.AllowComment = true    // Allow comment lines in the hostfile.
    validator.IDNACompatible = false // Want RFC 6125 2.2 compatibility. If true, IDNA2008 compatible.

    // Validate a file
    pathFile := filepath.Join("testdata", "hosts.txt")

    if validator.ValidateFile(pathFile) {
        fmt.Println("The hostfile is valid.")
    }

    // Output: The hostfile is valid.
}
import "github.com/KEINOS/go-hostpital/hostpital"

func ExampleParser() {
    // For the default settings, see the NewValidator() example.
    parser := hostpital.NewParser()

    // Set the IP address to use for all the hosts. Suitable for DNS sinkhole.
    parser.UseIPAddress = "0.0.0.0"

    // Parse a file to clean up
    pathFile := filepath.Join("testdata", "hosts.txt")

    parsed, err := parser.ParseFile(pathFile)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(parsed)
    // Output:
    // 0.0.0.0 dummy1.example.com
    // 0.0.0.0 dummy2.example.com
    // 0.0.0.0 dummy3.example.com
    // 0.0.0.0 dummy4.example.com
    // 0.0.0.0 dummy5.example.com dummy6.example.com
}
import "github.com/KEINOS/go-hostpital/hostpital"

// Various functions
func Example() {
    // Detects IPV4 and IPV6
    fmt.Println("IsIPAddress(\"0.0.0.0\") -->", hostpital.IsIPAddress("0.0.0.0"))
    fmt.Println("IsIPAddress(\"::\") -->", hostpital.IsIPAddress("::"))
    fmt.Println("IsIPAddress(\"0.0.0.0.0\") -->", hostpital.IsIPAddress("0.0.0.0.0"))

    // True if host name is ready for registration. False if it is a raw punycode or not IDNA2008 compatible.
    fmt.Println("IsIDNAComatible(\"xn--gpher-jua.com\") -->", hostpital.IsIDNAComatible("xn--gpher-jua.com"))
    fmt.Println("IsIDNAComatible(\"göpher.com\") -->", hostpital.IsIDNAComatible("göpher.com"))

    // ASCII/Punycode <---> Unicode conversion
    hostASCII, err := hostpital.TransformToASCII("göpher.com")
    fmt.Println("TransformToASCII(\"göpher.com\") -->", hostASCII, err)

    hostUnicode, err := hostpital.TransformToUnicode("xn--gpher-jua.com")
    fmt.Println("TransformToUnicode(\"xn--gpher-jua.com\") -->", hostUnicode, err)

    // Trim a comment from a line
    hostTrimmed, err := hostpital.TrimComment("127.0.0.0 localhost # this is a line comment")
    fmt.Println("TrimComments(\"127.0.0.0 localhost # this is a line comment\") --->", hostTrimmed, err)

    /* And more ... */

    // Output:
    // IsIPAddress("0.0.0.0") --> true
    // IsIPAddress("::") --> true
    // IsIPAddress("0.0.0.0.0") --> false
    // IsIDNAComatible("xn--gpher-jua.com") --> true
    // IsIDNAComatible("göpher.com") --> false
    // TransformToASCII("göpher.com") --> xn--gpher-jua.com <nil>
    // TransformToPunycode("xn--gpher-jua.com") --> göpher.com <nil>
    // TrimComments("127.0.0.0 localhost # this is a line comment") ---> 127.0.0.0 localhost  <nil>
}

Statuses

UnitTests golangci-lint CodeQL-Analysis PlatformTests

codecov Go Report Card

Contributing

Go Reference

Directories

Path Synopsis
cmd
Package hostpital provides functions to assist in the maintenance of hosts files.
Package hostpital provides functions to assist in the maintenance of hosts files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL