-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (52 loc) · 1.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"io"
"net/http"
"os"
gandi "github.com/breed808/gandiapi"
)
func getPublicIP() (string, error) {
ipClient := http.Client{}
req, err := http.NewRequest(http.MethodGet, "https://ifconfig.me/ip", nil)
if err != nil {
return "", err
}
resp, err := ipClient.Do(req)
if err != nil {
return "", err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func main() {
fqdn := os.Args[1]
publicIP, err := getPublicIP()
if err != nil {
fmt.Printf("Error acquiring public ip address: %v", err)
os.Exit(1)
}
client := gandi.NewClient(os.Getenv("GANDI_API_KEY"), false, false)
for _, name := range os.Args[2:] {
record := gandi.Record{
RrsetType: "A",
RrsetTTL: 3600,
RrsetName: name,
RrsetHref: "",
RrsetValues: []string{publicIP},
}
_, err = client.UpdateRecord(record, fqdn)
if err != nil {
fmt.Printf("Failed request: %s\n", err)
os.Exit(1)
}
fmt.Printf("Updated record for %s.%s\n", record.RrsetName, fqdn)
}
}