Skip to content

Commit

Permalink
Merge pull request #1 from guillaumerose/master
Browse files Browse the repository at this point in the history
Prepare for diverging from goodhost-cli
  • Loading branch information
guillaumerose authored Nov 25, 2020
2 parents 2fc9120 + 27d71af commit dcf148c
Show file tree
Hide file tree
Showing 313 changed files with 28,434 additions and 17,164 deletions.
23 changes: 0 additions & 23 deletions .github/workflows/release.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.DS_Store
goodhosts
goodhosts.exe
admin-helper
admin-helper.exe
.idea
dist
out
Expand Down
13 changes: 13 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
run:
timeout: 10m

linters:
enable:
- errcheck
- goimports
- gofmt
- gosec
- gocritic
- deadcode
- misspell
- golint
25 changes: 0 additions & 25 deletions .goreleaser.yml

This file was deleted.

102 changes: 0 additions & 102 deletions API.md

This file was deleted.

22 changes: 13 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# Go and compilation related variables
VERSION ?= $(shell git describe --tags --dirty)
BUILD_DIR ?= out

BINARY_NAME := goodhosts
BINARY_NAME := admin-helper
RELEASE_DIR ?= release

LDFLAGS := -X main.Version=$(VERSION) -extldflags='-static' -s -w

# Add default target
.PHONY: all
all: build

.PHONY: vendor
vendor:
go mod tidy
go mod vendor

$(BUILD_DIR):
Expand All @@ -17,31 +22,30 @@ $(BUILD_DIR):
.PHONY: clean
clean:
rm -rf $(BUILD_DIR)
rm -rf vendor
rm -fr release

$(BUILD_DIR)/macos-amd64/$(BINARY_NAME):
GOARCH=amd64 GOOS=darwin go build -o $(BUILD_DIR)/macos-amd64/$(BINARY_NAME) ./main.go
CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/macos-amd64/$(BINARY_NAME) ./main.go

$(BUILD_DIR)/linux-amd64/$(BINARY_NAME):
GOOS=linux GOARCH=amd64 go build -o $(BUILD_DIR)/linux-amd64/$(BINARY_NAME) ./main.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/linux-amd64/$(BINARY_NAME) ./main.go

$(BUILD_DIR)/windows-amd64/$(BINARY_NAME).exe:
GOARCH=amd64 GOOS=windows go build -o $(BUILD_DIR)/windows-amd64/$(BINARY_NAME).exe ./main.go
CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/windows-amd64/$(BINARY_NAME).exe ./main.go

.PHONY: cross ## Cross compiles all binaries
cross: $(BUILD_DIR)/macos-amd64/$(BINARY_NAME) $(BUILD_DIR)/linux-amd64/$(BINARY_NAME) $(BUILD_DIR)/windows-amd64/$(BINARY_NAME).exe

.PHONY: release
release: clean cross
mkdir $(RELEASE_DIR)
tar cJSf $(RELEASE_DIR)/goodhosts-cli-macos-amd64.tar.xz -C $(BUILD_DIR)/macos-amd64 $(BINARY_NAME)
tar cJSf $(RELEASE_DIR)/goodhosts-cli-linux-amd64.tar.xz -C $(BUILD_DIR)/linux-amd64 $(BINARY_NAME)
tar cJSf $(RELEASE_DIR)/goodhosts-cli-windows-amd64.tar.xz -C $(BUILD_DIR)/windows-amd64 $(BINARY_NAME).exe
tar cJSf $(RELEASE_DIR)/admin-helper-macos-amd64.tar.xz -C $(BUILD_DIR)/macos-amd64 $(BINARY_NAME)
tar cJSf $(RELEASE_DIR)/admin-helper-linux-amd64.tar.xz -C $(BUILD_DIR)/linux-amd64 $(BINARY_NAME)
tar cJSf $(RELEASE_DIR)/admin-helper-windows-amd64.tar.xz -C $(BUILD_DIR)/windows-amd64 $(BINARY_NAME).exe

pushd $(RELEASE_DIR) && sha256sum * > sha256sum.txt && popd

.PHONY: build
build:
go build -o $(BINARY_NAME) ./main.go
CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o $(BINARY_NAME) ./main.go

69 changes: 15 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,26 @@
# goodhosts cli, fork of Lex Toumbourou's project [goodhosts](https://github.com/lextoumbourou/goodhosts)
# admin-helper, all admin commands required by [CodeReady Containers](https://github.com/lextoumbourou/goodhosts)

