Giter Site home page Giter Site logo

hoster's Introduction

hoster

Hoster is a cross-platform operating system host file management library written in Go.

Features | Installation | Usage | CLI

Features

  • Cross platform: support Linux / Windows / macOS(darwin)
  • Lookup by address(IPv4, IPv6 etc.) and host(domains).
  • Host file backup and duplication.
  • CLI cmd/hoster for manipulating host file in terminal.

Installation

Requirements:

  • Golang version v1.13.0 (minimum)

Installation:

go get -u -v github.com/monitor1379/hoster

Usage

Create

Create a *HostManager and print the content of host file:

package main


import (
	"fmt"
	"time"

	"github.com/monitor1379/hoster"
)

func main() {
	// create a *HostManager
	hm, err := hoster.Default()
	if err != nil {
		panic(err)
	}

	// print:
	// "/etc/hosts" in non-Windows OS
	// or
	// "C:\Windows\System32\drivers\etc\hosts" in Windows
	fmt.Println(hm.HostFilePath())

	// print your host file content
	fmt.Println(hm.String())
}

Lookup

Assuming your host file content is:

127.0.0.1 localhost

192.168.10.10 hoster-1.k8s.svc.cluster.local  hoster-2.k8s.svc.cluster.local    # my kubernetes services
192.168.10.11 other-app.example.com


# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback

Lookup by address(ip) or host(domain):

package main

import (
	"fmt"

	"github.com/monitor1379/hoster"
)

func main() {
	hm, err := hoster.New("./hosts.txt")
	if err != nil {
		panic(err)
	}

	mapping, ok := hm.LookupByAddress("192.168.10.10")
	fmt.Println(ok)              // true
	fmt.Println(mapping.Address) // 192.168.10.10
	fmt.Println(mapping.Hosts)   // []string{"hoster-1.k8s.svc.cluster.local", "hoster-2.k8s.svc.cluster.local"}
	fmt.Println(mapping.Comment) // # my kubernetes services

	mapping, ok = hm.LookupByHost("hoster-2.k8s.svc.cluster.local")
	fmt.Println(ok)              // true
	fmt.Println(mapping.Address) // 192.168.10.10
	fmt.Println(mapping.Hosts)   // []string{"hoster-1.k8s.svc.cluster.local", "hoster-2.k8s.svc.cluster.local"}
	fmt.Println(mapping.Comment) // # my kubernetes services
}

Set and Delete

package main

import (
	"fmt"

	"github.com/monitor1379/hoster"
)

func main() {
	// hm is managing "./hosts.txt"
	hm, err := hoster.New("./hosts.txt")
	if err != nil {
		panic(err)
	}

	fmt.Println(hm.String())
	// print:
	// 127.0.0.1       localhost
	//
	// 192.168.10.10   hoster-1.k8s.svc.cluster.local  hoster-2.k8s.svc.cluster.local  # my kubernetes services
	// 192.168.10.11   other-app.example.com
	//
	// # The following lines are desirable for IPv6 capable hosts
	// ::1     ip6-localhost   ip6-loopback

	// hm2 is managing "./hosts2.txt" which content is copied from "./hosts.txt"
	hm2, err := hm.Duplicate("./host2.txt")
	if err != nil {
		panic(err)
	}

	err = hm2.Set("hoster-2.k8s.svc.cluster.local", "192.168.10.12", "# added by monitor1379")
	if err != nil {
		panic(err)
	}
	fmt.Println(hm2.String())
	// print:
	// 127.0.0.1       localhost
	//
	// 192.168.10.10   hoster-1.k8s.svc.cluster.local  # my kubernetes services
	// 192.168.10.11   other-app.example.com
	//
	// # The following lines are desirable for IPv6 capable hosts
	// ::1     ip6-localhost   ip6-loopback
	// 192.168.10.12   hoster-2.k8s.svc.cluster.local  # added by monitor1379

	err = hm2.DeleteHost("hoster-2.k8s.svc.cluster.local")
	if err != nil {
		panic(err)
	}
	fmt.Println(hm2.String())
	// print:
	// 127.0.0.1       localhost
	//
	// 192.168.10.10   hoster-1.k8s.svc.cluster.local  # my kubernetes services
	// 192.168.10.11   other-app.example.com
	//
	// # The following lines are desirable for IPv6 capable hosts
	// ::1     ip6-localhost   ip6-loopback

}

Backup

backup host file to a new path:

package main

import (
	"github.com/monitor1379/hoster"
)

func main() {
	hm, err := hoster.New("./hosts.txt")
	if err != nil {
		panic(err)
	}

	// note that after backup, hm is still managing "./hosts.txt"
	err = hm.Backup("./hosts-backup.txt")
	if err != nil {
		panic(err)
	}
}

CLI

cmd/hoster is a command-line program for manipulating host file in terminal.

common methods:

  • hoster version
  • hoster list [--file <host-file-path>]
  • hoster lookup [--file <host-file-path>] [--address <address>] [--host <host>]
  • hoster set [--file <host-file-path>] [--address <address>] [--host <host>] [--comment <comment>
> hoster

Hoster is a cross-platform operating system host file management library written in Go.

Usage:
  hoster [command]

Available Commands:
  help        Help about any command
  list        List address-host mappings
  lookup      lookup by address or host
  set         Add a address-host mapping
  version     Print the version of hoster

Flags:
  -f, --file string   specify a host file path (default "/etc/hosts")
  -h, --help          help for hoster

Use "hoster [command] --help" for more information about a command.

List command:

> hoster list

+-----------+-----------------+
|  ADDRESS  |      HOST       |
+-----------+-----------------+
| 127.0.0.1 | localhost       |
| 127.0.1.1 | red-coast-base  |
| ::1       | ip6-localhost   |
| ::1       | ip6-loopback    |
| fe00::0   | ip6-localnet    |
| ff00::0   | ip6-mcastprefix |
| ff02::1   | ip6-allnodes    |
| ff02::2   | ip6-allrouters  |
+-----------+-----------------+

hoster's People

Contributors

monitor1379 avatar

Stargazers

 avatar

Watchers

 avatar  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.