Skip to content

Commit

Permalink
Merge pull request #13 from luthermonson/clean
Browse files Browse the repository at this point in the history
Adding Clean Command
  • Loading branch information
luthermonson authored Sep 7, 2020
2 parents 4e60d02 + 78030fa commit 40ec433
Show file tree
Hide file tree
Showing 216 changed files with 3,874 additions and 23,201 deletions.
36 changes: 36 additions & 0 deletions cmd/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"github.com/urfave/cli/v2"
)

func Clean() *cli.Command {
return &cli.Command{
Name: "clean",
Aliases: []string{"c"},
Usage: "Clean the hostsfile by combining duplicate ips, sorting and removing duplicate hostnames",
Action: clean,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dry-run",
Usage: "Dry run only, will output contents of new hostsfile without writing the changes.",
},
},
}
}

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

hf.Clean()
if c.Bool("dry-run") {
outputHostsfile(hf, c.Bool("all"))
} else {
hf.Flush()
}

return debugFooter(c)
}
19 changes: 1 addition & 18 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cmd

import (
"fmt"

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

Expand All @@ -29,20 +26,6 @@ func list(c *cli.Context) error {
return err
}

for _, line := range hostsfile.Lines {
if !c.Bool("all") {
if line.IsComment() || line.Raw == "" {
continue
}
}

lineOutput := fmt.Sprintf("%s\n", line.Raw)
if line.IsMalformed() {
lineOutput = fmt.Sprintf("%s # <<< Malformed!\n", lineOutput)
}

logrus.Infof(lineOutput)
}

outputHostsfile(hostsfile, c.Bool("all"))
return debugFooter(c)
}
18 changes: 18 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Commands() []*cli.Command {
Backup(),
Restore(),
Edit(),
Clean(),
}
}

Expand Down Expand Up @@ -56,6 +57,23 @@ func loadHostsfile(c *cli.Context) (hostsfile.Hosts, error) {
return hfile, nil
}

func outputHostsfile(hf hostsfile.Hosts, all bool) {
for _, line := range hf.Lines {
if !all {
if line.IsComment() || line.Raw == "" {
continue
}
}

lineOutput := fmt.Sprintf("%s\n", line.Raw)
if line.IsMalformed() {
lineOutput = fmt.Sprintf("%s # <<< Malformed!\n", lineOutput)
}

logrus.Infof(lineOutput)
}
}

func debugFooter(c *cli.Context) error {
if c.Command.Name != "debug" && !c.Bool("debug") {
return nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/goodhosts/cli
go 1.13

require (
github.com/goodhosts/hostsfile v0.0.4
github.com/goodhosts/hostsfile v0.0.5
github.com/olekukonko/tablewriter v0.0.4
github.com/sirupsen/logrus v1.4.2
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
Expand Down
296 changes: 2 additions & 294 deletions go.sum

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions vendor/github.com/goodhosts/hostsfile/const.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions vendor/github.com/goodhosts/hostsfile/const_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions vendor/github.com/goodhosts/hostsfile/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

379 changes: 0 additions & 379 deletions vendor/github.com/goodhosts/hostsfile/go.sum

Large diffs are not rendered by default.

49 changes: 38 additions & 11 deletions vendor/github.com/goodhosts/hostsfile/hosts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/goodhosts/hostsfile/hostsline.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 40 additions & 2 deletions vendor/golang.org/x/sys/unix/affinity_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 0 additions & 54 deletions vendor/golang.org/x/sys/unix/asm_linux_riscv64.s

This file was deleted.

Loading

0 comments on commit 40ec433

Please sign in to comment.