changeset 602:24959a94de96

Irccdctl: use config class, closes #749
author David Demelier <markand@malikania.fr>
date Thu, 07 Dec 2017 20:07:10 +0100
parents 48c6a1167245
children 986ed3a7575d
files irccd/main.cpp irccdctl/main.cpp libcommon/CMakeLists.txt libcommon/irccd/config.cpp libcommon/irccd/config.hpp libirccd/CMakeLists.txt libirccd/irccd/daemon/config.cpp libirccd/irccd/daemon/config.hpp
diffstat 8 files changed, 171 insertions(+), 178 deletions(-) [+]
line wrap: on
line diff
--- a/irccd/main.cpp	Wed Dec 06 21:47:11 2017 +0100
+++ b/irccd/main.cpp	Thu Dec 07 20:07:10 2017 +0100
@@ -33,12 +33,12 @@
 #include <csignal>
 #include <iostream>
 
+#include <irccd/config.hpp>
 #include <irccd/options.hpp>
 #include <irccd/string_util.hpp>
 #include <irccd/system.hpp>
 
 #include <irccd/daemon/command_service.hpp>
-#include <irccd/daemon/config.hpp>
 #include <irccd/daemon/irccd.hpp>
 #include <irccd/daemon/logger.hpp>
 #include <irccd/daemon/plugin_service.hpp>
@@ -160,7 +160,12 @@
     if (it != result.end() || (it = result.find("--config")) != result.end())
         return config(it->second);
 
-    return config::find("irccd.conf");
+    auto cfg = config::find("irccd.conf");
+
+    if (!cfg)
+        throw std::runtime_error("no configuration file could be found");
+
+    return *cfg;
 }
 
 } // !namespace
--- a/irccdctl/main.cpp	Wed Dec 06 21:47:11 2017 +0100
+++ b/irccdctl/main.cpp	Thu Dec 07 20:07:10 2017 +0100
@@ -23,7 +23,7 @@
 #include <boost/filesystem.hpp>
 #include <boost/timer/timer.hpp>
 
-#include <irccd/ini.hpp>
+#include <irccd/config.hpp>
 #include <irccd/json_util.hpp>
 #include <irccd/options.hpp>
 #include <irccd/string_util.hpp>
@@ -257,28 +257,23 @@
     return alias;
 }
 
