dnscontrol/pkg/rtypecontrol/rtypecontrol.go
Tom Limoncelli 7ab7d147fb
CHORE: Move non-provider code out of /providers (#3916)
# Issue

Fixes https://github.com/StackExchange/dnscontrol/issues/3912

# Resolution

```
#!/bin/sh

# Reset

git fetch origin main
git reset --hard origin/main
git checkout main
git branch -D tlim_moveproviders
git checkout -b tlim_moveproviders
find . -name \*.bak -delete

# Move the *.go files out of providers/

mkdir -p pkg/providers
git mv providers/*.go pkg/providers

# move the _all file out of providers/

git mv providers/_all pkg/providers/_all

# Update the imports (in go.* and the affected files)

sed -i.bak -e 's@"github.com/StackExchange/dnscontrol/v4/providers"@"github.com/StackExchange/dnscontrol/v4/pkg/providers"@g' go.* $(fgrep -lr --include '*.go' '"github.com/StackExchange/dnscontrol/v4/providers"' *)
sed -i.bak -e 's@"../../providers"@"../../pkg/providers"@g' pkg/normalize/capabilities_test.go
sed -i.bak -e 's@"github.com/StackExchange/dnscontrol/v4/providers/_all"@"github.com/StackExchange/dnscontrol/v4/pkg/providers/_all"@g' go.* $(fgrep -lr --include '*.go' '"github.com/StackExchange/dnscontrol/v4/providers/_all"' *)

# Fix the docs

sed -i.bak -e 's@StackExchange/dnscontrol/blob/main/providers/_all/all.go@StackExchange/dnscontrol/blob/main/pkg/providers/_all/all.go@g' documentation/advanced-features/writing-providers.md
sed -i.bak -e 's@StackExchange/dnscontrol/providers@StackExchange/dnscontrol/pkg/providers@g' documentation/advanced-features/writing-providers.md
sed -i.bak -e 's@StackExchange/dnscontrol/v4/providers@StackExchange/dnscontrol/v4/pkg/providers@g' documentation/advanced-features/writing-providers.md
sed -i.bak -e 's@dnscontrol/providers/providers.go@dnscontrol/pkg/providers/providers.go@g' documentation/advanced-features/writing-providers.md
sed -i.bak -e 's@providers/_all/all.go@pkg/providers/_all/all.go@g' documentation/advanced-features/writing-providers.md
#sed -i.bak -e 's@@@g' documentation/advanced-features/writing-providers.md
#sed -i.bak -e 's@@@g' documentation/advanced-features/writing-providers.md

find . -name \*.bak -delete

go fmt ./...

git status

echo git commit -a -m'CHORE: Move Non-provider files in providers to pkg/providers'



```
2025-12-15 12:53:52 -05:00

40 lines
1.3 KiB
Go

package rtypecontrol
import (
"fmt"
"github.com/StackExchange/dnscontrol/v4/models"
"github.com/StackExchange/dnscontrol/v4/pkg/domaintags"
"github.com/StackExchange/dnscontrol/v4/pkg/providers"
)
// RType is an interface that defines the methods required for a DNS record type.
type RType interface {
// Returns the name of the rtype ("A", "MX", etc.)
Name() string
// RecordConfig factory. Updates a RecordConfig's fields based on args.
FromArgs(*domaintags.DomainNameVarieties, *models.RecordConfig, []any) error
FromStruct(*domaintags.DomainNameVarieties, *models.RecordConfig, string, any) error
CopyToLegacyFields(*models.RecordConfig)
}
// Func is a map of registered rtypes.
var Func map[string]RType = map[string]RType{}
// Register registers a new RType (Record Type) implementation. It can be used
// to register an RFC-defined type, a new custom type, or a "builder".
//
// It panics if the type is already registered, to prevent accidental overwrites.
func Register(t RType) {
name := t.Name()
if _, ok := Func[name]; ok {
panic(fmt.Sprintf("rtype %q already registered. Can't register it a second time!", name))
}
// Store the interface
Func[name] = t
// For compatibility with legacy systems:
providers.RegisterCustomRecordType(name, "", "")
}