| 1 | package Mojolicious::Command::db_env; | ||
| 2 | use Mojo::Base 'Mojolicious::Command'; | ||
| 3 | |||
| 4 | |||
| 5 | has description => 'Shell command to generate environment variables to access database'; | ||
| 6 | has usage => sub { shift->extract_usage }; | ||
| 7 | |||
| 8 | |||
| 9 | use C; | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | sub run { | ||
| 14 | my( $self, @args ) = @_; | ||
| 15 | |||
| 16 | my $DB = C::config->{ DbAccess }; | ||
| 17 | $DB->{ DSN } = sprintf "dbi:%s:dbname=%s;host=%s;port=%s", | ||
| 18 | @$DB{ qw/ DRVR NAME HOST PORT / }; | ||
| 19 | |||
| 20 | print "DB_$_=$DB->{ $_ }\n" for sort keys %$DB; | ||
| 21 | } | ||
| 22 | |||
| 23 | 1; | ||
| 24 | |||
| 25 | |||
| 26 | =encoding utf8 | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Mojolicious::Command::db_env - Print database config as environment variables | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | Usage: APPLICATION db_env [OPTIONS] | ||
| 35 | |||
| 36 | Options: | ||
| 37 | NONE | ||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | This command reads C<C::config-E<gt>{DbAccess}> and prints one line per config | ||
| 42 | key: | ||
| 43 | |||
| 44 | DB_KEY=value | ||
| 45 | |||
| 46 | This is convenient for shell usage. For example, in a POSIX shell: | ||
| 47 | |||
| 48 | eval "$(APPLICATION db_env)" | ||
| 49 | |||
| 50 | Note: this prints all keys, including passwords. | ||
| 51 | |||
| 52 | =head1 OPTIONS | ||
| 53 | |||
| 54 | None. | ||
| 55 | |||
| 56 | =head1 METHODS | ||
| 57 | |||
| 58 | =head2 run | ||
| 59 | |||
| 60 | Implements the command. | ||
| 61 | |||
| 62 | =head1 SEE ALSO | ||
| 63 | |||
| 64 | L<Mojolicious::Plugin::DbAccess>, L<D> | ||
| 65 | |||
| 66 | =cut |