App-Dochazka-CLI

 view release on metacpan or  search on metacpan

bin/dochazka-cli  view on Meta::CPAN

    if ( $noauth ) {
        return "App::Dochazka::CLI> ";
    } else {
        return "Dochazka($prompt_date) " . $current_emp->nick . " " . uc $current_priv . "> ";
    }
}

sub printversion {
    print "App::Dochazka::CLI version $App::Dochazka::CLI::VERSION\n";
    exit(0);
}


# -------------------------------------------------------------------------
# main
# -------------------------------------------------------------------------

# process command-line options
my $help = 0;
my $user = '';
my $password = '';
my $sitedir;
my $early_debug;
GetOptions( 
    'help|?' => \$help, 
    'user|u=s' => \$user, 
    'password|p=s' => \$password,
    'sitedir|s=s' => \$sitedir,
    'noauth|n' => \$noauth,
    'debug|d' => \$debug_mode,
    'early-debug|e=s' => \$early_debug,
    'version|v' => \$versionparam,
);

pod2usage(1) if $help;

print "Debug mode: parser state dumps will be generated\n" if $debug_mode;

printversion() if $versionparam;

# assemble array of sitedirs
if ( $sitedir and not -d $sitedir ) {
    die "sitedir value must be a valid directory";
}
my @sitedirs;
foreach my $target ( '/etc/dochazka-cli', "$home/.dochazka-cli", $sitedir ) {
    if ( defined( $target ) and -d $target ) {
        push @sitedirs, $target;
    }
}

my $interactive = -t STDIN ? 1 : 0;
my $pipe = -p STDIN ? 1 : 0;

# initialize CLI client
my $status = init_cli_client( 
    distro => 'App-Dochazka-CLI',
    sitedir => [ @sitedirs ],
    early_debug => $early_debug,
);
if ( $status->not_ok ) {
    print $status->code . ' (' . $status->level . ') ' . $status->text . "\n";
    print "Response: " . Dumper( $status->payload ) . "\n";
    exit;
}
init_logger();
init_prompt();

# determine server
if ( ! ( $server = $ARGV[0] ) ) {
    if ( $server = $meta->MREST_CLI_URI_BASE ) {
        print "URI base $server set from site configuration\n";
    } else {
        $server = 'http://localhost:5000';
        print "URI base not set; defaulting to $server\n";
    }
}

$meta->set( 'MREST_CLI_URI_BASE', $server );

# authenticate unless --noauth given
if ( ! $noauth ) {
    if ( ! $user ) {
        if ( $user = $site->DOCHAZKA_REST_LOGIN_NICK ) {
            print "Username $user set from site configuration\n";
            if ( $password = $site->DOCHAZKA_REST_LOGIN_PASSWORD ) {
                print "Password set from site configuration\n";
            }
        } else {
            print "Username: ";
            chomp( $user = <STDIN> );
        }
    }
    print "Authenticating to server at $server as user $user\n";
    if ( ! $password ) {
        ReadMode ('noecho');
        print "Password: ";
        chomp( $password = <STDIN> );
        ReadMode ('restore');
        print "\n";
    }
    my $status = authenticate_to_server( user => $user, password => $password );
    if ( $status->not_ok ) {

        # Handle two classic scenarios:
        # 1. server not running
        if ( $status->payload =~ m/Connection refused/ ) {
            print "Server refused connection - is it running?\n";
            exit;
        }
        # 2. authentication failed
        if ( $status->text =~ m/Authentication failed/ ) {
            print "Authentication failed.\n";
            exit;
        }
        # 3. authentication failed
        if ( $status->text =~ m/Internal auth failed/ ) {
            print "Authentication failed.\n";
            exit;
        }

        # some other scenario?
        print $status->code . ' (' . $status->level . ') ' . $status->text . "\n";
        print "Response: " . Dumper( $status->payload ) . "\n";
        exit;
    } else {
        print "Server is alive\n";
    }
}


# paraphrased from https://metacpan.org/source/HAYASHI/Term-ReadLine-Gnu-1.26/eg/fileman
sub initialize_readline {
    my $term = new Term::ReadLine 'dochazka-cli';
    $term->Attribs->{completion_function} = \&dochazka_cli_completion;
    return $term;
}

my $term = initialize_readline() if $interactive;

my $readline = $interactive 
    ? sub { $term->readline( get_prompt() ) } 
    : sub {
        my $line = <STDIN>;
        return unless defined($line);
        print get_prompt() . $line;
        return $line;
    };

binmode STDOUT, ":utf8";

my $cmd;
COMMAND: while ( defined ( $cmd = $readline->() ) ) {

    my $rv = process_command( $cmd );

    # The return value SHOULD be a status object, but tolerate the eventuality
    # that it might be just a plain string

    if ( ref( $rv ) eq 'App::CELL::Status' ) {
        if ( delete $rv->{'rest_test'} ) {
            print "HTTP status: " . ( delete $rv->{'http_status'} || '<NONE>' ) . "\n";
            print "Non-suppressed headers: " . Dumper( $rv->{'headers'} ) if $rv->{'headers'};



( run in 0.854 second using v1.01-cache-2.11-cpan-39bf76dae61 )