#! /usr/bin/env perl
# vim: set filetype=perl ts=4 sw=4 sts=4 et:
use common::sense;

use File::Basename;
use lib dirname(__FILE__) . '/../../../lib/perl';
use OVH::Result;
use OVH::Bastion;
use OVH::Bastion::Plugin qw( :DEFAULT help );

my $remainingOptions = OVH::Bastion::Plugin::begin(
    argv     => \@ARGV,
    header   => "Here are the public keys that allow you to connect to the bastion",
    helptext => <<'EOF',
List the public ingress keys of your account

Usage: --osh SCRIPT_NAME

The keys listed are the public ingress SSH keys tied to your account.
Their private counterpart should be detained only by you, and used
to authenticate yourself to this bastion.
EOF
);

my $fnret;

$fnret = OVH::Bastion::get_authorized_keys_from_file(file => $HOME . '/' . OVH::Bastion::AK_FILE, includeInvalid => 1);
$fnret or osh_exit $fnret;

my @result;
foreach my $key (@{$fnret->value || []}) {
    OVH::Bastion::print_public_key(key => $key, id => $key->{'index'}, err => $key->{'err'});
    $key->{'validity'}  = delete $key->{'err'};
    $key->{'id'}        = delete $key->{'index'};
    $key->{'from_list'} = delete $key->{'fromList'};
    push @result, $key;
}
if (!@result) {
    osh_info "No ingress keys configured for your account!";
}

osh_ok({keys => \@result, account => $self});
