| Giorgi Lekveishvili | 1caed36 | 2023-12-13 16:29:43 +0400 | [diff] [blame] | 1 | package welcome |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/libdns/gandi" |
| 11 | "github.com/libdns/libdns" |
| 12 | ) |
| 13 | |
| 14 | type DNSUpdater interface { |
| 15 | Update(zone string, records []string) error |
| 16 | } |
| 17 | |
| 18 | func 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 | |
| 49 | type gandiUpdater struct { |
| 50 | provider libdns.RecordSetter |
| 51 | } |
| 52 | |
| 53 | func NewGandiUpdater(apiToken string) *gandiUpdater { |
| 54 | return &gandiUpdater{ |
| Giorgi Lekveishvili | b59b7c2 | 2024-04-03 22:17:50 +0400 | [diff] [blame^] | 55 | provider: &gandi.Provider{BearerToken: apiToken}, |
| Giorgi Lekveishvili | 1caed36 | 2023-12-13 16:29:43 +0400 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | func (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 | } |