Skip to content

Commit 62843a9

Browse files
authored
Merge pull request #39 from goodhosts/cleanup
cleanup in prep for testing
2 parents 65e3131 + fb5d5fb commit 62843a9

File tree

10 files changed

+59
-66
lines changed

10 files changed

+59
-66
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ $ goodhosts -h
3737
goodhosts - manage your hosts file goodly
3838

3939
USAGE:
40-
main.exe [global options] command [command options] [arguments...]
40+
goodosts [global options] command [command options] [arguments...]
4141

4242
COMMANDS:
4343
add, a Add an entry to the hostsfile

cmd/add.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func add(c *cli.Context) error {
5555

5656
err = hostsfile.Add(ip, hostEntries...)
5757
if err != nil {
58-
return cli.NewExitError(err.Error(), 2)
58+
return cli.Exit(err.Error(), 2)
5959
}
6060

6161
if c.Bool("clean") {
@@ -70,7 +70,7 @@ func add(c *cli.Context) error {
7070

7171
logrus.Debugln("flushing hosts file to disk")
7272
if err := hostsfile.Flush(); err != nil {
73-
return cli.NewExitError(err.Error(), 2)
73+
return cli.Exit(err.Error(), 2)
7474
}
7575

7676
logrus.Infof("hosts entry added: %s %s\n", ip, strings.Join(hostEntries, " "))

cmd/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ func check(c *cli.Context) error {
4141
return nil
4242
}
4343

44-
return cli.NewExitError(fmt.Sprintf("%s does not match anything in the hosts file", input), 1)
44+
return cli.Exit(fmt.Sprintf("%s does not match anything in the hosts file", input), 1)
4545
}

cmd/clean.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func clean(c *cli.Context) error {
8888
}
8989

9090
if err := h.Flush(); err != nil {
91-
return cli.NewExitError(err.Error(), 2)
91+
return cli.Exit(err.Error(), 2)
9292
}
9393
return debugFooter(c)
9494
}

cmd/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func Commands() []*cli.Command {
2626
List(),
2727
Remove(),
2828
Restore(),
29-
Version(),
3029
}
3130
}
3231

@@ -48,11 +47,11 @@ func loadHostsfile(c *cli.Context, readOnly bool) (*hostsfile.Hosts, error) {
4847
}
4948

5049
if err != nil {
51-
return hfile, cli.NewExitError(err, 1)
50+
return hfile, cli.Exit(err, 1)
5251
}
5352

5453
if !readOnly && !hfile.IsWritable() {
55-
return hfile, cli.NewExitError("Host file not writable. Try running with elevated privileges.", 1)
54+
return hfile, cli.Exit("Host file not writable. Try running with elevated privileges.", 1)
5655
}
5756

5857
return hfile, nil

cmd/remove.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func remove(c *cli.Context) error {
3838
}
3939

4040
if args.Len() == 0 {
41-
return cli.NewExitError("no input", 1)
41+
return cli.Exit("no input", 1)
4242
}
4343

