| 1 | package WWW::Hetzner::CLI::Cmd::Record::Cmd::List; | ||
| 2 | # ABSTRACT: List DNS records | ||
| 3 | |||
| 4 | our $VERSION = '0.100'; | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | use MooX::Cmd; | ||
| 8 | use MooX::Options usage_string => 'USAGE: hcloud.pl record list --zone <zone-id> [--type <type>] [--name <name>]'; | ||
| 9 | use JSON::MaybeXS qw(encode_json); | ||
| 10 | |||
| 11 | option zone => ( | ||
| 12 | is => 'ro', | ||
| 13 | format => 's', | ||
| 14 | short => 'z', | ||
| 15 | required => 1, | ||
| 16 | doc => 'Zone ID', | ||
| 17 | ); | ||
| 18 | |||
| 19 | option type => ( | ||
| 20 | is => 'ro', | ||
| 21 | format => 's', | ||
| 22 | short => 't', | ||
| 23 | doc => 'Filter by record type (A, AAAA, CNAME, MX, TXT, etc.)', | ||
| 24 | ); | ||
| 25 | |||
| 26 | option name => ( | ||
| 27 | is => 'ro', | ||
| 28 | format => 's', | ||
| 29 | short => 'n', | ||
| 30 | doc => 'Filter by record name', | ||
| 31 | ); | ||
| 32 | |||
| 33 | sub execute { | ||
| 34 | my ($self, $args, $chain) = @_; | ||
| 35 | |||
| 36 | my $main = $chain->[0]; | ||
| 37 | my $cloud = $main->cloud; | ||
| 38 | |||
| 39 | my %params; | ||
| 40 | $params{type} = $self->type if $self->type; | ||
| 41 | $params{name} = $self->name if $self->name; | ||
| 42 | |||
| 43 | my $rrsets = $cloud->zones->rrsets($self->zone); | ||
| 44 | my $records = $rrsets->list(%params); | ||
| 45 | |||
| 46 | if ($main->output eq 'json') { | ||
| 47 | print encode_json([map { $_->data } @$records]), "\n"; | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | if (!@$records) { | ||
| 52 | print "No records found.\n"; | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | printf "%-25s %-8s %-8s %s\n", | ||
| 57 | 'NAME', 'TYPE', 'TTL', 'VALUES'; | ||
| 58 | print "-" x 80, "\n"; | ||
| 59 | |||
| 60 | for my $r (@$records) { | ||
| 61 | my $values = join(', ', map { $_->{value} } @{$r->records}); | ||
| 62 | printf "%-25s %-8s %-8s %s\n", | ||
| 63 | $r->name, | ||
| 64 | $r->type, | ||
| 65 | $r->ttl // '-', | ||
| 66 | $values; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; | ||
| 71 | |||
| 72 | __END__ |