Skip to content

Commit 0f59db3

Browse files
committed
Code cleanup. Parse subcmd before options
1 parent f35d0d7 commit 0f59db3

File tree

4 files changed

+85
-115
lines changed

4 files changed

+85
-115
lines changed

pg_probackup.c

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ static pgut_option options[] =
8484
{ 'u', 14, "window", &retention_window, SOURCE_CMDLINE },
8585
/* other */
8686
{ 'U', 15, "system-identifier", &system_identifier, SOURCE_FILE_STRICT },
87+
88+
{ 's', 'd', "dbname" , &pgut_dbname, SOURCE_CMDLINE },
89+
{ 's', 'h', "host" , &host, SOURCE_CMDLINE },
90+
{ 's', 'p', "port" , &port, SOURCE_CMDLINE },
91+
{ 'b', 'q', "quiet" , &quiet, SOURCE_CMDLINE },
92+
{ 's', 'U', "username" , &username, SOURCE_CMDLINE },
93+
{ 'b', 'v', "verbose" , &verbose, SOURCE_CMDLINE },
94+
{ 'B', 'w', "no-password" , &prompt_password, SOURCE_CMDLINE },
8795
{ 0 }
8896
};
8997

@@ -93,52 +101,49 @@ static pgut_option options[] =
93101
int
94102
main(int argc, char *argv[])
95103
{
96-
const char *cmd = NULL,
97-
*subcmd = NULL;
98-
const char *backup_id_string = NULL;
104+
ProbackupSubcmd backup_subcmd;
99105
time_t backup_id = 0;
100106
int i;
101107

102-
/* do not buffer progress messages */
103-
setvbuf(stdout, 0, _IONBF, 0); /* TODO: remove this */
104-
105108
/* initialize configuration */
106109
init_backup(&current);
107110

108-
/* overwrite configuration with command line arguments */
109-
i = pgut_getopt(argc, argv, options);
111+
PROGRAM_NAME = get_progname(argv[0]);
112+
set_pglocale_pgservice(argv[0], "pgscripts");
110113

111-
for (; i < argc; i++)
114+
/* Parse subcommands and non-subcommand options */
115+
if (argc > 1)
112116
{
113-
if (cmd == NULL)
114-
cmd = argv[i];
115-
else if (strcmp(cmd, "retention") == 0)
116-
subcmd = argv[i];
117-
else if (backup_id_string == NULL &&
118-
(strcmp(cmd, "show") == 0 ||
119-
strcmp(cmd, "validate") == 0 ||
120-
strcmp(cmd, "delete") == 0 ||
121-
strcmp(cmd, "restore") == 0 ||
122-
strcmp(cmd, "delwal") == 0))
123-
backup_id_string = argv[i];
117+
if (strcmp(argv[1], "init") == 0)
118+
backup_subcmd = INIT;
119+
else if (strcmp(argv[1], "backup") == 0)
120+
backup_subcmd = BACKUP;
121+
else if (strcmp(argv[1], "restore") == 0)
122+
backup_subcmd = RESTORE;
123+
else if (strcmp(argv[1], "validate") == 0)
124+
backup_subcmd = VALIDATE;
125+
else if (strcmp(argv[1], "show") == 0)
126+
backup_subcmd = SHOW;
127+
else if (strcmp(argv[1], "delete") == 0)
128+
backup_subcmd = DELETE;
129+
else if (strcmp(argv[1], "configure") == 0)
130+
backup_subcmd = CONFIGURE;
131+
else if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
132+
{
133+
help(true);
134+
exit(0);
135+
}
136+
else if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
137+
{
138+
fprintf(stderr, "%s %s\n", PROGRAM_NAME, PROGRAM_VERSION);
139+
exit(0);
140+
}
124141
else
125-
elog(ERROR, "too many arguments");
126-
}
127-
128-
/* command argument (backup/restore/show/...) is required. */
129-
if (cmd == NULL)
130-
{
131-
help(false);
132-
return 1;
142+
elog(ERROR, "Invalid subcommand");
133143
}
134144

135-
if (backup_id_string != NULL)
136-
{
137-
backup_id = base36dec(backup_id_string);
138-
if (backup_id == 0) {
139-
elog(ERROR, "wrong ID");
140-
}
141-
}
145+
/* Parse command line arguments */
146+
i = pgut_getopt(argc, argv, options);
142147

143148
/* BACKUP_PATH is always required */
144149
if (backup_path == NULL)
@@ -189,39 +194,41 @@ main(int argc, char *argv[])
189194
num_threads = 1;
190195

191196
/* do actual operation */
192-
if (pg_strcasecmp(cmd, "init") == 0)
193-
return do_init();
194-
else if (pg_strcasecmp(cmd, "backup") == 0)
195-
return do_backup(smooth_checkpoint);
196-
else if (pg_strcasecmp(cmd, "restore") == 0)
197-
return do_restore(backup_id,
197+
switch (backup_subcmd)
198+
{
199+
case INIT:
200+
return do_init();
201+
case BACKUP:
202+
return do_backup(smooth_checkpoint);
203+
case RESTORE:
204+
return do_restore(backup_id,
198205
target_time,
199206
target_xid,
200207
target_inclusive,
201208
target_tli);
202-
else if (pg_strcasecmp(cmd, "show") == 0)
203-
return do_show(backup_id);
204-
else if (pg_strcasecmp(cmd, "validate") == 0)
205-
return do_validate(backup_id,
209+
case VALIDATE:
210+
return do_validate(backup_id,
206211
target_time,
207212
target_xid,
208213
target_inclusive,
209214
target_tli);
210-
else if (pg_strcasecmp(cmd, "delete") == 0)
211-
return do_delete(backup_id);
212-
else if (pg_strcasecmp(cmd, "delwal") == 0)
213-
return do_deletewal(backup_id, true, true);
214-
else if (pg_strcasecmp(cmd, "retention") == 0)
215-
{
216-
if (subcmd == NULL)
217-
elog(ERROR, "you must specify retention command");
218-
else if (pg_strcasecmp(subcmd, "show") == 0)
219-
return do_retention_show();
220-
else if (pg_strcasecmp(subcmd, "purge") == 0)
221-
return do_retention_purge();
215+
case SHOW:
216+
return do_show(backup_id);
217+
case DELETE:
218+
return do_delete(backup_id);
219+
case CONFIGURE:
220+
elog(ERROR, "not implemented yet");
222221
}
223-
else
224-
elog(ERROR, "invalid command \"%s\"", cmd);
222+
223+
// if (pg_strcasecmp(cmd, "retention") == 0)
224+
// {
225+
// if (subcmd == NULL)
226+
// elog(ERROR, "you must specify retention command");
227+
// else if (pg_strcasecmp(subcmd, "show") == 0)
228+
// return do_retention_show();
229+
// else if (pg_strcasecmp(subcmd, "purge") == 0)
230+
// return do_retention_purge();
231+
// }
225232

226233
return 0;
227234
}

pg_probackup.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ typedef enum BackupMode
105105
BACKUP_MODE_FULL /* full backup */
106106
} BackupMode;
107107

108+
typedef enum ProbackupSubcmd
109+
{
110+
INIT = 0,
111+
BACKUP,
112+
RESTORE,
113+
VALIDATE,
114+
SHOW,
115+
DELETE,
116+
CONFIGURE
117+
} ProbackupSubcmd;
118+
108119

109120
/* special values of pgBackup fields */
110121
#define KEEP_INFINITE (INT_MAX)

pgut/pgut.c

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,6 @@ static void on_cleanup(void);
6565
static void exit_or_abort(int exitcode);
6666
static const char *get_username(void);
6767

68-
static pgut_option default_options[] =
69-
{
70-
{ 's', 'd', "dbname" , &pgut_dbname, SOURCE_CMDLINE },
71-
{ 's', 'h', "host" , &host, SOURCE_CMDLINE },
72-
{ 's', 'p', "port" , &port, SOURCE_CMDLINE },
73-
{ 'b', 'q', "quiet" , &quiet, SOURCE_CMDLINE },
74-
{ 's', 'U', "username" , &username, SOURCE_CMDLINE },
75-
{ 'b', 'v', "verbose" , &verbose, SOURCE_CMDLINE },
76-
{ 'B', 'w', "no-password" , &prompt_password, SOURCE_CMDLINE },
77-
{ 0 }
78-
};
79-
8068
static size_t
8169
option_length(const pgut_option opts[])
8270
{
@@ -112,33 +100,14 @@ option_copy(struct option dst[], const pgut_option opts[], size_t len)
112100
}
113101
}
114102

115-
static struct option *
116-
option_merge(const pgut_option opts1[], const pgut_option opts2[])
117-
{
118-
struct option *result;
119-
size_t len1 = option_length(opts1);
120-
size_t len2 = option_length(opts2);
121-
size_t n = len1 + len2;
122-
123-
result = pgut_newarray(struct option, n + 1);
124-
option_copy(result, opts1, len1);
125-
option_copy(result + len1, opts2, len2);
126-
memset(&result[n], 0, sizeof(pgut_option));
127-
128-
return result;
129-
}
130-
131103
static pgut_option *
132-
option_find(int c, pgut_option opts1[], pgut_option opts2[])
104+
option_find(int c, pgut_option opts1[])
133105
{
134106
size_t i;
135107

136108
for (i = 0; opts1 && opts1[i].type; i++)
137109
if (opts1[i].sname == c)
138110
return &opts1[i];
139-
for (i = 0; opts2 && opts2[i].type; i++)
140-
if (opts2[i].sname == c)
141-
return &opts2[i];
142111

143112
return NULL; /* not found */
144113
}
@@ -572,38 +541,20 @@ pgut_getopt(int argc, char **argv, pgut_option options[])
572541
int c;
573542
int optindex = 0;
574543
char *optstring;
575-
struct option *longopts;
576544
pgut_option *opt;
545+
struct option *longopts;
546+
size_t len1;
577547

578-
if (PROGRAM_NAME == NULL)
579-
{
580-
PROGRAM_NAME = get_progname(argv[0]);
581-
set_pglocale_pgservice(argv[0], "pgscripts");
582-
}
583-
584-
/* Help message and version are handled at first. */
585-
if (argc > 1)
586-
{
587-
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
588-
{
589-
help(true);
590-
exit_or_abort(0);
591-
}
592-
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
593-
{
594-
fprintf(stderr, "%s %s\n", PROGRAM_NAME, PROGRAM_VERSION);
595-
exit_or_abort(0);
596-
}
597-
}
548+
len1 = option_length(options);
549+
longopts = pgut_newarray(struct option, len1 + 1);
550+
option_copy(longopts, options, len1);
598551

599-
/* Merge default and user options. */
600-
longopts = option_merge(default_options, options);
601552
optstring = longopts_to_optstring(longopts);
602553

603554
/* Assign named options */
604555
while ((c = getopt_long(argc, argv, optstring, longopts, &optindex)) != -1)
605556
{
606-
opt = option_find(c, default_options, options);
557+
opt = option_find(c, options);
607558
if (opt && opt->allowed < SOURCE_CMDLINE)
608559
elog(ERROR, "option %s cannot be specified in command line",
609560
opt->lname);

pgut/pgut.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ extern const char *username;
8585
extern char *password;
8686
extern bool verbose;
8787
extern bool quiet;
88+
extern bool prompt_password;
8889

8990
extern bool interrupted;
9091

0 commit comments

Comments
 (0)