Skip to content

Commit

Permalink
feature comment filtering enable, disable
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuheiKubota committed Oct 4, 2019
1 parent c0f9856 commit 33e1837
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
18 changes: 11 additions & 7 deletions cmd_disable.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package main

import "errors"
import (
"errors"
)

type disableCmd struct {
_ struct{} `help:"change normal entry -> comment if exists"`
IP *string
Host *string
_ struct{} `help:"change normal entry -> comment if exists"`

IP *string
Host *string
Comment *string
}

func (c disableCmd) Run(g globalCmd) error {
if c.IP == nil && c.Host == nil {
return errors.New("IP or Host is required")
if c.IP == nil && c.Host == nil && c.Comment == nil {
return errors.New("IP or Host or Comment is required")
}

el, err := readEntries(g.Hosts)
Expand All @@ -31,7 +35,7 @@ func (c disableCmd) disable(el []entry) []entry {

for i, e := range el {
found := false
if matches(e, c.IP, c.Host) {
if matches(e, c.IP, c.Host, c.Comment) {
found = true
}

Expand Down
15 changes: 15 additions & 0 deletions cmd_disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,19 @@ func TestDisable(t *testing.T) {
gotwant.Test(t, result, ``)
})

t.Run("Comment", func(t *testing.T) {
comment := "HOST"
el := read(hosts)
disable := disableCmd{Comment: &comment}
el = disable.disable(el)
result := list(el)
gotwant.Test(t, result, `# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# 127.0.0.1 localhost # THIS IS LOCALHOST
::1 localhost
# 118.151.235.191 example.com
# 192.168.1.201 server01 # new server
`)
})

}
11 changes: 6 additions & 5 deletions cmd_enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
type enableCmd struct {
_ struct{} `help:"change comment -> normal entry if exists"`

IP *string
Host *string
IP *string
Host *string
Comment *string
}

func (c enableCmd) Run(g globalCmd) error {
if c.IP == nil && c.Host == nil {
return errors.New("IP or Host is required")
if c.IP == nil && c.Host == nil && c.Comment == nil {
return errors.New("IP or Host or Comment is required")
}

el, err := readEntries(g.Hosts)
Expand All @@ -34,7 +35,7 @@ func (c enableCmd) enable(el []entry) []entry {

for i, e := range el {
found := false
if matches(e, c.IP, c.Host) {
if matches(e, c.IP, c.Host, c.Comment) {
found = true
}

Expand Down
15 changes: 15 additions & 0 deletions cmd_enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,19 @@ func TestEnable(t *testing.T) {
gotwant.Test(t, result, ``)
})

t.Run("Comment", func(t *testing.T) {
comment := "server"
el := read(hosts)
enable := enableCmd{Comment: &comment}
el = enable.enable(el)
result := list(el)
gotwant.Test(t, result, `102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
127.0.0.1 localhost # THIS IS LOCALHOST
::1 localhost
# 118.151.235.191 example.com
192.168.1.201 server01 # new server
`)
})

}
20 changes: 15 additions & 5 deletions hosted.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"io"
"os"
"strings"
"time"

"github.com/shu-go/gli"
Expand Down Expand Up @@ -78,15 +79,24 @@ func writeEntriesToWriter(out io.Writer, el []entry) error {
return err
}

func matches(e entry, ip, host *string) bool {
if ip != nil && e.IP != *ip {
func matches(e entry, ip, host, comment *string) bool {
if ip == nil && host == nil && comment == nil {
return false
}
if host != nil && e.Host != *host {
return false

matching := true

if ip != nil {
matching = matching && (e.IP == *ip)
}
if host != nil {
matching = matching && (e.Host == *host)
}
if comment != nil {
matching = matching && strings.Contains(e.Comment, *comment)
}

return true
return matching
}

func main() {
Expand Down

0 comments on commit 33e1837

Please sign in to comment.