-void read(const std::string& path)
+void read(const config& cfg)
 {
-    try {
-        ini::document doc = ini::read_file(path);
-        ini::document::const_iterator it;
+    ini::document::const_iterator it;
 
-        if (!ctl && (it = doc.find("connect")) != doc.end())
-            read_connect(*it);
-        if ((it = doc.find("general")) != doc.end())
-            read_general(*it);
+    if (!ctl && (it = cfg.doc().find("connect")) != cfg.doc().end())
+        read_connect(*it);
+    if ((it = cfg.doc().find("general")) != cfg.doc().end())
+        read_general(*it);
 
-        // [alias.*] sections.
-        for (const auto& sc : doc) {
-            if (sc.key().compare(0, 6, "alias.") == 0) {
-                auto name = sc.key().substr(6);
-                auto alias = read_alias(sc, name);
+    // [alias.*] sections.
+    for (const auto& sc : cfg.doc()) {
+        if (sc.key().compare(0, 6, "alias.") == 0) {
+            auto name = sc.key().substr(6);
+            auto alias = read_alias(sc, name);
 
-                aliases.emplace(std::move(name), std::move(alias));
-            }
+            aliases.emplace(std::move(name), std::move(alias));
         }
-    } catch (const std::exception &ex) {
-        std::cerr << path << ": " << ex.what() << std::endl;
     }
 }
 
@@ -573,14 +568,8 @@
         if (it != result.end() || (it = result.find("--config")) != result.end())
             irccd::ctl::read(it->second);
         else {
-            for (const auto& path : irccd::sys::config_filenames("irccdctl.conf")) {
-                boost::system::error_code ec;
-
-                if (boost::filesystem::exists(path, ec) && !ec) {
-                    irccd::ctl::read(path);
-                    break;
-                }
-            }
+            if (auto conf = irccd::config::find("irccdctl.conf"))
+                irccd::ctl::read(*conf);
         }
     } catch (const std::exception& ex) {
         std::cerr << "abort: " << ex.what() << std::endl;
--- a/libcommon/CMakeLists.txt	Wed Dec 06 21:47:11 2017 +0100
+++ b/libcommon/CMakeLists.txt	Thu Dec 07 20:07:10 2017 +0100
@@ -22,6 +22,7 @@
 
 set(
     HEADERS
+    ${libcommon_SOURCE_DIR}/irccd/config.hpp
     ${libcommon_SOURCE_DIR}/irccd/fs_util.hpp
     ${libcommon_SOURCE_DIR}/irccd/ini.hpp
     ${libcommon_SOURCE_DIR}/irccd/json_util.hpp
@@ -35,6 +36,7 @@
 
 set(
     SOURCES
+    ${libcommon_SOURCE_DIR}/irccd/config.cpp
     ${libcommon_SOURCE_DIR}/irccd/ini.cpp
     ${libcommon_SOURCE_DIR}/irccd/json_util.cpp
     ${libcommon_SOURCE_DIR}/irccd/options.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libcommon/irccd/config.cpp	Thu Dec 07 20:07:10 2017 +0100
@@ -0,0 +1,39 @@
+/*
+ * config.cpp -- irccd configuration loader
+ *
+ * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <boost/filesystem.hpp>
+
+#include <irccd/system.hpp>
+
+#include "config.hpp"
+
+namespace irccd {
+
+boost::optional<config> config::find(const std::string& name)
+{
+    for (const auto& path : sys::config_filenames(name)) {
+        boost::system::error_code ec;
+
+        if (boost::filesystem::exists(path, ec) && !ec)
+            return config(path);
+    }
+
+    return boost::optional<config>();
+}
+
+} // !irccd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libcommon/irccd/config.hpp	Thu Dec 07 20:07:10 2017 +0100
@@ -0,0 +1,108 @@
+/*
+ * config.hpp -- irccd configuration loader
+ *
+ * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef IRCCD_CONFIG_HPP
+#define IRCCD_CONFIG_HPP
+
+/**
+ * \file config.hpp
+ * \brief Read .ini configuration file for irccd
+ */
+
+#include <boost/optional.hpp>
+
+#include "ini.hpp"
+
+namespace irccd {
+
+/**
+ * \brief Read .ini configuration file for irccd
+ */
+class config {
+private:
+    std::string path_;
+    ini::document document_;
+
+public:
+    /**
+     * Search the configuration file into the standard defined paths.
+     *
+     * \param name the file name
+     * \return the config or empty if not found
+     */
+    static boost::optional<config> find(const std::string& name);
+
+    /**
+     * Load the configuration from the specified path.
+     *
+     * \param path the path
+     */
+    inline config(std::string path = "")
+        : path_(std::move(path))
+    {
+        if (!path_.empty())
+            document_ = ini::read_file(path_);
+    }
+
+    /**
+     * Get the underlying document.
+     *
+     * \return the document
+     */
+    inline const ini::document& doc() const noexcept
+    {
+        return document_;
+    }
+
+    /**
+     * Get the path to the configuration file.
+     *
+     * \return the path
+     */
+    inline const std::string& path() const noexcept
+    {
+        return path_;
+    }
+
+    /**
+     * Convenience function to access a section.
+     *
+     * \param section the section name
+     * \return the section or empty one
+     */
+    inline ini::section section(const std::string& section) const noexcept
+    {
+        return document_.get(section);
+    }
+
+    /**
+     * Convenience function to access an ini value.
+     *
+     * \param section the section name
+     * \param option the option name
+     * \return the value or empty string
+     */
+    inline std::string value(const std::string& section, const std::string& option) const noexcept
+    {
+        return document_.get(section).get(option).value();
+    }
+};
+
+} // !irccd
+
+#endif // !IRCCD_CONFIG_HPP
--- a/libirccd/CMakeLists.txt	Wed Dec 06 21:47:11 2017 +0100
+++ b/libirccd/CMakeLists.txt	Thu Dec 07 20:07:10 2017 +0100
@@ -26,7 +26,6 @@
     ${libirccd_SOURCE_DIR}/irccd/daemon/basic_transport_server.hpp
     ${libirccd_SOURCE_DIR}/irccd/daemon/command.hpp
     ${libirccd_SOURCE_DIR}/irccd/daemon/command_service.hpp
-    ${libirccd_SOURCE_DIR}/irccd/daemon/config.hpp
     ${libirccd_SOURCE_DIR}/irccd/daemon/dynlib_plugin.hpp
     ${libirccd_SOURCE_DIR}/irccd/daemon/ip_transport_server.hpp
     ${libirccd_SOURCE_DIR}/irccd/daemon/irccd.hpp
@@ -49,7 +48,6 @@
     SOURCES
     ${libirccd_SOURCE_DIR}/irccd/daemon/command.cpp
     ${libirccd_SOURCE_DIR}/irccd/daemon/command_service.cpp
-    ${libirccd_SOURCE_DIR}/irccd/daemon/config.cpp
     ${libirccd_SOURCE_DIR}/irccd/daemon/dynlib_plugin.cpp
     ${libirccd_SOURCE_DIR}/irccd/daemon/irccd.cpp
     ${libirccd_SOURCE_DIR}/irccd/daemon/irc.cpp
--- a/libirccd/irccd/daemon/config.cpp	Wed Dec 06 21:47:11 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-/*
- * config.cpp -- irccd configuration loader
- *
- * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <stdexcept>
-
-#include <boost/filesystem.hpp>
-
-#include <irccd/system.hpp>
-
-#include "config.hpp"
-
-namespace irccd {
-
-config config::find(const std::string& name)
-{
-    for (const auto& path : sys::config_filenames(name)) {
-        boost::system::error_code ec;
-
-        if (boost::filesystem::exists(path, ec) && !ec)
-            return config(path);
-    }
-
-    throw std::runtime_error("no configuration file found");
-}
-
-} // !irccd
--- a/libirccd/irccd/daemon/config.hpp	Wed Dec 06 21:47:11 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,107 +0,0 @@
-/*
- * config.hpp -- irccd configuration loader
- *
- * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#ifndef IRCCD_CONFIG_HPP
-#define IRCCD_CONFIG_HPP
-
-/**
- * \file config.hpp
- * \brief Read .ini configuration file for irccd
- */
-
-#include "ini.hpp"
-
-namespace irccd {
-
-/**
- * \brief Read .ini configuration file for irccd
- */
-class config {
-private:
-    std::string path_;
-    ini::document document_;
-
-public:
-    /**
-     * Search the configuration file into the standard defined paths.
-     *
-     * \param name the file name
-     * \return the config
-     * \throw std::exception on errors or if no config could be found
-     */
-    static config find(const std::string& name);
-
-    /**
-     * Load the configuration from the specified path.
-     *
-     * \param path the path
-     */
-    inline config(std::string path = "")
-        : path_(std::move(path))
-    {
-        if (!path_.empty())
-            document_ = ini::read_file(path_);
-    }
-
-    /**
-     * Get the underlying document.
-     *
-     * \return the document
-     */
-    inline const ini::document& doc() const noexcept
-    {
-        return document_;
-    }
-
-    /**
-     * Get the path to the configuration file.
-     *
-     * \return the path
-     */
-    inline const std::string& path() const noexcept
-    {
-        return path_;
-    }
-
-    /**
-     * Convenience function to access a section.
-     *
-     * \param section the section name
-     * \return the section or empty one
-     */
-    inline ini::section section(const std::string& section) const noexcept
-    {
-        return document_.get(section);
-    }
-
-    /**
-     * Convenience function to access an ini value.
-     *
-     * \param section the section name
-     * \param option the option name
-     * \return the value or empty string
-     */
-    inline std::string value(const std::string& section, const std::string& option) const noexcept
-    {
-        return document_.get(section).get(option).value();
-    }
-};
-
-} // !irccd
-
-#endif // !IRCCD_CONFIG_HPP