Skip to content

Commit 0f6d414

Browse files
committed
Code cleanup. Now only PGDATA, PGHOST, PGPORT, PGUSER and PGDATABASE can be set from enviromental variables. If values was already set via command line option, it wouldn't be overwritten.
1 parent 612070c commit 0f6d414

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

pg_probackup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ static pgut_option options[] =
8484
{ 's', 'd', "dbname" , &pgut_dbname, SOURCE_CMDLINE },
8585
{ 's', 'h', "host" , &host, SOURCE_CMDLINE },
8686
{ 's', 'p', "port" , &port, SOURCE_CMDLINE },
87-
{ 'b', 'q', "quiet" , &quiet, SOURCE_CMDLINE },
8887
{ 's', 'U', "username" , &username, SOURCE_CMDLINE },
88+
{ 'b', 'q', "quiet" , &quiet, SOURCE_CMDLINE },
8989
{ 'b', 'v', "verbose" , &verbose, SOURCE_CMDLINE },
9090
{ 'B', 'w', "no-password" , &prompt_password, SOURCE_CMDLINE },
9191
{ 0 }

pgut/pgut.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ longopts_to_optstring(const struct option opts[])
505505
return result;
506506
}
507507

508+
/*
509+
* Read options from environmental variables.
510+
* Do not overwrite in option was already set via command line option.
511+
*/
508512
static void
509513
option_from_env(pgut_option options[])
510514
{
@@ -513,24 +517,30 @@ option_from_env(pgut_option options[])
513517
for (i = 0; options && options[i].type; i++)
514518
{
515519
pgut_option *opt = &options[i];
516-
char name[256];
517-
size_t j;
518-
const char *s;
519-
const char *value;
520+
const char *value = NULL;
520521

522+
/* If option was already set do not check env */
521523
if (opt->source > SOURCE_ENV || opt->allowed < SOURCE_ENV)
522524
continue;
523525

524-
for (s = opt->lname, j = 0; *s && j < lengthof(name) - 1; s++, j++)
526+
if (strcmp(opt->lname, "pgdata") == 0)
527+
value = getenv("PGDATA");
528+
if (strcmp(opt->lname, "port") == 0)
529+
value = getenv("PGPORT");
530+
if (strcmp(opt->lname, "host") == 0)
531+
value = getenv("PGHOST");
532+
if (strcmp(opt->lname, "username") == 0)
533+
value = getenv("PGUSER");
534+
if (strcmp(opt->lname, "dbname") == 0)
525535
{
526-
if (strchr("-_ ", *s))
527-
name[j] = '_'; /* - to _ */
528-
else
529-
name[j] = toupper(*s);
536+
value = getenv("PGDATABASE");
537+
if (value == NULL)
538+
value = getenv("PGUSER");
539+
if (value == NULL)
540+
value = get_username();
530541
}
531-
name[j] = '\0';
532542

533-
if ((value = getenv(name)) != NULL)
543+
if (value)
534544
assign_option(opt, value, SOURCE_ENV);
535545
}
536546
}
@@ -564,10 +574,6 @@ pgut_getopt(int argc, char **argv, pgut_option options[])
564574

565575
/* Read environment variables */
566576
option_from_env(options);
567-
(void) (pgut_dbname ||
568-
(pgut_dbname = getenv("PGDATABASE")) ||
569-
(pgut_dbname = getenv("PGUSER")) ||
570-
(pgut_dbname = get_username()));
571577

572578
init_cancel_handler();
573579
atexit(on_cleanup);

0 commit comments

Comments
 (0)