Skip to content

Commit dcf148c

Browse files
Merge pull request #1 from guillaumerose/master
Prepare for diverging from goodhost-cli
2 parents 2fc9120 + 27d71af commit dcf148c

File tree

313 files changed

+28434
-17164
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

313 files changed

+28434
-17164
lines changed

.github/workflows/release.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.DS_Store
2-
goodhosts
3-
goodhosts.exe
2+
admin-helper
3+
admin-helper.exe
44
.idea
55
dist
66
out

.golangci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
run:
2+
timeout: 10m
3+
4+
linters:
5+
enable:
6+
- errcheck
7+
- goimports
8+
- gofmt
9+
- gosec
10+
- gocritic
11+
- deadcode
12+
- misspell
13+
- golint

.goreleaser.yml

Lines changed: 0 additions & 25 deletions
This file was deleted.

API.md

Lines changed: 0 additions & 102 deletions
This file was deleted.

Makefile

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
# Go and compilation related variables
2+
VERSION ?= $(shell git describe --tags --dirty)
23
BUILD_DIR ?= out
34

4-
BINARY_NAME := goodhosts
5+
BINARY_NAME := admin-helper
56
RELEASE_DIR ?= release
67

8+
LDFLAGS := -X main.Version=$(VERSION) -extldflags='-static' -s -w
9+
710
# Add default target
811
.PHONY: all
912
all: build
1013

14+
.PHONY: vendor
1115
vendor:
16+
go mod tidy
1217
go mod vendor
1318

1419
$(BUILD_DIR):
@@ -17,31 +22,30 @@ $(BUILD_DIR):
1722
.PHONY: clean
1823
clean:
1924
rm -rf $(BUILD_DIR)
20-
rm -rf vendor
2125
rm -fr release
2226

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

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

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

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

3539
.PHONY: release
3640
release: clean cross
3741
mkdir $(RELEASE_DIR)
38-
tar cJSf $(RELEASE_DIR)/goodhosts-cli-macos-amd64.tar.xz -C $(BUILD_DIR)/macos-amd64 $(BINARY_NAME)
39-
tar cJSf $(RELEASE_DIR)/goodhosts-cli-linux-amd64.tar.xz -C $(BUILD_DIR)/linux-amd64 $(BINARY_NAME)
40-
tar cJSf $(RELEASE_DIR)/goodhosts-cli-windows-amd64.tar.xz -C $(BUILD_DIR)/windows-amd64 $(BINARY_NAME).exe
42+
tar cJSf $(RELEASE_DIR)/admin-helper-macos-amd64.tar.xz -C $(BUILD_DIR)/macos-amd64 $(BINARY_NAME)
43+
tar cJSf $(RELEASE_DIR)/admin-helper-linux-amd64.tar.xz -C $(BUILD_DIR)/linux-amd64 $(BINARY_NAME)
44+
tar cJSf $(RELEASE_DIR)/admin-helper-windows-amd64.tar.xz -C $(BUILD_DIR)/windows-amd64 $(BINARY_NAME).exe
4145

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

4448
.PHONY: build
4549
build:
46-
go build -o $(BINARY_NAME) ./main.go
50+
CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o $(BINARY_NAME) ./main.go
4751

README.md

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

