Skip to content

Instantly share code, notes, and snippets.

@chy-causer
Last active December 7, 2017 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chy-causer/b7b66cbe1db623db181ec417d924d83a to your computer and use it in GitHub Desktop.
Save chy-causer/b7b66cbe1db623db181ec417d924d83a to your computer and use it in GitHub Desktop.
Mojolicious::Command::pgshell
package Mojolicious::Command::pgshell;
use Mojo::Base 'Mojolicious::Command';
use DBI;
use 5.020;
has description => 'Use psql shell';
has usage => sub { shift->extract_usage };
sub run {
my $pg = shift->app->pg;
my ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI->parse_dsn($pg->dsn);
my @psql_options = map { "--$_" } split /;/, $driver_dsn =~ s/(?:db|database)=/dbname=/r;
$ENV{PGUSER} = $pg->username;
$ENV{PGPASSWORD} = $pg->password;
exec('psql', @psql_options, @_);
}
1;
=encoding utf8
=head1 NAME
Mojolicious::Command::pgshell - Get a Postgresql shell from your application's pg attribute
=head1 SYNOPSIS
Usage: APPLICATION pgshell [PGOPTIONS]
./myapp.pl pgshell
./myapp.pl pgshell -tA -c 'select now()'
MOJO_MODE=staging ./myapp.pl pgshell -f 'backup.sql'
Options:
See the psql help pages for options
=head1 DESCRIPTION
L<Mojolicious::Command::pgshell> drops you into a psql shell from your application's pg attribute.
=head1 ATTRIBUTES
L<Mojolicious::Command::pgshell> inherits all attributes from
L<Mojolicious::Command> and implements the following new ones.
=head2 description
my $description = $pgshell->description;
$pgshell = $pgshell->description('Foo');
Short description of this command, used for the command list.
=head2 usage
my $usage = $pgshell->usage;
$pgshell = $pgshell->usage('Foo');
Usage information for this command, used for the help screen.
=head1 METHODS
L<Mojolicious::Command::pgshell> inherits all methods from
L<Mojolicious::Command> and implements the following new ones.
=head2 run
$pgshell->run(@ARGV);
Run this command.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment