-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
120 lines (92 loc) · 2.64 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"go.mlcdf.fr/sally/build"
)
const usage = `Usage:
dyndns --domain [DOMAIN] --record [RECORD]
Options:
--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:
export DISCORD_WEBHOOK_URL='https://discord.com/api/webhooks/xxx'
export GANDI_TOKEN='foobar'
dyndns --domain example.com --record "*.pi"
How to create a Discord webhook: https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks
How to generate your Gandi token: https://docs.gandi.net/en/domain_names/advanced_users/api.html
`
type exitCode int
const (
exitOK exitCode = 0
exitError exitCode = 1
)
var (
// Injected from linker flags like `go build -ldflags "-X main.version=$VERSION" -X ...`
isTest = "false"
)
func main() {
code := int(mainRun())
os.Exit(code)
}
func mainRun() exitCode {
log.SetFlags(0)
flag.Usage = func() { fmt.Fprint(os.Stderr, usage) }
if len(os.Args) == 1 {
flag.Usage()
return exitOK
}
var (
versionFlag bool
domainFlag string
recordFlag string
ttlFlag int = 3600
alwaysNotifyFlag bool
)
flag.StringVar(&domainFlag, "domain", domainFlag, "")
flag.StringVar(&recordFlag, "record", recordFlag, "")
flag.IntVar(&ttlFlag, "ttl", ttlFlag, "Time to live. Defaults to 3600.")
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 {
fmt.Fprintln(os.Stdout, "dyndns "+build.String())
return exitOK
}
webhook := os.Getenv("DISCORD_WEBHOOK_URL")
if webhook == "" {
log.Println("error: required environment variable DISCORD_WEBHOOK_URL is empty or missing")
return exitError
}
discordClient := &discordClient{webhook}
logErr := log.New(io.MultiWriter(os.Stderr, discordClient), "", 0)
if domainFlag == "" {
logErr.Println("error: required flag --domain is missing")
return exitError
}
if recordFlag == "" {
logErr.Println("error: required flag --record is missing")
return exitError
}
token := os.Getenv("GANDI_TOKEN")
if token == "" {
log.Println("error: required environment variable GANDI_TOKEN is empty or missing")
return exitError
}
gandiClient := &gandiClient{token}
dyn := &DynDNS{
gandiClient,
discordClient,
}
err := dyn.execute(domainFlag, recordFlag, ttlFlag, alwaysNotifyFlag)
if err != nil {
logErr.Printf("error: %v", err)
return exitError
}
return exitOK
}