Skip to content

Commit

Permalink
Merge pull request #2 from kazeburo/support-fqdn
Browse files Browse the repository at this point in the history
support fqdn with final dot record name
  • Loading branch information
kazeburo committed Mar 9, 2021
2 parents baf0670 + 0e062ae commit b958c76
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ brews:
homepage: "https://github.com/kazeburo/sacloudns"
install: |
bin.install "sacloudns"
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Help Options:
[radd command options]
--zone= dnszone name to add a record
--ttl= record TTL to add (default: 300)
--name= record NAME to add
--name= record NAME or FQDN(with final dot) to add
--type= record TYPE to add
--data= record DATA to add
--wait wait for record propagation
Expand All @@ -85,7 +85,7 @@ Add an A record
Add a TXT for DNS challenge

```
./sacloudns radd --wait --zone example.com --name _acme --type TXT --data xxxxxx --ttl 30
./sacloudns radd --wait --zone example.com --name _acme.example.com. --type TXT --data xxxxxx --ttl 30
```

with `--wait` option, sacloudns wait for DNS record propergation. `--wait` is only available for TXT and CNAME
Expand All @@ -102,7 +102,7 @@ Help Options:
[rset command options]
--zone= dnszone name to set a record
--ttl= record TTL to set (default: 300)
--name= record NAME to set
--name= record NAME or FQDN(with final dot) to set
--type= record TYPE to set
--data= record DATA to set
--wait wait for record propagation
Expand All @@ -121,13 +121,16 @@ Help Options:
-h, --help Show this help message
[rdelete command options]
--zone= dnszone name to set a record
--name= record NAME to set
--type= record TYPE to set
--data= record DATA to set
--zone= dnszone name to delete a record
--name= record NAME or FQDN(with final dot) to delete
--type= record TYPE to delete
--data= record DATA to delete
```

```
./sacloudns rdelete --zone example.com --name test --type A --data 192.168.0.1
```

```
./sacloudns rdelete --zone example.com --name _acme.example.com. --type TXT --data xxxxxx --ttl 30
```
22 changes: 16 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type zoneOpts struct {
type raddOpts struct {
Zone string `long:"zone" description:"dnszone name to add a record"`
TTL int `long:"ttl" description:"record TTL to add" default:"300"`
Name string `long:"name" description:"record NAME to add" required:"true"`
Name string `long:"name" description:"record NAME or FQDN(with final dot) to add" required:"true"`
Type string `long:"type" description:"record TYPE to add" required:"true"`
RData string `long:"data" description:"record DATA to add" required:"true"`
Wait bool `long:"wait" description:"wait for record propagation"`
Expand All @@ -44,18 +44,18 @@ type raddOpts struct {
type rsetOpts struct {
Zone string `long:"zone" description:"dnszone name to set a record"`
TTL int `long:"ttl" description:"record TTL to set" default:"300"`
Name string `long:"name" description:"record NAME to set" required:"true"`
Name string `long:"name" description:"record NAME or FQDN(with final dot) to set" required:"true"`
Type string `long:"type" description:"record TYPE to set" required:"true"`
RData string `long:"data" description:"record DATA to set" required:"true"`
Wait bool `long:"wait" description:"wait for record propagation"`
WaitTimeout time.Duration `long:"wait-timeout" description:"wait timeout for record propagation" default:"60s"`
}

type rdelOpts struct {
Zone string `long:"zone" description:"dnszone name to set a record"`
Name string `long:"name" description:"record NAME to set" required:"true"`
Type string `long:"type" description:"record TYPE to set" required:"true"`
RData string `long:"data" description:"record DATA to set" required:"true"`
Zone string `long:"zone" description:"dnszone name to delete a record"`
Name string `long:"name" description:"record NAME or FQDN(with final dot) to delete" required:"true"`
Type string `long:"type" description:"record TYPE to delete" required:"true"`
RData string `long:"data" description:"record DATA to delete" required:"true"`
}

type mainOpts struct {
Expand Down Expand Up @@ -215,6 +215,13 @@ func fetchZone(ctx context.Context, name string) (*sacloud.DNS, error) {
return result.DNS[0], nil
}

func rewriteName(Name, Zone string) string {
if !strings.HasSuffix(Name, ".") {
return Name
}
return strings.TrimSuffix(Name, "."+Zone+".")
}

func (opts *zoneOpts) Execute(args []string) error {
if opts.Name == "" && len(args) > 0 {
opts.Name = args[0]
Expand Down Expand Up @@ -244,6 +251,7 @@ func (opts *raddOpts) Execute(args []string) error {
if err != nil {
return err
}
opts.Name = rewriteName(opts.Name, opts.Zone)
new := sacloud.NewDNSRecord(
types.EDNSRecordType(opts.Type),
opts.Name,
Expand Down Expand Up @@ -289,6 +297,7 @@ func (opts *rsetOpts) Execute(args []string) error {
if err != nil {
return err
}
opts.Name = rewriteName(opts.Name, opts.Zone)
new := sacloud.NewDNSRecord(
types.EDNSRecordType(opts.Type),
opts.Name,
Expand Down Expand Up @@ -330,6 +339,7 @@ func (opts *rdelOpts) Execute(args []string) error {
if err != nil {
return err
}
opts.Name = rewriteName(opts.Name, opts.Zone)
delete := sacloud.NewDNSRecord(
types.EDNSRecordType(opts.Type),
opts.Name,
Expand Down

0 comments on commit b958c76

Please sign in to comment.