blob: 0593937354120d55c6fe0173f79310845f18e23d [file] [log] [blame]
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +04001package welcome
2
3import (
4 "context"
5 "fmt"
6 "io"
7 "strings"
8 "time"
9
10 "github.com/libdns/gandi"
11 "github.com/libdns/libdns"
12)
13
14type DNSUpdater interface {
15 Update(zone string, records []string) error
16}
17
18func ParseRecords(zone string, records []string) ([]libdns.Record, error) {
19 var rrs []libdns.Record
20 for _, r := range records {
21 if r == "" {
22 continue
23 }
24 fmt.Println(r)
25 var name string
26 var ttl time.Duration
27 var tmp string
28 var t string
29 l := strings.NewReader(r)
30 if _, err := fmt.Fscanf(l, "%s %d %s %s", &name, &ttl, &tmp, &t); err != nil {
31 return nil, err
32 }
33 var value strings.Builder
34 if _, err := io.Copy(&value, l); err != nil {
35 return nil, err
36 }
37 val := strings.TrimSpace(value.String())
38 fmt.Printf("%s -- %d -- %s -- %s\n", name, ttl, t, val)
39 rrs = append(rrs, libdns.Record{
40 Type: t,
41 Name: libdns.RelativeName(name, zone),
42 Value: val,
43 TTL: ttl * time.Second,
44 })
45 }
46 return rrs, nil
47}
48
49type gandiUpdater struct {
50 provider libdns.RecordSetter
51}
52
53func NewGandiUpdater(apiToken string) *gandiUpdater {
54 return &gandiUpdater{
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +040055 provider: &gandi.Provider{BearerToken: apiToken},
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +040056 }
57}
58
59func (u *gandiUpdater) Update(zone string, records []string) error {
60 if rrs, err := ParseRecords(zone, records); err != nil {
61 return err
62 } else {
63 fmt.Printf("%+v\n", rrs)
64 _, err := u.provider.SetRecords(context.TODO(), zone, rrs)
65 return err
66 }
67}