Skip to content

Commit 9477b29

Browse files
committed
feat: treat pairs domain/ip always as uniq. Fixes #71
1 parent 2c4ea86 commit 9477b29

File tree

13 files changed

+92
-57
lines changed

13 files changed

+92
-57
lines changed

cmd/hostctl/actions/add_domains_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,53 @@ func Test_AddDomains(t *testing.T) {
6060
+----------+--------+-----------+----------------+
6161
`, r.Hostfile())
6262
})
63+
64+
t.Run("Add domains uniq", func(t *testing.T) {
65+
r := NewRunner(t, cmd, "add_uniq")
66+
defer r.Clean()
67+
68+
r.Run("hostctl add domains awesome same.domain.loc --ip 3.3.3.3").
69+
Run("hostctl add domains awesome same.domain.loc --ip 3.3.3.3").
70+
Containsf(`
71+
[ℹ] Using hosts file: %s
72+
73+
[✔] Domains 'same.domain.loc' added.
74+
75+
+---------+--------+---------+-----------------+
76+
| PROFILE | STATUS | IP | DOMAIN |
77+
+---------+--------+---------+-----------------+
78+
| awesome | on | 3.3.3.3 | same.domain.loc |
79+
+---------+--------+---------+-----------------+
80+
`, r.Hostfile())
81+
})
82+
83+
t.Run("Add domains uniq per profile", func(t *testing.T) {
84+
r := NewRunner(t, cmd, "multi_profile")
85+
defer r.Clean()
86+
87+
r.Run("hostctl add domains another same.domain.loc --ip 3.3.3.3").
88+
Run("hostctl add domains another same.domain.loc --ip 3.3.3.3").
89+
Run("hostctl add domains awesome same.domain.loc --ip 3.3.3.3").
90+
Run("hostctl add domains awesome same.domain.loc --ip 3.3.3.3").
91+
Run("hostctl list another").
92+
Containsf(`
93+
[ℹ] Using hosts file: %s
94+
95+
+---------+--------+---------+-----------------+
96+
| PROFILE | STATUS | IP | DOMAIN |
97+
+---------+--------+---------+-----------------+
98+
| another | on | 3.3.3.3 | same.domain.loc |
99+
+---------+--------+---------+-----------------+
100+
`, r.Hostfile()).
101+
Run("hostctl list awesome").
102+
Containsf(`
103+
[ℹ] Using hosts file: %s
104+
105+
+---------+--------+---------+-----------------+
106+
| PROFILE | STATUS | IP | DOMAIN |
107+
+---------+--------+---------+-----------------+
108+
| awesome | on | 3.3.3.3 | same.domain.loc |
109+
+---------+--------+---------+-----------------+
110+
`, r.Hostfile())
111+
})
63112
}

cmd/hostctl/actions/add_replace.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ func makeAddReplace(actionFn addRemoveFn) func(cmd *cobra.Command, profiles []st
5858
return func(cmd *cobra.Command, profiles []string) error {
5959
src, _ := cmd.Flags().GetString("host-file")
6060
from, _ := cmd.Flags().GetString("from")
61-
uniq, _ := cmd.Flags().GetBool("uniq")
6261
in := cmd.InOrStdin()
6362

64-
p, err := getProfileFromInput(in, from, uniq)
63+
p, err := getProfileFromInput(in, from)
6564
if err != nil {
6665
return err
6766
}
@@ -83,7 +82,7 @@ func makeAddReplace(actionFn addRemoveFn) func(cmd *cobra.Command, profiles []st
8382
}
8483
}
8584

86-
func getProfileFromInput(in io.Reader, from string, uniq bool) (*types.Profile, error) {
85+
func getProfileFromInput(in io.Reader, from string) (*types.Profile, error) {
8786
var (
8887
r io.Reader
8988
err error
@@ -104,5 +103,5 @@ func getProfileFromInput(in io.Reader, from string, uniq bool) (*types.Profile,
104103
return nil, err
105104
}
106105

107-
return parser.ParseProfile(r, uniq)
106+
return parser.ParseProfile(r)
108107
}

cmd/hostctl/actions/add_replace_test.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@ func Test_Add(t *testing.T) {
1212
r := NewRunner(t, cmd, "add")
1313
defer r.Clean()
1414

15-
tmp := r.TempHostfile("source")
16-
defer os.Remove(tmp.Name())
17-
1815
t.Run("Add from file", func(t *testing.T) {
19-
r.Runf("hostctl add awesome --uniq --from %s", tmp.Name()).
16+
tmp := r.TempHostfile("source")
17+
defer os.Remove(tmp.Name())
18+
r.Runf("hostctl add awesome --from %s", tmp.Name()).
19+
Contains(`
20+
+---------+--------+-----------+------------+
21+
| PROFILE | STATUS | IP | DOMAIN |
22+
+---------+--------+-----------+------------+
23+
| awesome | on | 127.0.0.1 | localhost |
24+
| awesome | on | 127.0.0.1 | first.loc |
25+
| awesome | on | 127.0.0.1 | second.loc |
26+
+---------+--------+-----------+------------+
27+
`)
28+
})
29+
30+
t.Run("Add from file uniq", func(t *testing.T) {
31+
tmp := r.TempHostfile("source")
32+
defer os.Remove(tmp.Name())
33+
r.Runf("hostctl add awesome --from %s", tmp.Name()).
34+
Runf("hostctl add awesome --from %s", tmp.Name()).
2035
Contains(`
2136
+---------+--------+-----------+------------+
2237
| PROFILE | STATUS | IP | DOMAIN |

cmd/hostctl/actions/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestReadFromURL(t *testing.T) {
116116
r, err := readerFromURL(server.URL)
117117
assert.NoError(t, err)
118118

119-
p, err := parser.ParseProfile(r, true)
119+
p, err := parser.ParseProfile(r)
120120
assert.NoError(t, err)
121121

122122
hosts := p.GetAllHostNames()

cmd/hostctl/actions/root.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ func registerCommands(rootCmd *cobra.Command) {
101101
addCmd.Flags().StringP("from", "f", "", "file to read")
102102
addCmd.PersistentFlags().
103103
DurationP("wait", "w", -1, "Enables a profile for a specific amount of time. (example: 5m, 1h)")
104-
addCmd.PersistentFlags().BoolP("uniq", "u", false, "only keep uniq domains per IP")
105104

106105
// remove
107106
removeCmd.Flags().Bool("all", false, "Remove all profiles")
@@ -113,7 +112,6 @@ func registerCommands(rootCmd *cobra.Command) {
113112
// replace
114113
replaceCmd := newReplaceCmd()
115114
replaceCmd.Flags().StringP("from", "f", "", "file to read")
116-
replaceCmd.Flags().BoolP("uniq", "u", false, "only keep uniq domains per IP")
117115

118116
// toggle
119117
toggleCmd := newToggleCmd()

pkg/file/add_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestFile_AddProfile(t *testing.T) {
2020
assert.NoError(t, err)
2121
r := strings.NewReader(`127.0.0.1 added.loc`)
2222

23-
p, err := parser.ParseProfile(r, true)
23+
p, err := parser.ParseProfile(r)
2424
assert.NoError(t, err)
2525
p.Name = "awesome"
2626
p.Status = types.Enabled
@@ -41,7 +41,7 @@ func TestFile_AddProfile(t *testing.T) {
4141
assert.NoError(t, err)
4242
r := strings.NewReader(`127.0.0.1 added.loc`)
4343

44-
p, err := parser.ParseProfile(r, true)
44+
p, err := parser.ParseProfile(r)
4545
assert.NoError(t, err)
4646
p.Name = "profile1"
4747

@@ -63,7 +63,7 @@ func TestFile_AddProfile(t *testing.T) {
6363
assert.NoError(t, err)
6464
r := strings.NewReader(`127.0.0.1 added.loc`)
6565

66-
p, err := parser.ParseProfile(r, true)
66+
p, err := parser.ParseProfile(r)
6767
assert.NoError(t, err)
6868
p.Name = "default"
6969

pkg/file/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (f *File) AddRoutes(name string, routes []*types.Route) error {
138138
}
139139

140140
// RemoveHostnames removes route information from a given types.
141-
// also removes the profile if gets empty.
141+
// also removes the profile if it gets empty.
142142
func (f *File) RemoveHostnames(name string, routes []string) (bool, error) {
143143
p, err := f.GetProfile(name)
144144
if err != nil {

pkg/file/file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestManagerRoutes(t *testing.T) {
114114
assert.NoError(t, err)
115115

116116
r := strings.NewReader(`3.3.3.4 some.profile.loc`)
117-
p, err := parser.ParseProfile(r, true)
117+
p, err := parser.ParseProfile(r)
118118
assert.NoError(t, err)
119119

120120
h, _ := mem.OpenFile("/tmp/etc/hosts", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)

pkg/file/replace_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestFile_ReplaceProfile(t *testing.T) {
2121

2222
r := strings.NewReader(`4.4.4.4 replaced.loc`)
2323

24-
p, err := parser.ParseProfile(r, true)
24+
p, err := parser.ParseProfile(r)
2525
assert.NoError(t, err)
2626
p.Name = "profile1"
2727
p.Status = types.Enabled
@@ -42,7 +42,7 @@ func TestFile_ReplaceProfile(t *testing.T) {
4242

4343
r := strings.NewReader(`4.4.4.4 replaced.loc`)
4444

45-
p, err := parser.ParseProfile(r, true)
45+
p, err := parser.ParseProfile(r)
4646
assert.NoError(t, err)
4747
p.Name = "awesome"
4848
p.Status = types.Enabled
@@ -63,7 +63,7 @@ func TestFile_ReplaceProfile(t *testing.T) {
6363

6464
r := strings.NewReader(`4.4.4.4 replaced.loc`)
6565

66-
p, err := parser.ParseProfile(r, true)
66+
p, err := parser.ParseProfile(r)
6767
assert.NoError(t, err)
6868
p.Name = types.Default
6969
p.Status = types.Enabled

pkg/parser/parser.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func parseRouteLine(str string) (*types.Route, bool) {
162162
}
163163

164164
// ParseProfile creates a new profile reading lines from a reader
165-
func ParseProfile(r io.Reader, uniq bool) (*types.Profile, error) {
165+
func ParseProfile(r io.Reader) (*types.Profile, error) {
166166
p := &types.Profile{}
167167
s := bufio.NewScanner(r)
168168

@@ -186,11 +186,7 @@ func ParseProfile(r io.Reader, uniq bool) (*types.Profile, error) {
186186
}
187187
}
188188

189-
if uniq {
190-
p.AddRoutesUniq(routes)
191-
} else {
192-
p.AddRoutes(routes)
193-
}
189+
p.AddRoutes(routes)
194190

195191
return p, nil
196192
}

0 commit comments

Comments
 (0)