@@ -12,7 +12,7 @@ import (
12
12
"time"
13
13
14
14
"github.com/bugsnag/bugsnag-go/v2"
15
- "github.com/cenkalti/backoff/v4 "
15
+ "github.com/cenkalti/backoff/v5 "
16
16
"github.com/joho/godotenv"
17
17
"github.com/pkg/errors"
18
18
)
@@ -92,14 +92,17 @@ func main() {
92
92
}
93
93
94
94
func getIPAddressWithRetry (ctx context.Context , client * http.Client ) (string , error ) {
95
- ip , err := backoff .RetryNotifyWithData (
96
- func () (string , error ) {
97
- return getIPAddress (ctx , client )
98
- },
99
- backoff .NewExponentialBackOff (),
100
- func (err error , d time.Duration ) {
101
- log .Printf ("Problem getting IP address: %s, retrying in %s\n " , err , d )
102
- },
95
+ operation := func () (string , error ) {
96
+ return getIPAddress (ctx , client )
97
+ }
98
+ notify := func (err error , d time.Duration ) {
99
+ log .Printf ("Problem getting IP address: %s, retrying in %s\n " , err , d )
100
+ }
101
+ ip , err := backoff .Retry (
102
+ ctx ,
103
+ operation ,
104
+ backoff .WithBackOff (backoff .NewExponentialBackOff ()),
105
+ backoff .WithNotify (notify ),
103
106
)
104
107
if err != nil {
105
108
return "" , errors .Wrapf (err , "Unable to get IP address, retries exhausted" )
@@ -130,45 +133,49 @@ func getIPAddress(ctx context.Context, client *http.Client) (string, error) {
130
133
}
131
134
132
135
func setDyndnsIPAddressWithRetry (ctx context.Context , client * http.Client , r DynDNSRequest ) error {
133
- err := backoff .RetryNotify (
134
- func () error {
135
- return setDyndnsIPAddress (ctx , client , r )
136
- },
137
- backoff .NewExponentialBackOff (),
138
- func (err error , d time.Duration ) {
139
- log .Printf ("Unable to set dyndns ip for domain %s: %s, retrying in %s\n " , r .DomainName , err , d )
140
- },
136
+ operation := func () (string , error ) {
137
+ return setDyndnsIPAddress (ctx , client , r )
138
+ }
139
+ notify := func (err error , d time.Duration ) {
140
+ log .Printf ("Unable to set dyndns ip for domain %s: %s, retrying in %s\n " , r .DomainName , err , d )
141
+ }
142
+
143
+ result , err := backoff .Retry (
144
+ ctx ,
145
+ operation ,
146
+ backoff .WithBackOff (backoff .NewExponentialBackOff ()),
147
+ backoff .WithNotify (notify ),
141
148
)
142
149
if err != nil {
143
150
return errors .Wrapf (err , "Unable to set dyndns ip for domain %s, retries exhausted" , r .DomainName )
144
151
}
152
+ log .Printf ("Set dyndns ip for domain %s: %s\n " , r .DomainName , result )
145
153
return nil
146
154
}
147
155
148
- func setDyndnsIPAddress (ctx context.Context , client * http.Client , r DynDNSRequest ) error {
156
+ func setDyndnsIPAddress (ctx context.Context , client * http.Client , r DynDNSRequest ) ( string , error ) {
149
157
url := fmt .Sprintf ("https://www.ovh.com/nic/update?system=dyndns&hostname=%s&myip=%s" , r .DomainName , r .IPAddress )
150
158
req , err := http .NewRequestWithContext (ctx , "GET" , url , nil )
151
159
if err != nil {
152
- return errors .Wrapf (err , "Unable to create request to set IP Address for domain: %s" , r .DomainName )
160
+ return "" , errors .Wrapf (err , "Unable to create request to set IP Address for domain: %s" , r .DomainName )
153
161
}
154
162
req .SetBasicAuth (r .Username , r .Password )
155
163
156
164
resp , err := client .Do (req )
157
165
if err != nil {
158
- return errors .Wrapf (err , "Unable to set IP Address for domain: %s" , r .DomainName )
166
+ return "" , errors .Wrapf (err , "Unable to set IP Address for domain: %s" , r .DomainName )
159
167
}
160
168
defer resp .Body .Close ()
161
169
162
170
if resp .StatusCode != 200 {
163
- return fmt .Errorf ("Unable to set IP Address for domain, got code: %d" , resp .StatusCode )
171
+ return "" , fmt .Errorf ("Unable to set IP Address for domain, got code: %d" , resp .StatusCode )
164
172
}
165
173
166
174
body , err := io .ReadAll (resp .Body )
167
175
if err != nil {
168
- return errors .Wrapf (err , "Unable to read response body for domain: %s" , r .DomainName )
176
+ return "" , errors .Wrapf (err , "Unable to read response body for domain: %s" , r .DomainName )
169
177
}
170
- log .Println (string (body ))
171
- return nil
178
+ return string (body ), nil
172
179
}
173
180
174
181
func getDomains () ([]string , bool ) {
0 commit comments