Skip to content

Commit 43318d6

Browse files
author
Oleg Gurev
committed
[PBCKP-874] Addeded default unit output for config-show
- config-show command now has --default-units parameter - If it is provided, all time and memory - configuration options will be printed in their default units - test added, test_help_1 corrected
1 parent 09236c6 commit 43318d6

File tree

9 files changed

+63
-18
lines changed

9 files changed

+63
-18
lines changed

src/configure.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,18 +269,21 @@ static const char *current_group = NULL;
269269
* Show configure options including default values.
270270
*/
271271
void
272-
do_show_config(void)
272+
do_show_config(bool show_default_units)
273273
{
274274
int i;
275275

276276
show_configure_start();
277277

278278
for (i = 0; instance_options[i].type; i++)
279279
{
280+
if (show_default_units && strchr("bBiIuU", instance_options[i].type) && instance_options[i].get_value == *option_get_value)
281+
instance_options[i].flags |= GET_VAL_IN_DEFAULT_UNITS; /* Set flag */
280282
if (show_format == SHOW_PLAIN)
281283
show_configure_plain(&instance_options[i]);
282284
else
283285
show_configure_json(&instance_options[i]);
286+
instance_options[i].flags &= ~(GET_VAL_IN_DEFAULT_UNITS); /* Reset flag. It was resetted in option_get_value(). Probably this reset isn't needed */
284287
}
285288

286289
show_configure_end();
@@ -801,6 +804,6 @@ show_configure_json(ConfigOption *opt)
801804
return;
802805

803806
json_add_value(&show_buf, opt->lname, value, json_level,
804-
true);
807+
!(opt->flags & GET_VAL_IN_DEFAULT_UNITS));
805808
pfree(value);
806809
}

src/help.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ help_pg_probackup(void)
121121

122122
printf(_("\n %s show-config -B backup-path --instance=instance_name\n"), PROGRAM_NAME);
123123
printf(_(" [--format=format]\n"));
124+
printf(_(" [--default-units]\n"));
124125
printf(_(" [--help]\n"));
125126

126127
printf(_("\n %s backup -B backup-path -b backup-mode --instance=instance_name\n"), PROGRAM_NAME);
@@ -953,7 +954,8 @@ help_show_config(void)
953954

954955
printf(_(" -B, --backup-path=backup-path location of the backup storage area\n"));
955956
printf(_(" --instance=instance_name name of the instance\n"));
956-
printf(_(" --format=format show format=PLAIN|JSON\n\n"));
957+
printf(_(" --format=format show format=PLAIN|JSON\n"));
958+
printf(_(" --default-units show memory and time values in default units\n\n"));
957959
}
958960

959961
static void

src/pg_probackup.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ bool no_validate_wal = false;
164164
/* show options */
165165
ShowFormat show_format = SHOW_PLAIN;
166166
bool show_archive = false;
167+
static bool show_default_units = false;
167168

168169
/* set-backup options */
169170
int64 ttl = -1;
@@ -275,6 +276,8 @@ static ConfigOption cmd_options[] =
275276
/* show options */
276277
{ 'f', 165, "format", opt_show_format, SOURCE_CMD_STRICT },
277278
{ 'b', 166, "archive", &show_archive, SOURCE_CMD_STRICT },
279+
/* show-config options */
280+
{ 'b', 167, "default-units", &show_default_units,SOURCE_CMD_STRICT },
278281
/* set-backup options */
279282
{ 'I', 170, "ttl", &ttl, SOURCE_CMD_STRICT, SOURCE_DEFAULT, 0, OPTION_UNIT_S, option_get_value},
280283
{ 's', 171, "expire-time", &expire_time_string, SOURCE_CMD_STRICT },
@@ -1049,7 +1052,7 @@ main(int argc, char *argv[])
10491052
do_merge(instanceState, current.backup_id, no_validate, no_sync);
10501053
break;
10511054
case SHOW_CONFIG_CMD:
1052-
do_show_config();
1055+
do_show_config(show_default_units);
10531056
break;
10541057
case SET_CONFIG_CMD:
10551058
do_set_config(instanceState, false);

src/pg_probackup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ extern void do_archive_get(InstanceState *instanceState, InstanceConfig *instanc
939939
char *wal_file_name, int batch_size, bool validate_wal);
940940

941941
/* in configure.c */
942-
extern void do_show_config(void);
942+
extern void do_show_config(bool show_default_units);
943943
extern void do_set_config(InstanceState *instanceState, bool missing_ok);
944944
extern void init_config(InstanceConfig *config, const char *instance_name);
945945
extern InstanceConfig *readInstanceConfigFile(InstanceState *instanceState);

