ghosts

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2014 License: MIT Imports: 14 Imported by: 0

README

GHosts

Build Status

Efficient hosts switcher

Features

  • Switch hosts profiles
  • Load remote hosts file
  • Easy to config

Supported systems

  • Unix(link)
  • Windows

Tested on Ubuntu linux.

Install

go get github.com/ddliu/ghosts/ghosts

Usage

ghosts prod
ghosts base remote

You need to run this command as root(sudo) or administrator.

Options
  • --config=/custom/path/to/ghosts.yml
  • --list Show groups
  • --raw Keep comments, spaces and empty lines
  • --target=/custom/path/to/hosts
  • --print Do not write hosts file, just show result

ghosts.yml

The ghosts.yml file contains groups of hosts definitions.

By default it located at:

  • Unix(Mac OS X, Linux): ~/.ghosts/ghosts.yml
  • Windows: HOME\ghosts\ghosts.yml

Config example:

# GHost Sample Config File

# Line by line
group1:
 - 192.168.0.100 www.example.com
 - 192.168.0.101 static.example.com

# Line by line
group2:
 - 192.168.0.102 db.example.com
 - 192.168.0.103 admin.example.com

# Multi-line scope
group3: |
 192.168.0.104 help.example.com
 192.168.0.105 mail.example.com

# Combination
base:
 - 127.0.0.1 localhost
 - group1
 - group2

# Load a remote host file
remote: https://raw.githubusercontent.com/ddliu/hosts/master/ghosts.example.hosts

# Load a local host file
local: ./hosts

# Include groups
test:
 - base
 - local

prod:
 - base
 - remote

Todo

  • Flush DNS cache
  • Prebuilt packages/binaries for different systems

Changelog

v0.1.0 (2014-03-30)

First release

Documentation

Index

Constants

View Source
const CONFIG_DIRECTORY = ".ghosts"
View Source
const EOL = "\n"
View Source
const HostLineRegex = `^([a-zA-Z0-9\._-]+)\s+([a-zA-Z0-9\._-]+)$`
View Source
const ValidHostnameRegex = `^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9_\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9_\-]*[A-Za-z0-9])$`
View Source
const ValidIpAddressRegex = `^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`

http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address

Variables

View Source
var Environment = struct {
	ConfigPath string
	TargetPath string
}{}

Global environment

View Source
var HostLineRegexCompile *regexp.Regexp
View Source
var ValidHostnameRegexCompile *regexp.Regexp
View Source
var ValidIpAddressRegexCompile *regexp.Regexp

Functions

func GetConfigPath

func GetConfigPath() string

func GetSysConfigPath

func GetSysConfigPath() (string, error)

func GetSysHomePath

func GetSysHomePath() (string, error)

Get home dir of current user (or sudo_user)

func GetSysTargetPath

func GetSysTargetPath() string

func GetTargetPath

func GetTargetPath() string

func MergeHosts

func MergeHosts(hosts ...HostStruct) string

Types

type App

type App struct {
	// contains filtered or unexported fields
}

func (*App) Generate

func (this *App) Generate(names ...string) (string, error)

func (*App) GenerateRaw

func (this *App) GenerateRaw(names ...string) (string, error)

func (*App) GetNames

func (this *App) GetNames() []string

func (*App) ParseConfig

func (this *App) ParseConfig() error

func (*App) Switch

func (this *App) Switch(names ...string) error

func (*App) SwitchRaw

func (this *App) SwitchRaw(names ...string) error

type Config

type Config struct {
	Path string
	// contains filtered or unexported fields
}

func (*Config) Parse

func (this *Config) Parse() (map[string]HostNode, error)

Load the config file and parse it

func (*Config) ParseName

func (this *Config) ParseName(name string) (HostNode, error)

Parse a group entry

type HostEntry

type HostEntry struct {
	Host string
	IP   string
}

Simple entry node

func (*HostEntry) GetList

func (this *HostEntry) GetList() []HostStruct

func (*HostEntry) GetRaw

func (this *HostEntry) GetRaw() string

func (*HostEntry) Validate

func (this *HostEntry) Validate() error

type HostFile

type HostFile struct {
	FilePath string
	// contains filtered or unexported fields
}

The file node

func (*HostFile) GetList

func (this *HostFile) GetList() []HostStruct

func (*HostFile) GetRaw

func (this *HostFile) GetRaw() string

func (*HostFile) Validate

func (this *HostFile) Validate() error

type HostFileRemote

type HostFileRemote struct {
	FileUrl string
	// contains filtered or unexported fields
}

The remote file node

func (*HostFileRemote) GetList

func (this *HostFileRemote) GetList() []HostStruct

func (*HostFileRemote) GetRaw

func (this *HostFileRemote) GetRaw() string

func (*HostFileRemote) Validate

func (this *HostFileRemote) Validate() error

type HostGroup

type HostGroup struct {
	Children []HostNode
}

func (*HostGroup) Add

func (this *HostGroup) Add(nodes ...HostNode)

func (*HostGroup) GetList

func (this *HostGroup) GetList() []HostStruct

func (*HostGroup) GetRaw

func (this *HostGroup) GetRaw() string

func (*HostGroup) Validate

func (this *HostGroup) Validate() error

type HostNode

type HostNode interface {
	Validate() error
	GetList() []HostStruct
	GetRaw() string
}

type HostScope

type HostScope struct {
	Scope string
	// contains filtered or unexported fields
}

The scope node, could be content of a host file

func (*HostScope) GetList

func (this *HostScope) GetList() []HostStruct

func (*HostScope) GetRaw

func (this *HostScope) GetRaw() string

func (*HostScope) Validate

func (this *HostScope) Validate() (err error)

type HostStruct

type HostStruct struct {
	Host string
	IP   string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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