Giter VIP home page Giter VIP logo

libhosty's Introduction

libhosty

made-with-Go Go Report Card Build and Test gopherbadger-tag-do-not-edit GoDoc

Description

libhosty is a pure golang library to manipulate the hosts file. It is inspired by txeh, with some enrichments.

Table of Contents

Main Features

  • Comment/Uncomment a line without removing it from the file
  • Restore the default hosts file for your system
  • Add/Remove Address lines
  • Add/Remove Comment lines
  • Add/Remove Empty lines
  • Query by hostname
  • Automatically handles duplicate entries

Installation

Ensure you have go on your system

> go version
go version go1.15.6 linux/amd64

and pull the library

> go get github.com/zerogate-it/libhosty

Usage

To use the library, just import it and call the Init() method.

Note: This code doesn't handle errors for readability purposes, but you SHOULD!

package main

import "github.com/zerogate-it/libhosty"

func main() {
    //you can define a custom config object
    // and use it to initialize libhosty with a custom hosts file
    //
    //cnf, _ := libhosty.NewHostsFileConfig("/home/sonica/hosts-export.txt")
    //hfl, _ := libhosty.InitWithConfig(cnf)
    
    //or initialize libhosty that will automatically try to loads
    // then default hosts file for your OS
    hfl, _ := libhosty.Init()
    
    //add an empty line
    hfl.AddEmptyFileLine()
    
    //add a host with a comment
    hfl.AddHostFileLine("12.12.12.12", "my.host.name", "comment on my hostname!")
    
    //add a comment
    hfl.AddCommentFileLine("just a comment")
    
    //add an empty line
    hfl.AddEmptyFileLine()
    
    //add another host without comment
    hfl.AddHostsFileLine("13.13.13.13", "another.host.name", "")
    
    //add another fqdn to the previous ip
    hfl.AddHostsFileLine("12.12.12.12", "second.host.name", "")
    
    // comment for host lines can be done by hostname, row line
    // or IP (as net.IP or string)
    //
    // Comment the line with address 12.12.12.12
    //
    // By-Row-Number
    idx, _ := hfl.GetHostsFileLineByHostname("second.host.name")
    hfl.CommentHostsFileLineByRow(idx)
    //
    // By-Hostname
    hfl.CommentHostsFileLineByHostname("second.host.name")
    //
    // By-Address-As-IP
    ip := net.ParseIP("12.12.12.12")
    hfl.CommentHostsFileLineByIP(ip)
    //
    // By-Address-As-String
    hfl.CommentHostsFileLineByAddress("12.12.12.12")
    
    // render the hosts file
    fmt.Println(hfl.RenderHostsFile())
    
    // write file to disk
    hfl.SaveHostsFile()
    
    // or to a custom location
    hfl.SaveHostsFileAs("/home/sonica/hosts-export.txt")
    
    // restore the original hosts file for linux
    hfl.RestoreDefaultLinuxHostsFile()
    
    // render the hosts file
    fmt.Println(hfl.RenderHostsFile())
    
    // write to disk
    hfl.SaveHostsFile()
}

The 1st fmt.Println() should output something like this (in a linux host)

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
::1                     localhost6.localdomain6 localhost6

# 12.12.12.12           my.host.name second.host.name   #comment on my hostname!
# just a comment line

13.13.13.13             another.host.name

While the 2nd fmt.Println() should output the default template for linux systems

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
::1                     localhost6.localdomain6 localhost6

If you handle errors properly, you'll notice that this example program will fail on the SaveHostsFile() call if started as a normal user, as editing the hosts file requires root privileges. This does not prevent libhosty from loading, managing, rendering and exporting the hosts file

Contributing

Issues and PRs are more than welcome!

Templates

If you find a hosts template (like the Docker one) that you think can be useful to have in this library feel free to open a Pull Request

Credits

Project Contributors will be listed here

License

Licenses under Apache License 2.0

libhosty's People

Contributors

areyoulazy avatar vinothsparrow avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.