From 92fd4ea547c079bb16619d19c2aa81ddbaf71859 Mon Sep 17 00:00:00 2001 From: Luther Monson Date: Fri, 16 Oct 2020 12:43:10 -0700 Subject: [PATCH 1/2] vendor --- go.mod | 2 +- go.sum | 4 ++-- .../github.com/goodhosts/hostsfile/hosts.go | 19 +++++++++++++++++-- vendor/modules.txt | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 8265ba2..dcaf90d 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/goodhosts/cli go 1.13 require ( - github.com/goodhosts/hostsfile v0.0.6 + github.com/goodhosts/hostsfile v0.0.7 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 diff --git a/go.sum b/go.sum index e66d454..2c05ecc 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dimchansky/utfbom v1.1.0 h1:FcM3g+nofKgUteL8dm/UpdRXNC9KmADgTpLKsu0TRo4= github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8= -github.com/goodhosts/hostsfile v0.0.6 h1:th49slfHTGLbSon0bR7jbBiBl5EpjFB42WzS//P3jVg= -github.com/goodhosts/hostsfile v0.0.6/go.mod h1:MAfdBdP0f9MVmfhmNP4EjQxPu7J/WnncHv8p/J8hkLs= +github.com/goodhosts/hostsfile v0.0.7 h1:5yBaORuv1dybDhDRju32bQQ1l4iHKJs+h6GIgFV4qJQ= +github.com/goodhosts/hostsfile v0.0.7/go.mod h1:MAfdBdP0f9MVmfhmNP4EjQxPu7J/WnncHv8p/J8hkLs= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54= diff --git a/vendor/github.com/goodhosts/hostsfile/hosts.go b/vendor/github.com/goodhosts/hostsfile/hosts.go index 52d49c8..97c1b85 100644 --- a/vendor/github.com/goodhosts/hostsfile/hosts.go +++ b/vendor/github.com/goodhosts/hostsfile/hosts.go @@ -279,13 +279,13 @@ func (h *Hosts) combineIp(ip string) { linesCopy := make([]HostsLine, len(h.Lines)) copy(linesCopy, h.Lines) - for i, line := range linesCopy { + for _, line := range linesCopy { if line.IP == ip { newLine.Combine(line) - h.removeByPosition(i) } } newLine.SortHosts() + h.removeIp(ip) h.Lines = append(h.Lines, newLine) } @@ -294,9 +294,24 @@ func (h *Hosts) removeByPosition(pos int) { h.Lines = []HostsLine{} return } + if pos == len(h.Lines) { + h.Lines = h.Lines[:pos-1] + return + } h.Lines = append(h.Lines[:pos], h.Lines[pos+1:]...) } +func (h *Hosts) removeIp(ip string) { + var newLines []HostsLine + for _, line := range h.Lines { + if line.IP != ip { + newLines = append(newLines, line) + } + } + + h.Lines = newLines +} + func (h Hosts) getHostPosition(ip string, host string) int { for i := range h.Lines { line := h.Lines[i] diff --git a/vendor/modules.txt b/vendor/modules.txt index 030b791..c4671b8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -2,7 +2,7 @@ github.com/cpuguy83/go-md2man/v2/md2man # github.com/dimchansky/utfbom v1.1.0 github.com/dimchansky/utfbom -# github.com/goodhosts/hostsfile v0.0.6 +# github.com/goodhosts/hostsfile v0.0.7 github.com/goodhosts/hostsfile # github.com/konsorten/go-windows-terminal-sequences v1.0.1 github.com/konsorten/go-windows-terminal-sequences From c978af65e3c6d8a0ef14f489987726991439a9e1 Mon Sep 17 00:00:00 2001 From: Luther Monson Date: Fri, 16 Oct 2020 12:43:28 -0700 Subject: [PATCH 2/2] adding version command --- cmd/main.go | 11 ++++++----- cmd/version.go | 19 +++++++++++++++++++ main.go | 4 ++++ 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 cmd/version.go diff --git a/cmd/main.go b/cmd/main.go index 52b2eea..bf3701b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,15 +17,16 @@ func Run(c *cli.Context) error { func Commands() []*cli.Command { return []*cli.Command{ + Add(), + Backup(), Check(), + Clean(), + Debug(), + Edit(), List(), - Add(), Remove(), - Debug(), - Backup(), Restore(), - Edit(), - Clean(), + Version(), } } diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..1556969 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "github.com/sirupsen/logrus" + "github.com/urfave/cli/v2" +) + +func Version() *cli.Command { + return &cli.Command{ + Name: "version", + Usage: "", + Action: version, + } +} + +func version(c *cli.Context) error { + logrus.Infof("goodhosts %s", c.Context.Value("version")) + return nil +} diff --git a/main.go b/main.go index dc8db67..1a4df28 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "io/ioutil" "os" @@ -12,6 +13,8 @@ import ( "github.com/urfave/cli/v2" ) +var version = "dev" + func main() { app := &cli.App{ Name: "goodhosts", @@ -19,6 +22,7 @@ func main() { Action: cmd.DefaultAction, Commands: cmd.Commands(), Before: func(ctx *cli.Context) error { + ctx.Context = context.WithValue(ctx.Context, "version", version) if ctx.Bool("debug") { logrus.SetLevel(logrus.DebugLevel) } else {