From 4c3583cd5aefbfebf3da7b05f41dfa4fef689c40 Mon Sep 17 00:00:00 2001 From: Tom Limoncelli Date: Wed, 25 Jan 2023 12:17:21 -0500 Subject: [PATCH] Domains flag should accept simple wildcards (#1983) --- commands/commands.go | 11 ++++- commands/commands_test.go | 63 +++++++++++++++++++++++++++++ documentation/functions/global/D.md | 2 +- 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 commands/commands_test.go diff --git a/commands/commands.go b/commands/commands.go index b01f043ab..82043545f 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -294,8 +294,15 @@ func (args *FilterArgs) shouldRunDomain(d string) bool { if args.Domains == "" { return true } - for _, dom := range strings.Split(args.Domains, ",") { - if dom == d { + return domainInList(d, strings.Split(args.Domains, ",")) +} + +func domainInList(domain string, list []string) bool { + for _, item := range list { + if strings.HasPrefix(item, "*") && strings.HasSuffix(domain, item[1:]) { + return true + } + if item == domain { return true } } diff --git a/commands/commands_test.go b/commands/commands_test.go new file mode 100644 index 000000000..046889734 --- /dev/null +++ b/commands/commands_test.go @@ -0,0 +1,63 @@ +package commands + +import "testing" + +func Test_domainInList(t *testing.T) { + type args struct { + domain string + list []string + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "small", + args: args{ + domain: "foo.com", + list: []string{"foo.com"}, + }, + want: true, + }, + { + name: "big", + args: args{ + domain: "foo.com", + list: []string{"example.com", "foo.com", "baz.com"}, + }, + want: true, + }, + { + name: "missing", + args: args{ + domain: "foo.com", + list: []string{"bar.com"}, + }, + want: false, + }, + { + name: "wildcard", + args: args{ + domain: "*.10.in-addr.arpa", + list: []string{"bar.com", "10.in-addr.arpa", "example.com"}, + }, + want: false, + }, + { + name: "wildcardmissing", + args: args{ + domain: "*.10.in-addr.arpa", + list: []string{"bar.com", "6.in-addr.arpa", "example.com"}, + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := domainInList(tt.args.domain, tt.args.list); got != tt.want { + t.Errorf("domainInList() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/documentation/functions/global/D.md b/documentation/functions/global/D.md index 33a6f3157..d6cb9dfdc 100644 --- a/documentation/functions/global/D.md +++ b/documentation/functions/global/D.md @@ -85,7 +85,7 @@ and `domain.tld!external` you now require humans to remember that may have noticed this mistake, but will your coworkers? Will you in six months? You get the idea. -DNSControl command line flag `--domains` is an exact match. If you +DNSControl command line flag `--domains` matches the full name (with the "!"). If you define domains `example.com!george` and `example.com!john` then: * `--domains=example.com` will not match either domain.