LCOV - code coverage report
Current view: top level - src/esys/repo/exe - cmdlist.cpp (source / functions) Hit Total Coverage
Test: esysrepo_coverage.info Lines: 77 90 85.6 %
Date: 2026-08-01 10:43:40 Functions: 12 16 75.0 %

          Line data    Source code
       1             : /*!
       2             :  * \file esys/repo/exe/cmdlist.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/exe/cmdlist.h"
      20             : #include "esys/repo/manifest/repository.h"
      21             : 
      22             : #include <nlohmann/json.hpp>
      23             : 
      24             : #include <fstream>
      25             : #include <iostream>
      26             : #include <sstream>
      27             : 
      28             : namespace esys::repo::exe
      29             : {
      30             : 
      31          21 : CmdList::CmdList()
      32          42 :     : Cmd("List")
      33             : {
      34          21 : }
      35             : 
      36          21 : CmdList::~CmdList() = default;
      37             : 
      38           0 : void CmdList::set_fullpath(bool fullpath)
      39             : {
      40           0 :     m_fullpath = fullpath;
      41           0 : }
      42             : 
      43           2 : bool CmdList::get_fullpath() const
      44             : {
      45           2 :     return m_fullpath;
      46             : }
      47             : 
      48           0 : void CmdList::set_name_only(bool name_only)
      49             : {
      50           0 :     m_name_only = name_only;
      51           0 : }
      52             : 
      53           2 : bool CmdList::get_name_only() const
      54             : {
      55           2 :     return m_name_only;
      56             : }
      57             : 
      58           0 : void CmdList::set_path_only(bool path_only)
      59             : {
      60           0 :     m_path_only = path_only;
      61           0 : }
      62             : 
      63           2 : bool CmdList::get_path_only() const
      64             : {
      65           2 :     return m_path_only;
      66             : }
      67             : 
      68           4 : void CmdList::set_json(bool json)
      69             : {
      70           4 :     m_json = json;
      71           4 :     if (json)
      72             :     {
      73             :         // Keep stdout clean for tools that parse ``esysrepo list --json``.
      74           4 :         set_print_cmd_name_by_base(false);
      75           4 :         set_print_result_summary(false);
      76             :     }
      77           4 : }
      78             : 
      79          11 : bool CmdList::get_json() const
      80             : {
      81          11 :     return m_json;
      82             : }
      83             : 
      84           2 : void CmdList::set_json_path(const std::string &path)
      85             : {
      86           2 :     m_json_path = path;
      87           2 : }
      88             : 
      89           5 : const std::string &CmdList::get_json_path() const
      90             : {
      91           5 :     return m_json_path;
      92             : }
      93             : 
      94           4 : Result CmdList::impl_run()
      95             : {
      96           4 :     if (!get_json())
      97             :     {
      98           2 :         if (get_fullpath()) warn("Option --fullpath is not implemented yet.");
      99           2 :         if (get_name_only()) warn("Option --name-only is not implemented yet.");
     100           2 :         if (get_path_only()) warn("Option --path-only is not implemtented yet.");
     101             :     }
     102             : 
     103           4 :     Result result = default_handling_folder_workspace();
     104           4 :     if (result.error()) return ESYSREPO_RESULT(result);
     105             : 
     106           4 :     result = open_esysrepo_folder();
     107           4 :     if (result.error()) return ESYSREPO_RESULT(result);
     108             : 
     109           4 :     result = load_manifest();
     110           4 :     if (result.error()) return ESYSREPO_RESULT(result);
     111             : 
     112           4 :     if (get_json())
     113             :     {
     114           2 :         if (!get_json_path().empty())
     115             :         {
     116           1 :             std::ofstream ofs(get_json_path(), std::ios::binary | std::ios::trunc);
     117           1 :             if (!ofs)
     118             :             {
     119           0 :                 error("Failed to open JSON output file: " + get_json_path());
     120           0 :                 return ESYSREPO_RESULT(ResultCode::ERROR_OPENING_FILE);
     121             :             }
     122           1 :             print_json(ofs);
     123           1 :             if (!ofs)
     124             :             {
     125           0 :                 error("Failed to write JSON output file: " + get_json_path());
     126           0 :                 return ESYSREPO_RESULT(ResultCode::GENERIC_ERROR);
     127             :             }
     128           1 :         }
     129             :         else
     130             :         {
     131           1 :             std::ostream &os = (get_console_os() != nullptr) ? *get_console_os() : std::cout;
     132           1 :             print_json(os);
     133             :         }
     134           2 :         return ESYSREPO_RESULT(ResultCode::OK);
     135             :     }
     136             : 
     137           2 :     std::ostringstream oss;
     138             : 
     139           2 :     oss << "path : name";
     140             : 
     141           8 :     for (auto location : get_manifest()->get_locations())
     142             :     {
     143          24 :         for (auto repo : location->get_repos())
     144             :         {
     145          60 :             print_info(oss, repo);
     146          20 :         }
     147           4 :     }
     148             : 
     149           2 :     info(oss.str());
     150             : 
     151           2 :     return ESYSREPO_RESULT(ResultCode::OK);
     152           4 : }
     153             : 
     154          20 : void CmdList::print_info(std::ostream &os, std::shared_ptr<manifest::Repository> repo)
     155             : {
     156          20 :     os << std::endl << repo->get_path() << " : " << repo->get_name();
     157          20 : }
     158             : 
     159           2 : void CmdList::print_json(std::ostream &os) const
     160             : {
     161           2 :     nlohmann::json root;
     162           2 :     root["meta"] = nlohmann::json::object();
     163           4 :     root["meta"]["schema"] = JSON_SCHEMA;
     164           2 :     root["meta"]["schema_version"] = JSON_SCHEMA_VERSION;
     165             : 
     166           2 :     const auto &workspace = get_workspace_path();
     167           4 :     root["workspace_root"] = workspace.empty() ? "." : workspace;
     168             : 
     169           2 :     nlohmann::json projects = nlohmann::json::array();
     170           4 :     if (get_manifest() != nullptr)
     171             :     {
     172           6 :         for (auto location : get_manifest()->get_locations())
     173             :         {
     174           6 :             for (auto repo : location->get_repos())
     175             :             {
     176           4 :                 nlohmann::json item;
     177           8 :                 item["path"] = repo->get_path();
     178           8 :                 item["name"] = repo->get_name();
     179           4 :                 projects.push_back(item);
     180           8 :             }
     181           2 :         }
     182             :     }
     183           2 :     root["projects"] = projects;
     184             : 
     185           4 :     os << root.dump(2) << std::endl;
     186           2 : }
     187             : 
     188             : } // namespace esys::repo::exe

Generated by: LCOV version 1.14