3-
Simple [hosts file](http://en.wikipedia.org/wiki/Hosts_%28file%29) (```/etc/hosts```) management in a Go cli. One simple
4-
interface for any OS or architecture, script and automate hosts file updates using one simple tool.
5-
6-
## Features
7-
8-
- List, add, remove and check hosts file entries from code or the command-line
9-
- Remove by IP, Host, or IP/Host
10-
- `check` returns proper exit codes for scripting e.g. `goodhosts check 10.0.5.12 || echo "Missing hosts entry for 10.0.5.12"`
11-
- linux/darwin/windows support
12-
- Custom hosts file support
13-
- Backup/Restore
14-
- Quick inline editor (vim/nano)
15-
16-
## Installation
17-
18-
Visit the [releases](https://github.com/goodhosts/cli/releases/) page and download the proper binary for your
19-
architecture. Unzip and run in place, put in your system path (linux: `/usr/local/bin` win: `~/bin`) for easier access.
3+
This project gathers all the interactions with the hosts that need to perform CodeReady Containers as administrator.
204

215
## Usage
226

23-
For full usage directions simply call `goodhosts -h`
7+
For full usage directions simply call `admin-helper -h`
248

259
```shell
26-
$ ./goodhosts -h
27-
NAME:
28-
goodhosts - manage your hosts file goodly
29-
30-
USAGE:
31-
goodhosts [global options] command [command options] [arguments...]
32-
33-
COMMANDS:
34-
check, c Check if ip or host exists
35-
list, ls List all entries in the hostsfile
36-
add, a Add an entry to the hostsfile
37-
remove, rm, r Remove ip or host(s) if exists
38-
debug, d Show debug table for hosts file
39-
backup Backup hosts file
40-
restore Restore hosts file from backup
41-
help, h Shows a list of commands or help for one command
42-
43-
GLOBAL OPTIONS:
44-
--custom value override the default hosts file
45-
--debug, -d Turn on verbose debug logging (default: false)
46-
--quiet, -q Turn on off all logging (default: false)
47-
--help, -h show help (default: false)
48-
```
10+
Usage:
11+
admin-helper [command]
4912

50-
Each sub-command can be called with a `-h` option to see detailed help information.
51-
```shell
13+
Available Commands:
14+
add Add an entry to the hostsfile
15+
clean Clean all entries added with a particular suffix
16+
help Help about any command
17+
remove Remove host(s) if exists
18+
19+
Flags:
20+
-h, --help help for admin-helper
21+
-v, --version version for admin-helper
5222

53-
$ ./goodhosts list -h
54-
NAME:
55-
goodhosts list - List all entries in the hostsfile
56-
57-
USAGE:
58-
goodhosts list [command options] [arguments...]
59-
60-
OPTIONS:
61-
--all Show all entries in the hosts file including commented lines. (default: false)
62-
--help, -h show help (default: false)
23+
Use "admin-helper [command] --help" for more information about a command.
6324
```
6425
6526
## License

cmd/add.go

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,44 @@
11
package cmd
22

33
import (
4-
"strings"
4+
"fmt"
55

6-
"github.com/sirupsen/logrus"
7-
"github.com/urfave/cli/v2"
6+
"github.com/spf13/cobra"
87
)
98

10-
func Add() *cli.Command {
11-
return &cli.Command{
12-
Name: "add",
13-
Aliases: []string{"a"},
14-
Usage: "Add an entry to the hostsfile",
15-
Action: add,
16-
ArgsUsage: "[IP] [HOST] ([HOST]...)",
17-
}
9+
var Add = &cobra.Command{
10+
Use: "add",
11+
Aliases: []string{"a"},
12+
Short: "Add an entry to the hostsfile",
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
return add(args)
15+
},
1816
}
19-
func add(c *cli.Context) error {
20-
21-
args := c.Args()
2217

23-
if args.Len() < 2 {
24-
logrus.Infof("adding a hostsfile entry requires an ip and a hostname.")
25-
return nil
18+
func add(args []string) error {
19+
if len(args) < 2 {
20+
return fmt.Errorf("adding to hosts file requires an ip and a hostname")
2621
}
2722

28-
hostsfile, err := loadHostsfile(c)
23+
hostsFile, err := loadHostsFile()
2924
if err != nil {
3025
return err
3126
}
3227

33-
ip := args.Slice()[0]
28+
ip := args[0]
3429
uniqueHosts := map[string]bool{}
3530
var hostEntries []string
3631

37-
for i := 1; i < args.Len(); i++ {
38-
uniqueHosts[args.Slice()[i]] = true
32+
for i := 1; i < len(args); i++ {
33+
uniqueHosts[args[i]] = true
3934
}
4035

41-
for key, _ := range uniqueHosts {
36+
for key := range uniqueHosts {
4237
hostEntries = append(hostEntries, key)
4338
}
4439

45-
err = hostsfile.Add(ip, hostEntries...)
46-
if err != nil {
47-
return cli.NewExitError(err.Error(), 2)
48-
}
49-
50-
logrus.Debugln("flushing hosts file to disk")
51-
err = hostsfile.Flush()
52-
if err != nil {
53-
return cli.NewExitError(err.Error(), 2)
40+
if err := hostsFile.Add(ip, hostEntries...); err != nil {
41+
return err
5442
}
55-
56-
logrus.Infof("hosts entry added: %s %s\n", ip, strings.Join(hostEntries, " "))
57-
return debugFooter(c)
43+
return hostsFile.Flush()
5844
}

0 commit comments

Comments
 (0)