]> git.ipfire.org Git - thirdparty/knot-dns.git/commitdiff
knotc: use stdout as a default for configuration export
authorDaniel Salzman <daniel.salzman@nic.cz>
Sun, 21 Jan 2018 21:19:48 +0000 (22:19 +0100)
committerDaniel Salzman <daniel.salzman@nic.cz>
Mon, 22 Jan 2018 14:13:55 +0000 (15:13 +0100)
doc/man/knotc.8in
doc/man_knotc.rst
src/knot/conf/base.c
src/knot/conf/base.h
src/utils/knotc/commands.c

index 900ab730e68efbd5e065f2ddc26286b58bc47e4c..4d42c20be299aa21f0ccce9ed7e238d89bd265d5 100644 (file)
@@ -174,8 +174,8 @@ Check the server configuration. (*)
 Import a configuration file into the configuration database. Ensure the
 server is not using the configuration database! (*)
 .TP
-\fBconf\-export\fP \fIfilename\fP
-Export the configuration database into a config file. (*)
+\fBconf\-export\fP [\fIfilename\fP]
+Export the configuration database into a config file or stdout. (*)
 .TP
 \fBconf\-list\fP [\fIitem\fP]
 List the configuration database sections or section items.
index c254b75b6a3b3aa7a9cf9ff6adb19e8f18d24bcd..95ac88358b1f1047a9dce4c853aeaba4419126a0 100644 (file)
@@ -151,8 +151,8 @@ Actions
   Import a configuration file into the configuration database. Ensure the
   server is not using the configuration database! (*)
 
-**conf-export** *filename*
-  Export the configuration database into a config file. (*)
+**conf-export** [*filename*]
+  Export the configuration database into a config file or stdout. (*)
 
 **conf-list** [*item*]
   List the configuration database sections or section items.
index 3a99599d79fc52b87d3159b17a63c7543b95cdac..f523a8e61a4f34ba46898519cec760fa41624921 100644 (file)
@@ -1,4 +1,4 @@
-/*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -867,7 +867,7 @@ int conf_export(
        const char *file_name,
        yp_style_t style)
 {
-       if (conf == NULL || file_name == NULL) {
+       if (conf == NULL) {
                return KNOT_EINVAL;
        }
 
@@ -878,7 +878,7 @@ int conf_export(
                return KNOT_ENOMEM;
        }
 
-       FILE *fp = fopen(file_name, "w");
+       FILE *fp = (file_name != NULL) ? fopen(file_name, "w") : stdout;
        if (fp == NULL) {
                free(buff);
                return knot_map_errno();
@@ -923,7 +923,9 @@ int conf_export(
 
        ret = KNOT_EOK;
 export_error:
-       fclose(fp);
+       if (file_name != NULL) {
+               fclose(fp);
+       }
        free(buff);
 
        return ret;
index 7a48531eecbb51967f90a2a91810ed2a29ea5731..bd7412c9d0a94224f912a575fee0998d5d6f0336 100644 (file)
@@ -1,4 +1,4 @@
-/*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -274,7 +274,7 @@ int conf_import(
  * Exports configuration to textual file.
  *
  * \param[in] conf       Configuration.
- * \param[in] file_name  Output filename.
+ * \param[in] file_name  Output filename (stdout is used if NULL).
  * \param[in] style      Formatting style.
  *
  * \return Error code, KNOT_EOK if success.
index 6bf21c7fe4bbd197fe0f351299ae413cc7afc5a7..3433dd05d87e7a72f680c33f0f5f7a526ec8be1e 100644 (file)
@@ -1,4 +1,4 @@
-/*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -978,17 +978,24 @@ static int cmd_conf_import(cmd_args_t *args)
 
 static int cmd_conf_export(cmd_args_t *args)
 {
-       int ret = check_args(args, 1, 1);
+       int ret = check_args(args, 0, 1);
        if (ret != KNOT_EOK) {
                return ret;
        }
 
-       log_debug("exporting confdb into file '%s'", args->argv[0]);
+       // Stdout is the default output file.
+       const char *file_name = NULL;
+       if (args->argc > 0) {
+               file_name = args->argv[0];
+               log_debug("exporting confdb into file '%s'", file_name);
+       }
 
-       ret = conf_export(conf(), args->argv[0], YP_SNONE);
+       ret = conf_export(conf(), file_name, YP_SNONE);
 
        if (ret == KNOT_EOK) {
-               log_info("OK");
+               if (args->argc > 0) {
+                       log_info("OK");
+               }
        } else {
                log_error("export (%s)", knot_strerror(ret));
        }
@@ -1125,7 +1132,7 @@ static const cmd_help_t cmd_help_table[] = {
        { CMD_CONF_INIT,       "",                                       "Initialize the confdb. (*)" },
        { CMD_CONF_CHECK,      "",                                       "Check the server configuration. (*)" },
        { CMD_CONF_IMPORT,     "<filename>",                             "Import a config file into the confdb. (*)" },
-       { CMD_CONF_EXPORT,     "<filename>",                             "Export the confdb into a config file. (*)" },
+       { CMD_CONF_EXPORT,     "[<filename>]",                           "Export the confdb into a config file or stdout. (*)" },
        { CMD_CONF_LIST,       "[<item>...]",                            "List the confdb sections or section items." },
        { CMD_CONF_READ,       "[<item>...]",                            "Get the item from the active confdb." },
        { CMD_CONF_BEGIN,      "",                                       "Begin a writing confdb transaction." },