forked from realDragonium/Ultraviolet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
472f784
commit 16e4343
Showing
11 changed files
with
384 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type DuplicateDomain struct { | ||
Cfg1Path string | ||
Cfg2Path string | ||
Domain string | ||
} | ||
|
||
func (err *DuplicateDomain) Error() string { | ||
return fmt.Sprintf("'%s' has been found in %s and %s", err.Domain, err.Cfg1Path, err.Cfg2Path) | ||
} | ||
|
||
func VerifyConfigs(cfgs []ServerConfig) []error { | ||
errors := []error{} | ||
domains := make(map[string]int) | ||
for index, cfg := range cfgs { | ||
for _, domain := range cfg.Domains { | ||
otherIndex, ok := domains[domain] | ||
if ok { | ||
errors = append(errors, &DuplicateDomain{ | ||
Domain: domain, | ||
Cfg1Path: cfg.FilePath, | ||
Cfg2Path: cfgs[otherIndex].FilePath, | ||
}) | ||
continue | ||
} | ||
domains[domain] = index | ||
} | ||
} | ||
return errors | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/realDragonium/Ultraviolet/config" | ||
) | ||
|
||
func TestVerifyConfigs(t *testing.T) { | ||
t.Run("can detect duplicate domains", func(t *testing.T) { | ||
domain := "uv" | ||
cfgs := []config.ServerConfig{ | ||
{ | ||
FilePath: "uv1", | ||
Domains: []string{domain}, | ||
}, | ||
{ | ||
FilePath: "uv2", | ||
Domains: []string{domain}, | ||
}, | ||
} | ||
|
||
errs := config.VerifyConfigs(cfgs) | ||
if len(errs) != 1 { | ||
t.Log(errs) | ||
t.Fatalf("expected 1 errors but got %d", len(errs)) | ||
} | ||
_, ok := errs[0].(*config.DuplicateDomain) | ||
if !ok { | ||
t.Errorf("expected DuplicateDomain but got %T", errs[0]) | ||
} | ||
}) | ||
|
||
t.Run("can detect multiple duplicate domains", func(t *testing.T) { | ||
domain := "uv" | ||
cfgs := []config.ServerConfig{ | ||
{ | ||
FilePath: "uv1", | ||
Domains: []string{domain}, | ||
}, | ||
{ | ||
FilePath: "uv2", | ||
Domains: []string{domain}, | ||
}, | ||
{ | ||
FilePath: "uv3", | ||
Domains: []string{domain}, | ||
}, | ||
} | ||
|
||
errs := config.VerifyConfigs(cfgs) | ||
if len(errs) != 2 { | ||
t.Fatalf("expected 2 errors but got %d", len(errs)) | ||
} | ||
}) | ||
} |
Oops, something went wrong.