4444
if args.Len() == 1 { //could be ip or hostname
@@ -60,7 +60,7 @@ func remove(c *cli.Context) error {
6060
if hostsfile.HasIp(args.Slice()[0]) {
6161
err = hostsfile.Remove(args.Slice()[0], hostEntries...)
6262
if err != nil {
63-
return cli.NewExitError(err.Error(), 2)
63+
return cli.Exit(err.Error(), 2)
6464
}
6565
}
6666
} else {
@@ -84,7 +84,7 @@ func remove(c *cli.Context) error {
8484

8585
logrus.Debugln("flushing hosts file to disk")
8686
if err := hostsfile.Flush(); err != nil {
87-
return cli.NewExitError(err.Error(), 2)
87+
return cli.Exit(err.Error(), 2)
8888
}
8989

9090
logrus.Infof("entry removed: %s\n", strings.Join(hostEntries, " "))

cmd/version.go

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

go.mod

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ module github.com/goodhosts/cli
33
go 1.21
44

55
require (
6-
github.com/goodhosts/hostsfile v0.1.2
7-
github.com/olekukonko/tablewriter v0.0.4
8-
github.com/sirupsen/logrus v1.4.2
6+
github.com/goodhosts/hostsfile v0.1.3
7+
github.com/olekukonko/tablewriter v0.0.5
8+
github.com/sirupsen/logrus v1.9.3
99
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
10-
github.com/urfave/cli/v2 v2.1.1
10+
github.com/urfave/cli/v2 v2.25.7
1111
)
1212

1313
require (
1414
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
15-
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
15+
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
1616
github.com/dimchansky/utfbom v1.1.1 // indirect
17-
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
18-
github.com/mattn/go-runewidth v0.0.7 // indirect
19-
github.com/russross/blackfriday/v2 v2.0.1 // indirect
20-
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
21-
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 // indirect
17+
github.com/mattn/go-runewidth v0.0.9 // indirect
18+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
19+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
20+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
2221
)

go.sum

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1-
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
21
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
32
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
43
github.com/corpix/uarand v0.1.1 h1:RMr1TWc9F4n5jiPDzFHtmaUXLKLNUFK0SgCLo4BhX/U=
54
github.com/corpix/uarand v0.1.1/go.mod h1:SFKZvkcRoLqVRFZ4u25xPmp6m9ktANfbpXZ7SJ0/FNU=
6-
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
7-
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
5+
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
6+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
7+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
88
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
99
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1010
github.com/dimchansky/utfbom v1.1.1 h1:vV6w1AhK4VMnhBno/TPVCoK9U/LP0PkLCS9tbxHdi/U=
1111
github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/j0mACtrfE=
12-
github.com/goodhosts/hostsfile v0.1.2 h1:FbN+0Ko5C/Hdhd2HhCXlSpVdcB30HQnhlVGZLZTiNVI=
13-
github.com/goodhosts/hostsfile v0.1.2/go.mod h1:HcPMANDthvsRjz5mWQuJrdWUgheVDz5MIm9bkBYHT78=
12+
github.com/goodhosts/hostsfile v0.1.3 h1:z5Qt3YZhrS/HT+pwWJMhQ3IrKZ44ZvAKx8UXz5RhydE=
13+
github.com/goodhosts/hostsfile v0.1.3/go.mod h1:HcPMANDthvsRjz5mWQuJrdWUgheVDz5MIm9bkBYHT78=
1414
github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428 h1:Mo9W14pwbO9VfRe+ygqZ8dFbPpoIK1HFrG/zjTuQ+nc=
1515
github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428/go.mod h1:uhpZMVGznybq1itEKXj6RYw9I71qK4kH+OGMjRC4KEo=
16-
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
1716
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
18-
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
19-
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
20-
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
21-
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
17+
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
18+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
19+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
20+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
2221
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2322
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
24-
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
25-
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
26-
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
27-
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
28-
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
23+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
24+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
2925
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
26+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
27+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
28+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3029
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3130
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
3231
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
3332
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3433
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1:J6v8awz+me+xeb/cUTotKgceAYouhIB3pjzgRd6IlGk=
3534
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA=
36-
github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k=
37-
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
38-
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
35+
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
36+
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
37+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
38+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
3939
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
40+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
41+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4042
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
41-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
42-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
4343
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
44+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
45+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
65
"io"
76
"os"
@@ -13,16 +12,19 @@ import (
1312
"github.com/urfave/cli/v2"
1413
)
1514

16-
var version = "dev"
15+
var (
16+
version = "dev"
17+
commit = "none"
18+
date = "unknown"
19+
)
1720

1821
func main() {
1922
app := &cli.App{
2023
Name: "goodhosts",
2124
Usage: "manage your hosts file goodly",
2225
Action: cmd.DefaultAction,
23-
Commands: cmd.Commands(),
26+
Commands: append(cmd.Commands(), Version()),
2427
Before: func(ctx *cli.Context) error {
25-
ctx.Context = context.WithValue(ctx.Context, cmd.VersionKey("version"), version)
2628
if ctx.Bool("debug") {
2729
logrus.SetLevel(logrus.DebugLevel)
2830
} else {
@@ -61,3 +63,15 @@ func main() {
6163
logrus.Fatal(err)
6264
}
6365
}
66+
67+
func Version() *cli.Command {
68+
return &cli.Command{
69+
Name: "version",
70+
Usage: "",
71+
Aliases: []string{"v", "ver"},
72+
Action: func(c *cli.Context) error {
73+
logrus.Infof("goodhosts %s@%s built on %s", version, commit, date)
74+
return nil
75+
},
76+
}
77+
}

0 commit comments

Comments
 (0)