Skip to content

Commit

Permalink
Merge pull request #4 from guillaumerose/contains
Browse files Browse the repository at this point in the history
Add contains command
  • Loading branch information
guillaumerose authored Dec 23, 2020
2 parents 6174a02 + 46e6c9b commit 162f3bb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
30 changes: 30 additions & 0 deletions cmd/contains.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"fmt"

"github.com/code-ready/admin-helper/pkg/hosts"
"github.com/spf13/cobra"
)

var Contains = &cobra.Command{
Use: "contains",
Short: "Check if an ip and host are present in hosts file",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return contains(args)
},
}

func contains(args []string) error {
hosts, err := hosts.New()
if err != nil {
return err
}
if hosts.Contains(args[0], args[1]) {
fmt.Println("true")
} else {
fmt.Println("false")
}
return nil
}
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ func Commands() []*cobra.Command {
Add,
Remove,
Clean,
Contains,
}
}
26 changes: 23 additions & 3 deletions pkg/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ func New() (*Hosts, error) {
if err != nil {
return nil, err
}
if !file.IsWritable() {
return nil, fmt.Errorf("host file not writable, try running with elevated privileges")
}
return &Hosts{
File: &file,
}, nil
}

func (h *Hosts) Add(ip string, hosts []string) error {
if err := h.checkIsWritable(); err != nil {
return err
}

uniqueHosts := map[string]bool{}
for i := 0; i < len(hosts); i++ {
uniqueHosts[hosts[i]] = true
Expand All @@ -45,6 +46,10 @@ func (h *Hosts) Add(ip string, hosts []string) error {
}

func (h *Hosts) Remove(hosts []string) error {
if err := h.checkIsWritable(); err != nil {
return err
}

uniqueHosts := map[string]bool{}
for i := 0; i < len(hosts); i++ {
uniqueHosts[hosts[i]] = true
Expand All @@ -64,6 +69,10 @@ func (h *Hosts) Remove(hosts []string) error {
}

func (h *Hosts) Clean(rawSuffixes []string) error {
if err := h.checkIsWritable(); err != nil {
return err
}

var suffixes []string
for _, suffix := range rawSuffixes {
if !strings.HasPrefix(suffix, ".") {
Expand Down Expand Up @@ -91,3 +100,14 @@ func (h *Hosts) Clean(rawSuffixes []string) error {
}
return h.File.Flush()
}

func (h *Hosts) checkIsWritable() error {
if !h.File.IsWritable() {
return fmt.Errorf("host file not writable, try running with elevated privileges")
}
return nil
}

func (h *Hosts) Contains(ip, host string) bool {
return h.File.Has(ip, host)
}
15 changes: 15 additions & 0 deletions pkg/hosts/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ func TestClean(t *testing.T) {
assert.Equal(t, "127.0.0.1 entry2.suffix2"+eol(), string(content))
}

func TestContains(t *testing.T) {
dir, err := ioutil.TempDir("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
assert.NoError(t, ioutil.WriteFile(hostsFile, []byte(`127.0.0.1 entry1.suffix1 entry2.suffix2`), 0600))

host := hosts(t, hostsFile)

assert.True(t, host.Contains("127.0.0.1", "entry1.suffix1"))
assert.False(t, host.Contains("127.0.0.2", "entry1.suffix1"))
assert.False(t, host.Contains("127.0.0.1", "entry1.suffix2"))
}

func hosts(t *testing.T, hostsFile string) Hosts {
file, err := hostsfile.NewCustomHosts(hostsFile)
assert.NoError(t, err)
Expand Down

0 comments on commit 162f3bb

Please sign in to comment.