Skip to content

Commit 69dce8e

Browse files
committed
Opt: Deduplication of configuration during addition
1 parent 24db9a8 commit 69dce8e

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

control/create/create.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ type Controller struct {
3030
func (cc Controller) ExecuteCreate() {
3131
switch cc.createType {
3232
case typeUsers:
33-
cc.configuration.Main.Users = append(cc.configuration.Main.Users, cc.createContent)
33+
cc.configuration.Main.Users = utils.RemoveDuplicate(
34+
append(cc.configuration.Main.Users, cc.createContent))
3435
cc.updateConfig()
3536
case typePorts:
36-
cc.configuration.Main.Ports = append(cc.configuration.Main.Ports, cc.createContent)
37+
cc.configuration.Main.Ports = utils.RemoveDuplicate(
38+
append(cc.configuration.Main.Ports, cc.createContent))
3739
cc.updateConfig()
3840
case typePasswords:
39-
cc.configuration.Main.Passwords = append(cc.configuration.Main.Passwords, cc.createContent)
41+
cc.configuration.Main.Passwords = utils.RemoveDuplicate(
42+
append(cc.configuration.Main.Passwords, cc.createContent))
4043
cc.updateConfig()
4144
case typeCaches:
4245
cc.createCaches()

utils/tools.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,16 @@ func InterfaceSlice(slice interface{}) []interface{} {
2323

2424
return ret
2525
}
26+
27+
func RemoveDuplicate(s []string) []string {
28+
result := make([]string, 0, len(s))
29+
temp := map[string]bool{}
30+
31+
for _, v := range s {
32+
if !temp[v] {
33+
temp[v] = true
34+
result = append(result, v)
35+
}
36+
}
37+
return result
38+
}

0 commit comments

Comments
 (0)