Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0f59db3

Browse files
committed
Code cleanup. Parse subcmd before options
1 parentf35d0d7 commit0f59db3

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(intargc,char*argv[])
95103
{
96-
constchar*cmd=NULL,
97-
*subcmd=NULL;
98-
constchar*backup_id_string=NULL;
104+
ProbackupSubcmdbackup_subcmd;
99105
time_tbackup_id=0;
100106
inti;
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-
elseif (strcmp(cmd,"retention")==0)
116-
subcmd=argv[i];
117-
elseif (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+
elseif (strcmp(argv[1],"backup")==0)
120+
backup_subcmd=BACKUP;
121+
elseif (strcmp(argv[1],"restore")==0)
122+
backup_subcmd=RESTORE;
123+
elseif (strcmp(argv[1],"validate")==0)
124+
backup_subcmd=VALIDATE;
125+
elseif (strcmp(argv[1],"show")==0)
126+
backup_subcmd=SHOW;
127+
elseif (strcmp(argv[1],"delete")==0)
128+
backup_subcmd=DELETE;
129+
elseif (strcmp(argv[1],"configure")==0)
130+
backup_subcmd=CONFIGURE;
131+
elseif (strcmp(argv[1],"--help")==0||strcmp(argv[1],"-?")==0)
132+
{
133+
help(true);
134+
exit(0);
135+
}
136+
elseif (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-
return1;
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-
returndo_init();
194-
elseif (pg_strcasecmp(cmd,"backup")==0)
195-
returndo_backup(smooth_checkpoint);
196-
elseif (pg_strcasecmp(cmd,"restore")==0)
197-
returndo_restore(backup_id,
197+
switch (backup_subcmd)
198+
{
199+
caseINIT:
200+
returndo_init();
201+
caseBACKUP:
202+
returndo_backup(smooth_checkpoint);
203+
caseRESTORE:
204+
returndo_restore(backup_id,
198205
target_time,
199206
target_xid,
200207
target_inclusive,
201208
target_tli);
202-
elseif (pg_strcasecmp(cmd,"show")==0)
203-
returndo_show(backup_id);
204-
elseif (pg_strcasecmp(cmd,"validate")==0)
205-
returndo_validate(backup_id,
209+
caseVALIDATE:
210+
returndo_validate(backup_id,
206211
target_time,
207212
target_xid,
208213
target_inclusive,
209214
target_tli);
210-
elseif (pg_strcasecmp(cmd,"delete")==0)
211-
returndo_delete(backup_id);
212-
elseif (pg_strcasecmp(cmd,"delwal")==0)
213-
returndo_deletewal(backup_id, true, true);
214-
elseif (pg_strcasecmp(cmd,"retention")==0)
215-
{
216-
if (subcmd==NULL)
217-
elog(ERROR,"you must specify retention command");
218-
elseif (pg_strcasecmp(subcmd,"show")==0)
219-
returndo_retention_show();
220-
elseif (pg_strcasecmp(subcmd,"purge")==0)
221-
returndo_retention_purge();
215+
caseSHOW:
216+
returndo_show(backup_id);
217+
caseDELETE:
218+
returndo_delete(backup_id);
219+
caseCONFIGURE:
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
return0;
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+
typedefenumProbackupSubcmd
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
#defineKEEP_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
staticvoidexit_or_abort(intexitcode);
6666
staticconstchar*get_username(void);
6767

68-
staticpgut_optiondefault_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
staticsize_t
8169
option_length(constpgut_optionopts[])
8270
{
@@ -112,33 +100,14 @@ option_copy(struct option dst[], const pgut_option opts[], size_t len)
112100
}
113101
}
114102

115-
staticstructoption*
116-
option_merge(constpgut_optionopts1[],constpgut_optionopts2[])
117-
{
118-
structoption*result;
119-
size_tlen1=option_length(opts1);
120-
size_tlen2=option_length(opts2);
121-
size_tn=len1+len2;
122-
123-
result=pgut_newarray(structoption,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-
returnresult;
129-
}
130-
131103
staticpgut_option*
132-
option_find(intc,pgut_optionopts1[],pgut_optionopts2[])
104+
option_find(intc,pgut_optionopts1[])
133105
{
134106
size_ti;
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
returnNULL;/* not found */
144113
}
@@ -572,38 +541,20 @@ pgut_getopt(int argc, char **argv, pgut_option options[])
572541
intc;
573542
intoptindex=0;
574543
char*optstring;
575-
structoption*longopts;
576544
pgut_option*opt;
545+
structoption*longopts;
546+
size_tlen1;
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(structoption,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
externchar*password;
8686
externboolverbose;
8787
externboolquiet;
88+
externboolprompt_password;
8889

8990
externboolinterrupted;
9091

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp