-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (135 loc) · 3.98 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"errors"
"fmt"
"os"
"slices"
"strings"
"time"
"github.com/transip/gotransip/v6"
"github.com/transip/gotransip/v6/domain"
"github.com/urfave/cli/v2"
)
var version = "dev"
const (
AccountNameEnvVar = "TRANSIP_ACCOUNT_NAME"
AccountNameFlagFull = "account"
AccountNameFlagShort = "a"
DnsEntryFlagFull = "entry"
DnsEntryFlagShort = "e"
DnsEntryTypeFlagFull = "type"
DnsEntryTypeFlagShort = "t"
DomainNameFlagFull = "domain"
DomainNameFlagShort = "d"
DnsEntryTTLFlagFull = "ttl"
PrivateKeyPathEnvVar = "TRANSIP_PRIVATE_KEY"
PrivateKeyPathFlagFull = "private-key"
PrivateKeyPathFlagShort = "k"
DnsEntryTypeA = "A"
DnsEntryTypeAAAA = "AAAA"
DefaultDnsEntryTTL = 1 * time.Hour
)
var (
supportedDnsEntryTypes = []string{DnsEntryTypeA, DnsEntryTypeAAAA}
getIPAddressforDnsEntryType = map[string]GetIPAddress{
DnsEntryTypeA: GetIPv4,
DnsEntryTypeAAAA: GetIPv6,
}
)
func main() {
var accountName string
var privateKeyPath string
var domainName string
var dnsEntries cli.StringSlice
var dnsEntryTypes cli.StringSlice
var dnsEntryTTL time.Duration
app := &cli.App{
Usage: "Automatically update DNS entries in your TransIP domain with your current public IP address.",
Version: version,
HideHelpCommand: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: AccountNameFlagFull,
Aliases: []string{AccountNameFlagShort},
Usage: "TransIP account name",
Required: true,
EnvVars: []string{AccountNameEnvVar},
Destination: &accountName,
},
&cli.StringFlag{
Name: PrivateKeyPathFlagFull,
Aliases: []string{PrivateKeyPathFlagShort},
Usage: "path to TransIP API private key file",
Required: true,
TakesFile: true,
EnvVars: []string{PrivateKeyPathEnvVar},
Destination: &privateKeyPath,
},
&cli.StringFlag{
Name: DomainNameFlagFull,
Aliases: []string{DomainNameFlagShort},
Usage: "domain name for which DNS entries should be synchronized",
Required: true,
Destination: &domainName,
},
&cli.StringSliceFlag{
Name: DnsEntryFlagFull,
Aliases: []string{DnsEntryFlagShort},
Usage: "one or more DNS entries to synchronize",
Required: true,
Destination: &dnsEntries,
},
&cli.StringSliceFlag{
Name: DnsEntryTypeFlagFull,
Aliases: []string{DnsEntryTypeFlagShort},
Usage: fmt.Sprintf("one or more DNS entry types to synchronize (options: %s)", strings.Join(supportedDnsEntryTypes, ", ")),
Required: true,
Action: func(ctx *cli.Context, s []string) error {
for _, v := range s {
if !slices.Contains(supportedDnsEntryTypes, v) {
return fmt.Errorf("unsupported DNS entry type: %s", v)
}
}
return nil
},
Destination: &dnsEntryTypes,
},
&cli.DurationFlag{
Name: DnsEntryTTLFlagFull,
Usage: "Time To Live (TTL) for newly created DNS entries",
Value: DefaultDnsEntryTTL,
Action: func(ctx *cli.Context, i time.Duration) error {
if i <= 0 {
return fmt.Errorf("invalid DNS entry TTL value: %d", i)
}
return nil
},
Destination: &dnsEntryTTL,
},
},
Action: func(ctx *cli.Context) error {
client, err := gotransip.NewClient(gotransip.ClientConfiguration{
AccountName: accountName,
PrivateKeyPath: privateKeyPath,
})
if err != nil {
return err
}
updater := &Updater{
DomainRepository: &domain.Repository{Client: client},
StdOut: ctx.App.Writer,
}
errs := make([]error, 0)
for dnsEntryType, getIPAddress := range getIPAddressforDnsEntryType {
if slices.Contains(dnsEntryTypes.Value(), dnsEntryType) {
errs = append(errs, updater.UpdateDNSEntries(domainName, dnsEntries.Value(), dnsEntryTTL, dnsEntryType, getIPAddress))
}
}
return errors.Join(errs...)
},
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}