Skip to content

Commit

Permalink
Merge pull request #3 from kazeburo/feature/fzone
Browse files Browse the repository at this point in the history
Add fzone sub command
  • Loading branch information
kazeburo committed Apr 2, 2021
2 parents 6d7c370 + 3f60ee9 commit 110fb69
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ Help Options:
-h, --help Show this help message
Available commands:
fzone find zone for the record
list list zones
radd add a record
rdelete delete a record
rset replace records or add a record
version display version
zone describe zone
```

Expand Down Expand Up @@ -134,3 +136,28 @@ Help Options:
```
./sacloudns rdelete --zone example.com --name _acme.example.com. --type TXT --data xxxxxx --ttl 30
```

## Find zone for a record

Find DNS zone for a record

```
% ./sacloudns fzone -h
Usage:
sacloudns [OPTIONS] fzone [fzone-OPTIONS]
Help Options:
-h, --help Show this help message
[fzone command options]
--name= record name to find zone
```

If zone exmaple.com is exits in the account.

```
./sacloudns fzone foo.bar.baz.example.com
example.com
```


55 changes: 47 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type zoneOpts struct {
Name string `long:"name" description:"dnszone name to find"`
}

type fzoneOpts struct {
Name string `long:"name" description:"record name to find zone"`
}

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"`
Expand Down Expand Up @@ -59,12 +63,13 @@ type rdelOpts struct {
}

type mainOpts struct {
ListCmd listOpts `command:"list" description:"list zones"`
ZoneCmd zoneOpts `command:"zone" description:"describe zone"`
RAddCmd raddOpts `command:"radd" description:"add a record"`
RSetCmd rsetOpts `command:"rset" description:"replace records or add a record"`
RDelCmd rdelOpts `command:"rdelete" description:"delete a record"`
VersionCMD verOpts `command:"version" description:"display version"`
ListCmd listOpts `command:"list" description:"list zones"`
ZoneCmd zoneOpts `command:"zone" description:"describe zone"`
FzoneCmd fzoneOpts `command:"fzone" description:"find zone for the record"`
RAddCmd raddOpts `command:"radd" description:"add a record"`
RSetCmd rsetOpts `command:"rset" description:"replace records or add a record"`
RDelCmd rdelOpts `command:"rdelete" description:"delete a record"`
VersionCMD verOpts `command:"version" description:"display version"`
}

func outJSON(result interface{}) error {
Expand Down Expand Up @@ -233,6 +238,40 @@ func (opts *zoneOpts) Execute(args []string) error {
return outJSON(zone)
}

func searchZoneForRecord(ctx context.Context, record string) (*sacloud.DNS, error) {
testRecord := record
result, err := searchZone(ctx, &sacloud.FindCondition{})
if err != nil {
return nil, err
}
for testRecord != "" {
for _, z := range result.DNS {
if z.DNSZone == testRecord {
return z, nil
}
}
if strings.Contains(testRecord, ".") {
t := strings.SplitN(testRecord, ".", 2)
testRecord = t[1]
continue
}
break
}
return nil, fmt.Errorf("Could not find zone for %s", record)
}

func (opts *fzoneOpts) Execute(args []string) error {
if opts.Name == "" && len(args) > 0 {
opts.Name = args[0]
}
result, err := searchZoneForRecord(context.Background(), opts.Name)
if err != nil {
return err
}
os.Stdout.Write([]byte(result.DNSZone + "\n"))
return nil
}

func (opts *listOpts) Execute(args []string) error {
result, err := searchZone(context.Background(), &sacloud.FindCondition{})
if err != nil {
Expand All @@ -242,7 +281,7 @@ func (opts *listOpts) Execute(args []string) error {
}

func (opts *raddOpts) Execute(args []string) error {
if opts.Wait && opts.Type != "TXT" {
if opts.Wait {
if err := availPropagation(opts.Type); err != nil {
return err
}
Expand Down Expand Up @@ -288,7 +327,7 @@ func (opts *raddOpts) Execute(args []string) error {
}

func (opts *rsetOpts) Execute(args []string) error {
if opts.Wait && opts.Type != "TXT" {
if opts.Wait {
if err := availPropagation(opts.Type); err != nil {
return err
}
Expand Down

0 comments on commit 110fb69

Please sign in to comment.