Skip to content

Commit c71151d

Browse files
committed
PGPRO-533: Add json format for show-config
1 parent 191d5e3 commit c71151d

File tree

5 files changed

+386
-174
lines changed

5 files changed

+386
-174
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ OBJS = src/backup.o src/catalog.o src/configure.o src/data.o \
44
src/pg_probackup.o src/restore.o src/show.o src/status.o \
55
src/util.o src/validate.o src/datapagemap.o src/parsexlog.o \
66
src/xlogreader.o src/streamutil.o src/receivelog.o \
7-
src/archive.o src/utils/parray.o src/utils/pgut.o src/utils/logger.o
7+
src/archive.o src/utils/parray.o src/utils/pgut.o src/utils/logger.o \
8+
src/utils/json.o
89

910
EXTRA_CLEAN = src/datapagemap.c src/datapagemap.h src/xlogreader.c \
1011
src/receivelog.c src/receivelog.h src/streamutil.c src/streamutil.h src/logging.h

src/configure.c

Lines changed: 159 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,33 @@
22
*
33
* configure.c: - manage backup catalog.
44
*
5-
* Copyright (c) 2017-2017, Postgres Professional
5+
* Copyright (c) 2017-2018, Postgres Professional
66
*
77
*-------------------------------------------------------------------------
88
*/
99

1010
#include "pg_probackup.h"
1111

12+
#include "pqexpbuffer.h"
13+
14+
#include "utils/json.h"
15+
16+
1217
static void opt_log_level_console(pgut_option *opt, const char *arg);
1318
static void opt_log_level_file(pgut_option *opt, const char *arg);
1419
static void opt_compress_alg(pgut_option *opt, const char *arg);
1520

21+
static void show_configure_start(void);
22+
static void show_configure_end(void);
23+
static void show_configure(pgBackupConfig *config);
24+
25+
static void show_configure_json(pgBackupConfig *config);
26+
1627
static pgBackupConfig *cur_config = NULL;
1728

29+
static PQExpBufferData show_buf;
30+
static int32 json_level = 0;
31+
1832
/* Set configure options */
1933
int
2034
do_configure(bool show_only)
@@ -68,7 +82,7 @@ do_configure(bool show_only)
6882
config->compress_level = compress_level;
6983

7084
if (show_only)
71-
writeBackupCatalogConfig(stderr, config);
85+
show_configure(config);
7286
else
7387
writeBackupCatalogConfigFile(config);
7488

@@ -251,7 +265,6 @@ readBackupCatalogConfigFile(void)
251265
pgut_readopt(path, options, ERROR);
252266

253267
return config;
254-
255268
}
256269

