5 Commits

3 changed files with 40 additions and 12 deletions

View File

@ -2,19 +2,26 @@ CLI tool to manage cloudflare DNS records
## Installation ## Installation
### Using go ### Using [go](https://go.dev)
```bash ```bash
go install go.balki.me/cloudflare-dns-cli@latest go install go.balki.me/cloudflare-dns-cli@latest
# Run directly without installing
go run go.balki.me/cloudflare-dns-cli@latest -h
``` ```
### Using docker ### Using [docker](https://www.docker.com)
```bash ```bash
mkdir -p $HOME/bin mkdir -p $HOME/bin
docker run --pull=always --rm -v "$HOME/bin:/op" golang sh -c "go install go.balki.me/cloudflare-dns-cli@latest && install -o $(id -u) -g $(id -g) /go/bin/cloudflare-dns-cli /op/" docker run --pull=always --rm -v "$HOME/bin:/op" golang sh -c "go install go.balki.me/cloudflare-dns-cli@latest && install -o $(id -u) -g $(id -g) /go/bin/cloudflare-dns-cli /op/"
# Run directly without installing
docker run --pull=always --rm golang go run go.balki.me/cloudflare-dns-cli@latest -h
``` ```
## Features ## Features
1. Add/Modify `A`, `AAAA`, `CNAME` records 1. Add/Modify `A`, `AAAA`, `CNAME`, `MX` records
2. List/Delete records 2. List/Delete records
3. Save all records in JSON format 3. Save all records in JSON format
@ -30,6 +37,7 @@ Usage of cloudflare-dns-cli:
Domain name, e.g. example.com. env var: DOMAIN Domain name, e.g. example.com. env var: DOMAIN
-i value -i value
IP address (default 127.0.0.1) IP address (default 127.0.0.1)
-m Set mx record with cname value
-o string -o string
Path to save all records as json, e.g. ./records.json Path to save all records as json, e.g. ./records.json
-s string -s string

2
go.mod
View File

@ -1,6 +1,6 @@
module go.balki.me/cloudflare-dns-cli module go.balki.me/cloudflare-dns-cli
go 1.20 go 1.21
require ( require (
github.com/libdns/cloudflare v0.1.0 github.com/libdns/cloudflare v0.1.0

30
main.go
View File

@ -23,6 +23,7 @@ var (
sub = "<UNSET>" sub = "<UNSET>"
path string path string
del = false del = false
mx = false
) )
func genRecord() (libdns.Record, error) { func genRecord() (libdns.Record, error) {
@ -30,11 +31,19 @@ func genRecord() (libdns.Record, error) {
ipv6 := ip.To16() ipv6 := ip.To16()
switch { switch {
case cname != "": case cname != "":
return libdns.Record{ if mx {
Type: "CNAME", return libdns.Record{
Name: sub, Type: "MX",
Value: cname, Name: sub,
}, nil Value: cname,
}, nil
} else {
return libdns.Record{
Type: "CNAME",
Name: sub,
Value: cname,
}, nil
}
case ipv4 != nil: case ipv4 != nil:
return libdns.Record{ return libdns.Record{
Type: "A", Type: "A",
@ -101,8 +110,19 @@ func main() {
flag.StringVar(&cname, "c", cname, "CNAME target") flag.StringVar(&cname, "c", cname, "CNAME target")
flag.StringVar(&path, "o", path, "Path to save all records as json, e.g. ./records.json") flag.StringVar(&path, "o", path, "Path to save all records as json, e.g. ./records.json")
flag.BoolVar(&del, "x", del, "Delete records of subdomain") flag.BoolVar(&del, "x", del, "Delete records of subdomain")
flag.BoolVar(&mx, "m", mx, "Set mx record with cname value")
flag.Parse() flag.Parse()
if token == "" {
flag.Usage()
log.Panicln("empty cloudflare api token")
}
if domain == "" {
flag.Usage()
log.Panicln("empty domain name")
}
provider := cloudflare.Provider{APIToken: token} provider := cloudflare.Provider{APIToken: token}
zone := domain + "." zone := domain + "."