Simple [hosts file](http://en.wikipedia.org/wiki/Hosts_%28file%29) (```/etc/hosts```) management in a Go cli. One simple
interface for any OS or architecture, script and automate hosts file updates using one simple tool.

## Features

- List, add, remove and check hosts file entries from code or the command-line
- Remove by IP, Host, or IP/Host
- `check` returns proper exit codes for scripting e.g. `goodhosts check 10.0.5.12 || echo "Missing hosts entry for 10.0.5.12"`
- linux/darwin/windows support
- Custom hosts file support
- Backup/Restore
- Quick inline editor (vim/nano)

## Installation

Visit the [releases](https://github.com/goodhosts/cli/releases/) page and download the proper binary for your
architecture. Unzip and run in place, put in your system path (linux: `/usr/local/bin` win: `~/bin`) for easier access.
This project gathers all the interactions with the hosts that need to perform CodeReady Containers as administrator.

## Usage

For full usage directions simply call `goodhosts -h`
For full usage directions simply call `admin-helper -h`

```shell
$ ./goodhosts -h
NAME:
goodhosts - manage your hosts file goodly

USAGE:
goodhosts [global options] command [command options] [arguments...]

COMMANDS:
check, c Check if ip or host exists
list, ls List all entries in the hostsfile
add, a Add an entry to the hostsfile
remove, rm, r Remove ip or host(s) if exists
debug, d Show debug table for hosts file
backup Backup hosts file
restore Restore hosts file from backup
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--custom value override the default hosts file
--debug, -d Turn on verbose debug logging (default: false)
--quiet, -q Turn on off all logging (default: false)
--help, -h show help (default: false)
```
Usage:
admin-helper [command]

Each sub-command can be called with a `-h` option to see detailed help information.
```shell
Available Commands:
add Add an entry to the hostsfile
clean Clean all entries added with a particular suffix
help Help about any command
remove Remove host(s) if exists

Flags:
-h, --help help for admin-helper
-v, --version version for admin-helper

$ ./goodhosts list -h
NAME:
goodhosts list - List all entries in the hostsfile

USAGE:
goodhosts list [command options] [arguments...]

OPTIONS:
--all Show all entries in the hosts file including commented lines. (default: false)
--help, -h show help (default: false)
Use "admin-helper [command] --help" for more information about a command.
```
## License
Expand Down
54 changes: 20 additions & 34 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,44 @@
package cmd

import (
"strings"
"fmt"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/spf13/cobra"
)

func Add() *cli.Command {
return &cli.Command{
Name: "add",
Aliases: []string{"a"},
Usage: "Add an entry to the hostsfile",
Action: add,
ArgsUsage: "[IP] [HOST] ([HOST]...)",
}
var Add = &cobra.Command{
Use: "add",
Aliases: []string{"a"},
Short: "Add an entry to the hostsfile",
RunE: func(cmd *cobra.Command, args []string) error {
return add(args)
},
}
func add(c *cli.Context) error {

args := c.Args()

if args.Len() < 2 {
logrus.Infof("adding a hostsfile entry requires an ip and a hostname.")
return nil
func add(args []string) error {
if len(args) < 2 {
return fmt.Errorf("adding to hosts file requires an ip and a hostname")
}

hostsfile, err := loadHostsfile(c)
hostsFile, err := loadHostsFile()
if err != nil {
return err
}

ip := args.Slice()[0]
ip := args[0]
uniqueHosts := map[string]bool{}
var hostEntries []string

for i := 1; i < args.Len(); i++ {
uniqueHosts[args.Slice()[i]] = true
for i := 1; i < len(args); i++ {
uniqueHosts[args[i]] = true
}

for key, _ := range uniqueHosts {
for key := range uniqueHosts {
hostEntries = append(hostEntries, key)
}

err = hostsfile.Add(ip, hostEntries...)
if err != nil {
return cli.NewExitError(err.Error(), 2)
}

logrus.Debugln("flushing hosts file to disk")
err = hostsfile.Flush()
if err != nil {
return cli.NewExitError(err.Error(), 2)
if err := hostsFile.Add(ip, hostEntries...); err != nil {
return err
}

logrus.Infof("hosts entry added: %s %s\n", ip, strings.Join(hostEntries, " "))
return debugFooter(c)
return hostsFile.Flush()
}
Loading

0 comments on commit dcf148c

Please sign in to comment.