257270
static void
@@ -271,3 +284,146 @@ opt_compress_alg(pgut_option *opt, const char *arg)
271284
{
272285
cur_config->compress_alg = parse_compress_alg(arg);
273286
}
287+
288+
/*
289+
* Initialize configure visualization.
290+
*/
291+
static void
292+
show_configure_start(void)
293+
{
294+
if (show_format == SHOW_PLAIN)
295+
return;
296+
297+
/* For now we need buffer only for JSON format */
298+
json_level = 0;
299+
initPQExpBuffer(&show_buf);
300+
}
301+
302+
/*
303+
* Finalize configure visualization.
304+
*/
305+
static void
306+
show_configure_end(void)
307+
{
308+
if (show_format == SHOW_PLAIN)
309+
return;
310+
else
311+
appendPQExpBufferChar(&show_buf, '\n');
312+
313+
fputs(show_buf.data, stdout);
314+
termPQExpBuffer(&show_buf);
315+
}
316+
317+
/*
318+
* Show configure information of pg_probackup.
319+
*/
320+
static void
321+
show_configure(pgBackupConfig *config)
322+
{
323+
show_configure_start();
324+
325+
if (show_format == SHOW_PLAIN)
326+
writeBackupCatalogConfig(stdout, config);
327+
else
328+
show_configure_json(config);
329+
330+
show_configure_end();
331+
}
332+
333+
/*
334+
* Json output.
335+
*/
336+
337+
static void
338+
show_configure_json(pgBackupConfig *config)
339+
{
340+
PQExpBuffer buf = &show_buf;
341+
342+
json_add(buf, JT_BEGIN_OBJECT, &json_level);
343+
344+
json_add_value(buf, "pgdata", config->pgdata, json_level, false);
345+
346+
json_add_key(buf, "system-identifier", json_level, true);
347+
appendPQExpBuffer(buf, UINT64_FORMAT, config->system_identifier);
348+
349+
/* Connection parameters */
350+
if (config->pgdatabase)
351+
json_add_value(buf, "pgdatabase", config->pgdatabase, json_level, true);
352+
if (config->pghost)
353+
json_add_value(buf, "pghost", config->pghost, json_level, true);
354+
if (config->pgport)
355+
json_add_value(buf, "pgport", config->pgport, json_level, true);
356+
if (config->pguser)
357+
json_add_value(buf, "pguser", config->pguser, json_level, true);
358+
359+
/* Replica parameters */
360+
if (config->master_host)
361+
json_add_value(buf, "master-host", config->master_host, json_level,
362+
true);
363+
if (config->master_port)
364+
json_add_value(buf, "master-port", config->master_port, json_level,
365+
true);
366+
if (config->master_db)
367+
json_add_value(buf, "master-db", config->master_db, json_level, true);
368+
if (config->master_user)
369+
json_add_value(buf, "master-user", config->master_user, json_level,
370+
true);
371+
372+
if (config->replica_timeout != INT_MIN)
373+
{
374+
json_add_key(buf, "replica-timeout", json_level, true);
375+
appendPQExpBuffer(buf, "%d", config->replica_timeout);
376+
}
377+
378+
/* Logging parameters */
379+
if (config->log_level_console != INT_MIN)
380+
json_add_value(buf, "log-level-console",
381+
deparse_log_level(config->log_level_console), json_level,
382+
true);
383+
if (config->log_level_file != INT_MIN)
384+
json_add_value(buf, "log-level-file",
385+
deparse_log_level(config->log_level_file), json_level,
386+
true);
387+
if (config->log_filename)
388+
json_add_value(buf, "log-filename", config->log_filename, json_level,
389+
true);
390+
if (config->error_log_filename)
391+
json_add_value(buf, "error-log-filename", config->error_log_filename,
392+
json_level, true);
393+
if (config->log_directory)
394+
json_add_value(buf, "log-directory", config->log_directory, json_level,
395+
true);
396+
397+
if (config->log_rotation_size)
398+
{
399+
json_add_key(buf, "log-rotation-size", json_level, true);
400+
appendPQExpBuffer(buf, "%d", config->log_rotation_size);
401+
}
402+
if (config->log_rotation_age)
403+
{
404+
json_add_key(buf, "log-rotation-age", json_level, true);
405+
appendPQExpBuffer(buf, "%d", config->log_rotation_age);
406+
}
407+
408+
/* Retention parameters */
409+
if (config->retention_redundancy)
410+
{
411+
json_add_key(buf, "retention-redundancy", json_level, true);
412+
appendPQExpBuffer(buf, "%u", config->retention_redundancy);
413+
}
414+
if (config->retention_window)
415+
{
416+
json_add_key(buf, "retention-window", json_level, true);
417+
appendPQExpBuffer(buf, "%u", config->retention_window);
418+
}
419+
420+
/* Compression parameters */
421+
json_add_value(buf, "compress-algorithm",
422+
deparse_compress_alg(config->compress_alg), json_level,
423+
true);
424+
425+
json_add_key(buf, "compress-level", json_level, true);
426+
appendPQExpBuffer(buf, "%d", config->compress_level);
427+
428+
json_add(buf, JT_END_OBJECT, &json_level);
429+
}

0 commit comments

Comments
 (0)