Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds --my-ini-file <file> option to read connection info from mysql INI config file #380

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ Type `php srdb.cli.php` to run the program. Type `php srdb.cli.php
Optional. Port on database server to connect to. The default is
3306. (MySQL default port).

--my-ini-file
Reads MySQL configuration ini file provided as argument to
extract connection parameters (host, user, password, database)
from the [client] section. Will not override --host, --name,
--user ou --password if provided as CLI argument.

-s, --search
String to search for or `preg_replace()` style regular
expression.
Expand Down
37 changes: 37 additions & 0 deletions srdb.cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
[ 'u:', 'user:', 'Required. Database user.', ],
[ 'p:', 'pass:', 'Database user\'s password.', ],
[ 'P:', 'port:', 'Optional. Port on database server to connect to. The default is 3306. (MySQL default port).', ],
[ '', 'my-ini-file:', 'Reads MySQL configuration ini file provided as argument to extract connection parameters (host, user, password, database) from the [client] section. Will not override --host, --name, --user ou --password if provided as CLI argument.'],
[ 's:', 'search:', 'String to search for or `preg_replace()` style regular expression.', ],
[ 'r:', 'replace:', 'None empty string to replace search with or `preg_replace()` style replacement.', ],
[ 't:', 'tables:', 'If set only runs the script on the specified table, comma separate for multiple values.', ],
Expand Down Expand Up @@ -163,6 +164,42 @@ function strip_colons( $string ) {
exit ( 1 );
}

// if --my-ini-file used, populate missing connection info from provided INI file
if ( isset( $options['my-ini-file'] ) ) {

if ( ! file_exists( $options['my-ini-file'] ) ) {
fwrite( STDERR, "Error: --my-ini-file: file '{$options['my-ini-file']}' not found.\n" );
exit ( 1 );
}

$my_ini_conf = parse_ini_file( $options['my-ini-file'], true );

if ( $my_ini_conf === FALSE ) {
fwrite( STDERR, "Error: --my-ini-file: Unable to parse '{$options['my-ini-file']}'.\n" );
exit ( 1 );
}

if ( ! array_key_exists( 'client', $my_ini_conf ) ) {
fwrite( STDERR, "Error: --my-ini-file: '{$options['my-ini-file']}' is missing [client] section.\n" );
exit ( 1 );
}

$my_ini_conf = $my_ini_conf['client'];

foreach ( [
'user' => ['user', 'u'],
'password' => ['pass', 'p'],
'database' => ['name', 'n'],
'host' => ['host', 'h']
] as $key => $optnames ) {

if ( ! isset( $options[$optnames[0]] ) && ! isset( $options[$optnames[1]] ) && isset( $my_ini_conf[$key] ) ) {
$options[$optnames[0]] = $my_ini_conf[$key];
}

}
}

// check required args are passed
foreach ( $required as $short_opt => $long_opt ) {

Expand Down