Skip to content

Commit 40ec433

Browse files
authored
Merge pull request #13 from luthermonson/clean
Adding Clean Command
2 parents 4e60d02 + 78030fa commit 40ec433

216 files changed

Lines changed: 3874 additions & 23201 deletions

File tree

Some content is hidden

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

cmd/clean.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cmd
2+
3+
import (
4+
"github.com/urfave/cli/v2"
5+
)
6+
7+
func Clean() *cli.Command {
8+
return &cli.Command{
9+
Name: "clean",
10+
Aliases: []string{"c"},
11+
Usage: "Clean the hostsfile by combining duplicate ips, sorting and removing duplicate hostnames",
12+
Action: clean,
13+
Flags: []cli.Flag{
14+
&cli.BoolFlag{
15+
Name: "dry-run",
16+
Usage: "Dry run only, will output contents of new hostsfile without writing the changes.",
17+
},
18+
},
19+
}
20+
}
21+
22+
func clean(c *cli.Context) error {
23+
hf, err := loadHostsfile(c)
24+
if err != nil {
25+
return err
26+
}
27+
28+
hf.Clean()
29+
if c.Bool("dry-run") {
30+
outputHostsfile(hf, c.Bool("all"))
31+
} else {
32+
hf.Flush()
33+
}
34+
35+
return debugFooter(c)
36+
}

cmd/list.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
6-
"github.com/sirupsen/logrus"
74
"github.com/urfave/cli/v2"
85
)
96

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

32-
for _, line := range hostsfile.Lines {
33-
if !c.Bool("all") {
34-
if line.IsComment() || line.Raw == "" {
35-
continue
36-
}
37-
}
38-
39-
lineOutput := fmt.Sprintf("%s\n", line.Raw)
40-
if line.IsMalformed() {
41-
lineOutput = fmt.Sprintf("%s # <<< Malformed!\n", lineOutput)
42-
}
43-
44-
logrus.Infof(lineOutput)
45-
}
46-
29+
outputHostsfile(hostsfile, c.Bool("all"))
4730
return debugFooter(c)
4831
}

cmd/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func Commands() []*cli.Command {
2525
Backup(),
2626
Restore(),
2727
Edit(),
28+
Clean(),
2829
}
2930
}
3031

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

60+
func outputHostsfile(hf hostsfile.Hosts, all bool) {
61+
for _, line := range hf.Lines {
62+
if !all {
63+
if line.IsComment() || line.Raw == "" {
64+
continue
65+
}
66+
}
67+
68+
lineOutput := fmt.Sprintf("%s\n", line.Raw)
69+
if line.IsMalformed() {
70+
lineOutput = fmt.Sprintf("%s # <<< Malformed!\n", lineOutput)
71+
}
72+
73+
logrus.Infof(lineOutput)
74+
}
75+
}
76+
5977
func debugFooter(c *cli.Context) error {
6078
if c.Command.Name != "debug" && !c.Bool("debug") {
6179
return nil

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/goodhosts/cli
33
go 1.13
44

55
require (
6-
github.com/goodhosts/hostsfile v0.0.4
6+
github.com/goodhosts/hostsfile v0.0.5
77
github.com/olekukonko/tablewriter v0.0.4
88
github.com/sirupsen/logrus v1.4.2
99
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816

go.sum

Lines changed: 2 additions & 294 deletions
Large diffs are not rendered by default.

vendor/github.com/goodhosts/hostsfile/const.go

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/goodhosts/hostsfile/const_windows.go

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/goodhosts/hostsfile/go.mod

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/goodhosts/hostsfile/go.sum

Lines changed: 0 additions & 379 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/goodhosts/hostsfile/hosts.go

Lines changed: 38 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)