package System::Introspector::Host;
use Moo;

use System::Introspector::Util qw(
    handle_from_command
    output_from_command
    output_from_file
    transform_exceptions
);

sub gather {
    my ($self) = @_;
    return transform_exceptions {
        return {
            hostname => $self->_gather_hostname,
            uname    => $self->_gather_uname_info,
        };
    };
}

my @UnameFields = qw(
    kernel_name
    kernel_release
    kernel_version
    nodename
    machine
    processor
    hardware_platform
    operating_system
);

sub _gather_uname_info {
    my ($self) = @_;
    my %uname;
    for my $field (@UnameFields) {
        (my $option = $field) =~ s{_}{-}g;
        my $value = output_from_command [uname => "--$option"];
        chomp $value;
        $uname{ $field } = $value;
    }
    return \%uname;
}

sub _gather_hostname {
    my ($self) = @_;
    my $hostname = output_from_file '/etc/hostname';
    chomp $hostname;
    $hostname =~ s{(?:^\s+|\s+$)}{}g;
    return $hostname;
}

1;

__END__

=head1 NAME

System::Introspector::Host - Gather generic host information

=head1 DESCRIPTION

Gathers the hostname and information provided by C<uname>.

=head1 SEE ALSO

=over

=item L<System::Introspector>

=back

=cut