src/utils/configuration.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,8 @@ config_set_opt(ConfigOption options[], void *var, OptionSource source)
678678
/*
679679
* Return value of the function in the string representation. Result is
680680
* allocated string.
681+
* We can set GET_VAL_IN_DEFAULT_UNITS flag in opt->flags
682+
* before call option_get_value() to get option value in default units
681683
*/
682684
char *
683685
option_get_value(ConfigOption *opt)
@@ -692,20 +694,33 @@ option_get_value(ConfigOption *opt)
692694
*/
693695
if (opt->flags & OPTION_UNIT)
694696
{
695-
if (opt->type == 'i')
696-
convert_from_base_unit(*((int32 *) opt->var),
697-
opt->flags & OPTION_UNIT, &value, &unit);
698-
else if (opt->type == 'I')
699-
convert_from_base_unit(*((int64 *) opt->var),
700-
opt->flags & OPTION_UNIT, &value, &unit);
701-
else if (opt->type == 'u')
702-
convert_from_base_unit_u(*((uint32 *) opt->var),
703-
opt->flags & OPTION_UNIT, &value_u, &unit);
704-
else if (opt->type == 'U')
705-
convert_from_base_unit_u(*((uint64 *) opt->var),
706-
opt->flags & OPTION_UNIT, &value_u, &unit);
697+
if (opt->flags & GET_VAL_IN_DEFAULT_UNITS){
698+
if (opt->type == 'i')
699+
value = *((int32 *) opt->var);
700+
else if (opt->type == 'I')
701+
value = *((int64 *) opt->var);
702+
else if (opt->type == 'u')
703+
value_u = *((uint32 *) opt->var);
704+
else if (opt->type == 'U')
705+
value_u = *((uint64 *) opt->var);
706+
unit = "";
707+
}
708+
else
709+
{
710+
if (opt->type == 'i')
711+
convert_from_base_unit(*((int32 *) opt->var),
712+
opt->flags & OPTION_UNIT, &value, &unit);
713+
else if (opt->type == 'I')
714+
convert_from_base_unit(*((int64 *) opt->var),
715+
opt->flags & OPTION_UNIT, &value, &unit);
716+
else if (opt->type == 'u')
717+
convert_from_base_unit_u(*((uint32 *) opt->var),
718+
opt->flags & OPTION_UNIT, &value_u, &unit);
719+
else if (opt->type == 'U')
720+
convert_from_base_unit_u(*((uint64 *) opt->var),
721+
opt->flags & OPTION_UNIT, &value_u, &unit);
722+
}
707723
}
708-
709724
/* Get string representation itself */
710725
switch (opt->type)
711726
{

src/utils/configuration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ struct ConfigOption
100100
#define OPTION_UNIT_TIME 0xF0000 /* mask for time-related units */
101101

102102
#define OPTION_UNIT (OPTION_UNIT_MEMORY | OPTION_UNIT_TIME)
103+
#define GET_VAL_IN_DEFAULT_UNITS 0x80000000 /* bitflag to get memory and time values in default units*/
103104

104105
extern ProbackupSubcmd parse_subcmd(char const * const subcmd_str);
105106
extern char const *get_subcmd_name(ProbackupSubcmd const subcmd);

tests/expected/option_help.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pg_probackup - utility to manage backup/recovery of PostgreSQL database.
3939

4040
pg_probackup show-config -B backup-path --instance=instance_name
4141
[--format=format]
42+
[--default-units]
4243
[--help]
4344

4445
pg_probackup backup -B backup-path -b backup-mode --instance=instance_name

tests/expected/option_help_ru.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pg_probackup - утилита для управления резервным к
3939

4040
pg_probackup show-config -B backup-path --instance=instance_name
4141
[--format=format]
42+
[--default-units]
4243
[--help]
4344

4445
pg_probackup backup -B backup-path -b backup-mode --instance=instance_name

tests/option_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,25 @@ def test_help_6(self):
230230
self.skipTest(
231231
'You need configure PostgreSQL with --enabled-nls option for this test')
232232

233+
# @unittest.skip("skip")
234+
def test_options_default_units(self):
235+
"""check --default-units option"""
236+
backup_dir = os.path.join(self.tmp_path, self.module_name, self.fname, 'backup')
237+
node = self.make_simple_node(
238+
base_dir=os.path.join(self.module_name, self.fname, 'node'))
239+
self.init_pb(backup_dir)
240+
self.add_instance(backup_dir, 'node', node)
241+
# check that --default-units option works correctly
242+
output = self.run_pb(["show-config", "--backup-path", backup_dir, "--instance=node"])
243+
self.assertIn(container=output, member="archive-timeout = 5min")
244+
output = self.run_pb(["show-config", "--backup-path", backup_dir, "--instance=node", "--default-units"])
245+
self.assertIn(container=output, member="archive-timeout = 300")
246+
self.assertNotIn(container=output, member="archive-timeout = 300s")
247+
# check that we have now quotes ("") in json output
248+
output = self.run_pb(["show-config", "--backup-path", backup_dir, "--instance=node", "--default-units", "--format=json"])
249+
self.assertIn(container=output, member='"archive-timeout": 300,')
250+
self.assertIn(container=output, member='"retention-redundancy": 0,')
251+
self.assertNotIn(container=output, member='"archive-timeout": "300",')
233252

234253
def check_locale(locale_name):
235254
ret=True

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy