Skip to content

Commit 162f3bb

Browse files
Merge pull request #4 from guillaumerose/contains
Add contains command
2 parents 6174a02 + 46e6c9b commit 162f3bb

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

cmd/contains.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/code-ready/admin-helper/pkg/hosts"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var Contains = &cobra.Command{
11+
Use: "contains",
12+
Short: "Check if an ip and host are present in hosts file",
13+
Args: cobra.ExactArgs(2),
14+
RunE: func(cmd *cobra.Command, args []string) error {
15+
return contains(args)
16+
},
17+
}
18+
19+
func contains(args []string) error {
20+
hosts, err := hosts.New()
21+
if err != nil {
22+
return err
23+
}
24+
if hosts.Contains(args[0], args[1]) {
25+
fmt.Println("true")
26+
} else {
27+
fmt.Println("false")
28+
}
29+
return nil
30+
}

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ func Commands() []*cobra.Command {
99
Add,
1010
Remove,
1111
Clean,
12+
Contains,
1213
}
1314
}

pkg/hosts/hosts.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ func New() (*Hosts, error) {
1717
if err != nil {
1818
return nil, err
1919
}
20-
if !file.IsWritable() {
21-
return nil, fmt.Errorf("host file not writable, try running with elevated privileges")
22-
}
2320
return &Hosts{
2421
File: &file,
2522
}, nil
2623
}
2724

2825
func (h *Hosts) Add(ip string, hosts []string) error {
26+
if err := h.checkIsWritable(); err != nil {
27+
return err
28+
}
29+
2930
uniqueHosts := map[string]bool{}
3031
for i := 0; i < len(hosts); i++ {
3132
uniqueHosts[hosts[i]] = true
@@ -45,6 +46,10 @@ func (h *Hosts) Add(ip string, hosts []string) error {
4546
}
4647

4748
func (h *Hosts) Remove(hosts []string) error {
49+
if err := h.checkIsWritable(); err != nil {
50+
return err
51+
}
52+
4853
uniqueHosts := map[string]bool{}
4954
for i := 0; i < len(hosts); i++ {
5055
uniqueHosts[hosts[i]] = true
@@ -64,6 +69,10 @@ func (h *Hosts) Remove(hosts []string) error {
6469
}
6570

6671
func (h *Hosts) Clean(rawSuffixes []string) error {
72+
if err := h.checkIsWritable(); err != nil {
73+
return err
74+
}
75+
6776
var suffixes []string
6877
for _, suffix := range rawSuffixes {
6978
if !strings.HasPrefix(suffix, ".") {
@@ -91,3 +100,14 @@ func (h *Hosts) Clean(rawSuffixes []string) error {
91100
}
92101
return h.File.Flush()
93102
}
103+
104+
func (h *Hosts) checkIsWritable() error {
105+
if !h.File.IsWritable() {
106+
return fmt.Errorf("host file not writable, try running with elevated privileges")
107+
}
108+
return nil
109+
}
110+
111+
func (h *Hosts) Contains(ip, host string) bool {
112+
return h.File.Has(ip, host)
113+
}

pkg/hosts/hosts_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ func TestClean(t *testing.T) {
6363
assert.Equal(t, "127.0.0.1 entry2.suffix2"+eol(), string(content))
6464
}
6565

66+
func TestContains(t *testing.T) {
67+
dir, err := ioutil.TempDir("", "hosts")
68+
assert.NoError(t, err)
69+
defer os.RemoveAll(dir)
70+
71+
hostsFile := filepath.Join(dir, "hosts")
72+
assert.NoError(t, ioutil.WriteFile(hostsFile, []byte(`127.0.0.1 entry1.suffix1 entry2.suffix2`), 0600))
73+
74+
host := hosts(t, hostsFile)
75+
76+
assert.True(t, host.Contains("127.0.0.1", "entry1.suffix1"))
77+
assert.False(t, host.Contains("127.0.0.2", "entry1.suffix1"))
78+
assert.False(t, host.Contains("127.0.0.1", "entry1.suffix2"))
79+
}
80+
6681
func hosts(t *testing.T, hostsFile string) Hosts {
6782
file, err := hostsfile.NewCustomHosts(hostsFile)
6883
assert.NoError(t, err)

0 commit comments

Comments
 (0)