Verified Commit 92549fb9 authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Add convenience functions for `command` class

- Refactor `eval()`: The function now returns a new `command` instance.
- Add `as_vector()`: This function splits the command line by ' ' and
returns it as a vector of strings.
parent 29a31ecd
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
#include <fmt/ostream.h>
#include <fstream>
#include <regex>
#include <ranges>
#include "config_file.hpp"

namespace {
@@ -333,7 +334,7 @@ command::env() const {
    return m_env;
}

std::string
command
command::eval(const std::string& adhoc_id,
              const std::filesystem::path& adhoc_directory,
              const std::vector<std::string>& adhoc_nodes) const {
@@ -378,7 +379,22 @@ command::eval(const std::string& adhoc_id,

    result += m_cmdline.substr(last_pos, m_cmdline.length() - last_pos);

    return result;
    return command{result, m_env};
}

std::vector<std::string>
command::as_vector() const {
    std::vector<std::string> tmp;

    for(auto&& r : std::views::split(m_cmdline, ' ') |
                           std::views::transform([](auto&& v) -> std::string {
                               auto c = v | std::views::common;
                               return std::string{c.begin(), c.end()};
                           })) {
        tmp.emplace_back(std::move(r));
    }

    return tmp;
}


+16 −4
Original line number Diff line number Diff line
@@ -135,19 +135,31 @@ public:
    env() const;

    /**
     * @brief Substitute the keywords in the command line template with the
     * appropriate values and produce the command line to be executed.
     * @brief Return a copy of the current `command` where all the keywords in
     * its command line template have been replaced with string
     * representations of the arguments provided.
     *
     * @param adhoc_id The ID of the adhoc storage system.
     * @param adhoc_directory The directory where the adhoc storage will run.
     * @param adhoc_nodes The nodes where the adhoc storage will run.
     * @return The evaluated command line.
     * @return The evaluated command.
     */
    std::string
    command
    eval(const std::string& adhoc_id,
         const std::filesystem::path& adhoc_directory,
         const std::vector<std::string>& adhoc_nodes) const;

    /**
     * @brief Get the command line to be executed as a vector of strings. The
     * command line is split on spaces with each string in the resulting
     * vector being a token in the command line.
     *
     * @return The command line to be executed as a vector of strings.
     */
    std::vector<std::string>
    as_vector() const;


private:
    std::string m_cmdline;
    std::optional<environment> m_env;