| 1 | package WWW::Hetzner::CLI::Cmd::Server; | ||
| 2 | # ABSTRACT: Server commands | ||
| 3 | |||
| 4 | our $VERSION = '0.100'; | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | use MooX::Cmd; | ||
| 8 | use MooX::Options usage_string => 'USAGE: hcloud.pl server <command> [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 selector => ( | ||
| 20 | is => 'ro', | ||
| 21 | format => 's', | ||
| 22 | short => 'l', | ||
| 23 | doc => 'Label selector (e.g., env=prod)', | ||
| 24 | ); | ||
| 25 | |||
| 26 | option name => ( | ||
| 27 | is => 'ro', | ||
| 28 | format => 's', | ||
| 29 | short => 'n', | ||
| 30 | doc => 'Filter by name', | ||
| 31 | ); | ||
| 32 | |||
| 33 | sub _list { | ||
| 34 | my ($self, $chain) = @_; | ||
| 35 | |||
| 36 | my $main = $chain->[0]; | ||
| 37 | my $cloud = $main->cloud; | ||
| 38 | |||
| 39 | my %params; | ||
| 40 | $params{label_selector} = $self->selector if $self->selector; | ||
| 41 | $params{name} = $self->name if $self->name; | ||
| 42 | |||
| 43 | my $servers = $cloud->servers->list(%params); | ||
| 44 | |||
| 45 | if ($main->output eq 'json') { | ||
| 46 | print encode_json($servers), "\n"; | ||
| 47 | return; | ||
| 48 | } | ||
| 49 | |||
| 50 | # Table output | ||
| 51 | if (!@$servers) { | ||
| 52 | print "No servers found.\n"; | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | printf "%-10s %-25s %-12s %-16s %-10s %s\n", | ||
| 57 | 'ID', 'NAME', 'STATUS', 'IPV4', 'TYPE', 'DATACENTER'; | ||
| 58 | print "-" x 90, "\n"; | ||
| 59 | |||
| 60 | for my $s (@$servers) { | ||
| 61 | printf "%-10s %-25s %-12s %-16s %-10s %s\n", | ||
| 62 | $s->{id}, | ||
| 63 | $s->{name}, | ||
| 64 | $s->{status}, | ||
| 65 | $s->{public_net}{ipv4}{ip} // '-', | ||
| 66 | $s->{server_type}{name} // '-', | ||
| 67 | $s->{datacenter}{name} // '-'; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | 1. | ||
| 73 | |||
| 74 | __END__ |