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