LCOV - code coverage report
Current view: top level - src/esys/repo - configfileimpl.cpp (source / functions) Hit Total Coverage
Test: esysrepo_coverage.info Lines: 67 73 91.8 %
Date: 2026-08-01 10:43:40 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /*!
       2             :  * \file esys/repo/configfileimpl.cpp
       3             :  * \brief
       4             :  *
       5             :  * \cond
       6             :  * __legal_b__
       7             :  *
       8             :  * Copyright (c) 2020-2022 Michel Gillet
       9             :  * Distributed under the MIT License.
      10             :  * (See accompanying file LICENSE.txt or
      11             :  * copy at https://opensource.org/licenses/MIT)
      12             :  *
      13             :  * __legal_e__
      14             :  * \endcond
      15             :  *
      16             :  */
      17             : 
      18             : #include "esys/repo/esysrepo_prec.h"
      19             : #include "esys/repo/configfileimpl.h"
      20             : 
      21             : #include <boost/filesystem.hpp>
      22             : 
      23             : #include <fstream>
      24             : #include <iomanip>
      25             : 
      26             : namespace esys::repo
      27             : {
      28             : 
      29          59 : ConfigFileImpl::ConfigFileImpl(ConfigFile *self)
      30          59 :     : m_self(self)
      31             : {
      32          59 : }
      33             : 
      34         118 : ConfigFileImpl::~ConfigFileImpl() = default;
      35             : 
      36          37 : Result ConfigFileImpl::open(const std::string &path)
      37             : {
      38         111 :     if (self()->get_config() == nullptr) self()->set_config(std::make_shared<Config>());
      39             : 
      40          37 :     std::ifstream fin(path);
      41          37 :     json obj;
      42             : 
      43          37 :     if (!fin.is_open()) return ESYSREPO_RESULT(ResultCode::ERROR_OPENING_FILE, path);
      44             : 
      45          37 :     try
      46             :     {
      47          37 :         fin >> obj;
      48             :     }
      49           0 :     catch (json::parse_error &)
      50             :     {
      51           0 :         return ESYSREPO_RESULT(ResultCode::CFGFILE_ERROR_PARSING_FILE, path);
      52           0 :     }
      53             : 
      54          37 :     if (!obj.contains("manifest_type")) return ESYSREPO_RESULT(ResultCode::CFGFILE_NO_MANIFEST_TYPE, path);
      55             : 
      56          37 :     manifest::Type type = manifest::Type::NOT_SET;
      57             : 
      58          37 :     std::string value = obj["manifest_type"];
      59          37 :     Result result = manifest::convert(value, type);
      60          37 :     if (result.error()) return ESYSREPO_RESULT(result, ResultCode::CFGFILE_UNKNOWN_MANIFEST_TYPE, value);
      61             : 
      62          37 :     self()->get_config()->set_manifest_type(type);
      63             : 
      64          37 :     if (!obj.contains("manifest_url")) return ESYSREPO_RESULT(ResultCode::CFGFILE_NO_MANIFEST_URL, path);
      65         111 :     self()->get_config()->set_manifest_url(obj["manifest_url"]);
      66             : 
      67          37 :     if (obj.contains("manifest_path"))
      68             :     {
      69         148 :         self()->get_config()->set_manifest_path(obj["manifest_path"]);
      70             :     }
      71             : 
      72          37 :     if (obj.contains("manifest_kind"))
      73             :     {
      74          37 :         manifest::Kind kind;
      75             : 
      76          37 :         std::string value = obj["manifest_kind"];
      77          37 :         auto result = convert(value, kind);
      78          37 :         if (result.error()) return ESYSREPO_RESULT(result, ResultCode::CFGFILE_UNKNOWN_MANIFEST_KIND, value);
      79             : 
      80          37 :         self()->get_config()->set_manifest_kind(kind);
      81          37 :     }
      82             : 
      83          37 :     if (obj.contains("manifest_format"))
      84             :     {
      85          37 :         manifest::Format format;
      86             : 
      87          37 :         std::string value = obj["manifest_format"];
      88          37 :         auto result = convert(value, format);
      89          37 :         if (result.error()) return ESYSREPO_RESULT(result, ResultCode::CFGFILE_UNKNOWN_MANIFEST_FORMAT, value);
      90             : 
      91          37 :         self()->get_config()->set_manifest_format(format);
      92          37 :     }
      93          37 :     return ESYSREPO_RESULT(ResultCode::OK);
      94          74 : }
      95             : 
      96          22 : Result ConfigFileImpl::write(const std::string &path)
      97             : {
      98          44 :     if (self()->get_config() == nullptr) return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR);
      99             : 
     100          22 :     auto cfg = self()->get_config();
     101             : 
     102          22 :     if (cfg == nullptr) return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR);
     103             : 
     104          22 :     json cfg_json;
     105          22 :     std::string text;
     106          22 :     Result result = manifest::convert(cfg->get_manifest_type(), text);
     107          22 :     if (result.error()) return ESYSREPO_RESULT(result);
     108             : 
     109          44 :     cfg_json["manifest_type"] = text;
     110          44 :     if (!cfg->get_manifest_path().empty()) cfg_json["manifest_path"] = cfg->get_manifest_path();
     111          44 :     if (!cfg->get_manifest_url().empty()) cfg_json["manifest_url"] = cfg->get_manifest_url();
     112             : 
     113          22 :     std::string kind;
     114          22 :     result = convert(cfg->get_manifest_kind(), kind);
     115          22 :     if (result.error()) return ESYSREPO_RESULT(result);
     116          44 :     cfg_json["manifest_kind"] = kind;
     117             : 
     118          22 :     std::string format;
     119          22 :     result = convert(cfg->get_manifest_format(), format);
     120          22 :     if (result.error()) return ESYSREPO_RESULT(result);
     121          44 :     cfg_json["manifest_format"] = format;
     122             : 
     123          22 :     std::ofstream ofs;
     124             : 
     125          22 :     if (boost::filesystem::exists(path))
     126           0 :         ofs.open(path.c_str(), std::ios_base::trunc);
     127             :     else
     128          22 :         ofs.open(path.c_str(), std::ios_base::out);
     129             : 
     130          22 :     if (!ofs.is_open()) return ESYSREPO_RESULT(ResultCode::ERROR_OPENING_FILE, path);
     131             : 
     132          22 :     ofs << std::setw(4);
     133             : 
     134          22 :     ofs << cfg_json;
     135             : 
     136          22 :     ofs.close();
     137             : 
     138          22 :     return ESYSREPO_RESULT(ResultCode::OK);
     139         110 : }
     140             : 
     141         303 : ConfigFile *ConfigFileImpl::self()
     142             : {
     143         303 :     return m_self;
     144             : }
     145             : 
     146           0 : const ConfigFile *ConfigFileImpl::self() const
     147             : {
     148           0 :     return m_self;
     149             : }
     150             : 
     151             : } // namespace esys::repo

Generated by: LCOV version 1.14