package Common::RunCommand;

use strict;
use warnings;
use open ':std', ':encoding(UTF-8)';

use Capture::Tiny(qw/capture/);

use lib '/app/tools/common/lib';
use Common::Session;

our @ISA       = qw(Exporter);
our @EXPORT    = qw/execute/;
our @EXPORT_OK = qw/execute/;

sub execute {
    my $command = shift;
    my $nostdout = shift || 0;
    logMessage( 'info2', $command ) if not $nostdout;
    [ capture { system($command ) } ];
}

sub execute_die {
    my $command = shift;
    my $nostdout = shift || 0;
    my $output  = Common::RunCommand::execute( $command, $nostdout );
    if ( $output->[2] ) {
        logMessage( 'fatal', $output->[1] );
        exit $output->[2];
    }
    $output;
}

sub execute_log {
    my $command = shift;
    my $nostdout = shift || 0;
    my $output  = Common::RunCommand::execute( $command, $nostdout );
    if ( $output->[0] ) {
        logMessage( 'info2', $output->[0] );
    }
    $output;
}

sub execute_die_log {
    my $nostdout = shift || 0;
    my $command = shift;
    my $output  = Common::RunCommand::execute_log( $command, $nostdout );
    if ( $output->[2] ) {
        logMessage( 'fatal', $output->[1] );
        exit $output->[2];
    }
    $output;
}

1;
