Skip to content

Commit ef0efd0

Browse files
committed
Add daemon func
1 parent 6ae220c commit ef0efd0

File tree

6 files changed

+451
-125
lines changed

6 files changed

+451
-125
lines changed

cmd/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ For example:
2929
` + "DOMAIN=mydomain.com RECORD=sub.mydomain.com TOKEN=<api-token> cloudflare-ddns" + `
3030
`,
3131
RunE: func(cmd *cobra.Command, args []string) error {
32+
if viper.GetBool(conf.Daemon) {
33+
return errors.Trace(ddns.DaemonWithDefaults(context.Background()))
34+
}
3235
return errors.Trace(ddns.Run(context.Background()))
3336
},
3437
Version: conf.Version,
@@ -41,6 +44,7 @@ func init() {
4144
Root.PersistentFlags().String(conf.Token, "", "CloudFlare API token with permissions Zone:Zone:Read and Zone:DNS:Edit")
4245
Root.PersistentFlags().Bool(conf.JSONOutput, false, "Log format, either pretty or json, defaults to pretty")
4346
Root.PersistentFlags().BoolP(conf.Verbose, conf.VerboseShort, false, "Verbose logging, prints additional log output")
47+
Root.PersistentFlags().Bool(conf.Daemon, false, "Run as a service, continually monitoring for DNS changes")
4448
Root.SetVersionTemplate("{{.Version}}\n")
4549

4650
viper.BindPFlag(conf.Config, Root.PersistentFlags().Lookup(conf.Config))

conf/conf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var ModuleName string
99
var (
1010
// Config is the path to the config file, if present
1111
Config = "config"
12+
// Daemon is the flag for enabling daemon mode
13+
Daemon = "daemon"
1214
// Domain is the domain to update within CloudFlare
1315
Domain = "domain"
1416
// Record is the DNS record to update within CloudFlare, may be same as Domain or a subdomain

ddns/ddns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func Daemon(ctx context.Context, updatePeriod, failureRetryDelay time.Duration)
5656
log.Info().Msgf(
5757
"No IP change detected since %s (%d seconds ago)",
5858
lastIPUpdate.Format(time.RFC1123Z),
59-
int(time.Now().Sub(lastIPUpdate).Seconds()))
59+
int(time.Since(lastIPUpdate).Seconds()))
6060
time.Sleep(updatePeriod)
6161
continue
6262
}

ddns/ddns_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package ddns

go.mod

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,18 @@ module github.com/mattolenik/cloudflare-ddns-client
33
go 1.16
44

55
require (
6-
github.com/cloudflare/cloudflare-go v0.16.0
7-
github.com/juju/errors v0.0.0-20200330140219-3fe23663418f
6+
github.com/cloudflare/cloudflare-go v0.24.0
7+
github.com/juju/errors v0.0.0-20210818161939-5560c4c073ff
88
github.com/juju/testing v0.0.0-20201030020617-7189b3728523 // indirect
99
github.com/lixiangzhong/dnsutil v1.4.0
10-
github.com/magiconair/properties v1.8.5 // indirect
11-
github.com/miekg/dns v1.1.42 // indirect
12-
github.com/mitchellh/mapstructure v1.4.1 // indirect
13-
github.com/pelletier/go-toml v1.9.0 // indirect
14-
github.com/rs/zerolog v1.21.0
15-
github.com/spf13/afero v1.6.0 // indirect
16-
github.com/spf13/cast v1.3.1 // indirect
17-
github.com/spf13/cobra v1.1.3
18-
github.com/spf13/jwalterweatherman v1.1.0 // indirect
19-
github.com/spf13/viper v1.7.1
10+
github.com/miekg/dns v1.1.43 // indirect
11+
github.com/rs/zerolog v1.25.0
12+
github.com/spf13/cobra v1.2.1
13+
github.com/spf13/viper v1.9.0
2014
github.com/stretchr/testify v1.7.0
21-
golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf // indirect
22-
golang.org/x/net v0.0.0-20210508051633-16afe75a6701 // indirect
23-
golang.org/x/sys v0.0.0-20210507161434-a76c4d0a0096 // indirect
24-
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
25-
gopkg.in/ini.v1 v1.62.0 // indirect
15+
golang.org/x/net v0.0.0-20211005215030-d2e5035098b3 // indirect
16+
golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef // indirect
17+
golang.org/x/text v0.3.7 // indirect
18+
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
2619
gotest.tools/gotestsum v0.6.0
2720
)

0 commit comments

Comments
 (0)