Skip to content

Commit

Permalink
feat: add --always-notify flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mlcdf committed Dec 21, 2021
1 parent ad10d7a commit b6fe474
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions internal/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func init() {
}
}

func (c *Client) PostInfo(webhook *Webhook) error {
webhook.Embeds[0].Color = 2201331
return c.Post(webhook)
}

func (c *Client) PostError(webhook *Webhook) error {
webhook.Embeds[0].Color = 15092300
return c.Post(webhook)
Expand Down
17 changes: 15 additions & 2 deletions internal/dyndns/dyndns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ type DynDNS struct {
finderFunc ipfinder.IpFinderFunc
gandiClient *gandi.Client
discordClient *discord.Client
alwaysNotify bool
}

// New creates a new DynDNS
func New(ipf ipfinder.IpFinderFunc, g *gandi.Client, d *discord.Client) *DynDNS {
return &DynDNS{finderFunc: ipf, gandiClient: g, discordClient: d}
func New(ipf ipfinder.IpFinderFunc, g *gandi.Client, d *discord.Client, alwaysNotify bool) *DynDNS {
return &DynDNS{finderFunc: ipf, gandiClient: g, discordClient: d, alwaysNotify: alwaysNotify}
}

// Run check the current IPs, and the one defines in the DNS records.
Expand All @@ -41,6 +42,18 @@ func (dyndns *DynDNS) Run(domain string, record string, ttl int) error {

if len(ipsToUpdate) == 0 {
log.Println("IP address(es) match - no further action")

if dyndns.alwaysNotify {
err := dyndns.discordClient.PostInfo(&discord.Webhook{
Embeds: []discord.Embed{
{
Title: fmt.Sprintf("IP address(es) match for record %s.%s - no further action", record, domain),
Description: "To disable notifications when nothing happens, remove the `--always-notify` flag",
},
},
})
return errors.Wrap(err, "failed to send message to discord")
}
return nil
}

Expand Down
16 changes: 10 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const usage = `Usage:
Options:
--livebox Query the Livebox (router) to find the IP instead of api.ipify.org
--ttl Time to live in seconds. Defaults to 3600
--always-notify Always notify the Discord channel (even when nothing changes)
-V, --version Print version
Examples:
Expand All @@ -41,11 +42,12 @@ func main() {
}

var (
versionFlag bool
domainFlag string
recordFlag string
ttlFlag int = 3600
liveboxFlag bool
versionFlag bool
domainFlag string
recordFlag string
ttlFlag int = 3600
liveboxFlag bool
alwaysNotifyFlag bool
)

flag.StringVar(&domainFlag, "domain", domainFlag, "")
Expand All @@ -57,6 +59,8 @@ func main() {
flag.BoolVar(&versionFlag, "version", versionFlag, "print the version")
flag.BoolVar(&versionFlag, "V", versionFlag, "print the version")

flag.BoolVar(&alwaysNotifyFlag, "always-notify", alwaysNotifyFlag, "")

flag.Parse()

if versionFlag {
Expand All @@ -83,7 +87,7 @@ func main() {
}

gandiClient := gandi.New(mustEnv("GANDI_TOKEN"))
dyn := dyndns.New(ipf, gandiClient, discordClient)
dyn := dyndns.New(ipf, gandiClient, discordClient, alwaysNotifyFlag)

err := dyn.Run(domainFlag, recordFlag, ttlFlag)
if err != nil {
Expand Down

0 comments on commit b6fe474

Please sign in to comment.