first commit
This commit is contained in:
commit
4b1861fa9b
14 changed files with 811 additions and 0 deletions
75
dnsconfig/zones.go
Normal file
75
dnsconfig/zones.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package dnsconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Record struct {
|
||||
Name string `toml:"name"`
|
||||
Type string `toml:"type"`
|
||||
Value string `toml:"value"`
|
||||
Flat bool `toml:"flat"`
|
||||
TTL int `toml:"ttl"`
|
||||
}
|
||||
|
||||
type Zone struct {
|
||||
Domain string `toml:"domain"`
|
||||
DefaultTTL int `toml:"default_ttl"`
|
||||
Records []Record `toml:"record"`
|
||||
}
|
||||
|
||||
func GetZones() ([]Zone, error) {
|
||||
var zones []Zone
|
||||
|
||||
files, err := os.ReadDir("./zones/")
|
||||
if err != nil {
|
||||
return zones, err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
name := file.Name()
|
||||
path := fmt.Sprintf("./zones/%s", name)
|
||||
|
||||
if !strings.HasSuffix(strings.ToLower(name), ".toml") {
|
||||
continue
|
||||
}
|
||||
|
||||
var zone Zone
|
||||
_, err = toml.DecodeFile(path, &zone)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(zone.Domain) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if zone.DefaultTTL <= 0 {
|
||||
zone.DefaultTTL = 3600
|
||||
}
|
||||
|
||||
zones = append(zones, zone)
|
||||
}
|
||||
|
||||
return zones, nil
|
||||
}
|
||||
|
||||
func CreateZone(zone Zone) error {
|
||||
if len(zone.Domain) == 0 {
|
||||
return fmt.Errorf("zone name cannot be empty")
|
||||
}
|
||||
|
||||
name := strings.ReplaceAll(strings.ToLower(zone.Domain), ".", "_")
|
||||
path := fmt.Sprintf("./zones/%s.toml", name)
|
||||
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return toml.NewEncoder(file).Encode(&zone)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue