Initial commit.

This commit is contained in:
2021-06-06 23:13:55 +05:00
commit 462d09cee2
16 changed files with 631 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package configuration
import (
"errors"
"flag"
"fmt"
)
var (
// ErrConfigurationError can be used to determine if received error was
// about invalid configuration.
ErrConfigurationError = errors.New("configuration")
// DomainPostfix is a string that will be added to parsed domain if not already
// present. E.g. with DomainPostfix = "example.com" for parsed domain "host-1"
// it will be added and parsed domain will be in form "host-1.example.com", but
// for parsed domain "host-2.example.com" it won't be added as it is already
// present in the very end.
DomainPostfix string
// HostsFilePath defines a path from which hosts file will be used for parsing.
HostsFilePath string
// StorageToUse defines storage translator will use for updating data.
StorageToUse string
)
// Initialize initializes configuration subsystem.
func Initialize() {
flag.StringVar(&DomainPostfix, "domain-postfix", "", "Postfix to append to domain. Some storages requires this parameter to be filled.")
flag.StringVar(&HostsFilePath, "hosts-file", "", "Path to hosts file to parse.")
flag.StringVar(&StorageToUse, "storage", "", "Storage to use. Currently supported: 'powerdns'.")
initializePowerDNS()
}
func Parse() {
flag.Parse()
}
// Validate validates configuration data and returns error is something isn't right.
func Validate() error {
if HostsFilePath == "" {
return fmt.Errorf("%w: empty hosts file path", ErrConfigurationError)
}
if StorageToUse == "" {
return fmt.Errorf("%w: no storage name was provided", ErrConfigurationError)
}
if err := validatePowerDNS(); err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,43 @@
package configuration
import (
"flag"
"fmt"
"strings"
)
var (
// PowerDNSAPIKey defines API key for PowerDNS HTTP API.
PowerDNSAPIKey string
// PowerDNSURI defines URL for PowerDNS HTTP API.
PowerDNSURI string
)
func initializePowerDNS() {
flag.StringVar(&PowerDNSAPIKey, "powerdns-api-key", "", "API key for PowerDNS HTTP API.")
flag.StringVar(&PowerDNSURI, "powerdns-uri", "", "URI for PowerDNS API. Should be in 'proto://ADDR:PORT' form.")
}
func validatePowerDNS() error {
if strings.ToLower(StorageToUse) != "powerdns" {
return nil
}
// PowerDNS storage requires DomainSuffix to determine zone name to update.
if DomainPostfix == "" {
return fmt.Errorf("%w: domain postfix isn't filled which is required by PowerDNS storage", ErrConfigurationError)
}
if PowerDNSAPIKey == "" {
return fmt.Errorf("%w: no PowerDNS API key was provided", ErrConfigurationError)
}
if PowerDNSURI == "" {
return fmt.Errorf("%w: no PowerDNS HTTP API server URI provided", ErrConfigurationError)
}
// Hack: trim slashes in end.
PowerDNSURI = strings.TrimRight(PowerDNSURI, "/")
return nil
}