Skip to content

Commit

Permalink
Merge pull request #40 from goodhosts/renovations
Browse files Browse the repository at this point in the history
migrate to mage
  • Loading branch information
luthermonson authored Dec 24, 2023
2 parents 62843a9 + 601b65c commit 26733a7
Show file tree
Hide file tree
Showing 21 changed files with 352 additions and 185 deletions.
32 changes: 12 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,16 @@ jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.21
id: go
- name: Checkout
uses: actions/checkout@v2
- name: Run goimports
run: |
go install golang.org/x/tools/cmd/goimports@latest
output="$($(go env GOPATH)/bin/goimports -l $(find . -type f -name "*.go" -not -path "./vendor/*"))"
test -z $output && exit 0
$(go env GOPATH)/bin/goimports -d .
exit 1
- name: Test
env:
GOROOT: ""
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.54.2
./bin/golangci-lint run
go test -v ./...
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "1.21"
- name: Continuous Integration
uses: magefile/mage-action@v2
with:
version: latest
args: ci
- name: Upload to Codecov
uses: codecov/codecov-action@v3
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
coverage.txt
.DS_Store
/goodhosts
/goodhosts.exe
/.idea
/dist
/bin
/out
/release

Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ builds:
- amd64
- arm64
- arm
- 386
- "386"
- ppc64le
- s390x
- mips64
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 - Luther Monson
Copyright (c) 2024 - Luther Monson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
31 changes: 0 additions & 31 deletions Makefile

This file was deleted.

11 changes: 5 additions & 6 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ func Add() *cli.Command {
}
}
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
}

hostsfile, err := loadHostsfile(c, false)
hf, err := loadHostsfile(c, false)
if err != nil {
return err
}
Expand All @@ -53,23 +52,23 @@ func add(c *cli.Context) error {
hostEntries = append(hostEntries, key)
}

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

if c.Bool("clean") {
hostsfile.Clean()
hf.Clean()
}

if c.Bool("dry-run") {
logrus.Debugln("performing a dry run, writing output")
outputHostsfile(hostsfile, true)
outputHostsfile(hf, true)
return debugFooter(c)
}

logrus.Debugln("flushing hosts file to disk")
if err := hostsfile.Flush(); err != nil {
if err := hf.Flush(); err != nil {
return cli.Exit(err.Error(), 2)
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ func Backup() *cli.Command {
}

func backup(c *cli.Context) error {
hostsfile, err := loadHostsfile(c, false)
hf, err := loadHostsfile(c, false)
if err != nil {
return err
}

output := c.String("output")
if output == "" {
output = filepath.Join(
filepath.Dir(hostsfile.Path),
"."+filepath.Base(hostsfile.Path))
filepath.Dir(hf.Path),
"."+filepath.Base(hf.Path))
}

_, err = copyFile(hostsfile.Path, output)
if err != nil {
if err := copyFile(hf.Path, output); err != nil {
return err
}

logrus.Infof("backup complete")
return debugFooter(c)
}
6 changes: 3 additions & 3 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ func check(c *cli.Context) error {
return nil
}

hostsfile, err := loadHostsfile(c, true)
hf, err := loadHostsfile(c, true)
if err != nil {
return err
}
input := c.Args().First()

if net.ParseIP(input) != nil {
if hostsfile.HasIp(input) {
if hf.HasIP(input) {
logrus.Infof("%s exists in hosts file\n", input)
return nil
}
}

if hostsfile.HasHostname(input) {
if hf.HasHostname(input) {
logrus.Infof("%s exists in hosts file\n", input)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func clean(c *cli.Context) error {
h.Clean()
} else {
if c.Bool("remove-duplicate-ips") {
h.RemoveDuplicateIps()
h.CombineDuplicateIPs()
}
if c.Bool("remove-duplicate-hosts") {
h.RemoveDuplicateHosts()
Expand All @@ -75,7 +75,7 @@ func clean(c *cli.Context) error {
h.SortHosts()
}
if c.Bool("sort-ips") {
h.SortByIp()
h.SortIPs()
}
// needed for windows for 9/line, -1 default for linux will noop but if passed by cli we will run
h.HostsPerLine(hostsfile.HostsPerLine)
Expand Down
4 changes: 2 additions & 2 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func Edit() *cli.Command {
}

func edit(c *cli.Context) error {
hostsfile, err := loadHostsfile(c, false)
hf, err := loadHostsfile(c, false)
if err != nil {
return err
}

cmd := exec.Command(c.String("editor"), hostsfile.Path)
cmd := exec.Command(c.String("editor"), hf.Path)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
Expand Down
4 changes: 2 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func List() *cli.Command {
}

func list(c *cli.Context) error {
hostsfile, err := loadHostsfile(c, false)
hf, err := loadHostsfile(c, false)
if err != nil {
return err
}

outputHostsfile(hostsfile, c.Bool("all"))
outputHostsfile(hf, c.Bool("all"))
return debugFooter(c)
}
71 changes: 39 additions & 32 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"github.com/urfave/cli/v2"
)

func Run(c *cli.Context) error {
return list(c)
}

func Commands() []*cli.Command {
return []*cli.Command{
Add(),
Expand All @@ -35,26 +31,26 @@ func DefaultAction(c *cli.Context) error {

func loadHostsfile(c *cli.Context, readOnly bool) (*hostsfile.Hosts, error) {
customHostsfile := c.String("file")
var hfile *hostsfile.Hosts
var hf *hostsfile.Hosts
var err error

if customHostsfile != "" {
logrus.Debugf("loading custom hosts file: %s\n", customHostsfile)
hfile, err = hostsfile.NewCustomHosts(customHostsfile)
hf, err = hostsfile.NewCustomHosts(customHostsfile)
} else {
logrus.Debugf("loading default hosts file: %s\n", hostsfile.HostsFilePath)
hfile, err = hostsfile.NewHosts()
hf, err = hostsfile.NewHosts()
}

if err != nil {
return hfile, cli.Exit(err, 1)
return hf, cli.Exit(err, 1)
}

if !readOnly && !hfile.IsWritable() {
return hfile, cli.Exit("Host file not writable. Try running with elevated privileges.", 1)
if !readOnly && !hf.IsWritable() {
return hf, cli.Exit("Host file not writable. Try running with elevated privileges.", 1)
}

return hfile, nil
return hf, nil
}

func outputHostsfile(hf *hostsfile.Hosts, all bool) {
Expand All @@ -79,15 +75,15 @@ func debugFooter(c *cli.Context) error {
return nil
}

hostsfile, err := loadHostsfile(c, true)
hf, err := loadHostsfile(c, true)
if err != nil {
return err
}

logrus.Infof("hosts file path: %s\n", hostsfile.Path)
logrus.Infof("hosts file path: %s\n", hf.Path)

var comments, empty, entry, malformed int
for _, line := range hostsfile.Lines {
for _, line := range hf.Lines {

if line.IsComment() {
comments++
Expand All @@ -107,11 +103,11 @@ func debugFooter(c *cli.Context) error {
}

data := [][]string{
[]string{"lines", fmt.Sprintf("%d", len(hostsfile.Lines))},
[]string{"entries", fmt.Sprintf("%d", entry)},
[]string{"comments", fmt.Sprintf("%d", comments)},
[]string{"empty", fmt.Sprintf("%d", empty)},
[]string{"malformed", fmt.Sprintf("%d", malformed)},
{"lines", fmt.Sprintf("%d", len(hf.Lines))},
{"entries", fmt.Sprintf("%d", entry)},
{"comments", fmt.Sprintf("%d", comments)},
{"empty", fmt.Sprintf("%d", empty)},
{"malformed", fmt.Sprintf("%d", malformed)},
}

table := tablewriter.NewWriter(os.Stdout)
Expand All @@ -125,29 +121,40 @@ func debugFooter(c *cli.Context) error {
return nil
}

func copyFile(src, dst string) (int64, error) {
func copyFile(src, dst string) (err error) {
logrus.Debugf("copying file: src %s, dst %s",
src, dst)
sourceFileStat, err := os.Stat(src)

var fi os.FileInfo
fi, err = os.Stat(src)
if err != nil {
return 0, err
return err
}

if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
if !fi.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", src)
}

source, err := os.Open(src)
var source *os.File
source, err = os.Open(src)
if err != nil {
return 0, err
return err
}
defer source.Close()

destination, err := os.Create(dst)
defer func() {
err = source.Close()
}()

var destination *os.File
destination, err = os.Create(dst)
if err != nil {
return 0, err
return err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err

defer func() {
err = destination.Close()
}()

_, err = io.Copy(destination, source)
return
}
Loading

0 comments on commit 26733a7

Please sign in to comment.