$darkmode
command.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
23 #pragma once
24 
25 #include <filesystem> // for filesystem::path
26 #include <string> // for string
27 #include <vector> // for vector<>
28 
29 #include <fable/schema.hpp> // for Schema, Struct, Variant
30 
31 #include <cloe/core.hpp> // for Confable, Schema
32 
33 namespace cloe {
34 
51 class Command : public Confable {
52  public: // Types and Constructors
56  enum class Mode {
57  Sync,
58  Async,
59  Detach,
60  };
61 
65  enum class Verbosity {
66  Never,
67  OnError,
68  Always,
69  };
70 
71  Command() = default;
72 
73  explicit Command(const std::string& command) {
74  from_conf(Json{
75  {"command", command},
76  });
77  }
78 
79  Command(const std::filesystem::path& executable, std::initializer_list<std::string> args) {
80  from_conf(Json{
81  {"executable", executable.native()},
82  {"args", args},
83  });
84  }
85 
86  public: // Special
90  std::filesystem::path executable() const { return executable_; }
91 
95  const std::vector<std::string>& args() const { return args_; }
96 
103  std::string command() const;
104 
105  Verbosity verbosity() const { return log_output_; }
106  Command verbosity(Verbosity v) && {
107  set_verbosity(v);
108  return std::move(*this);
109  }
110  void set_verbosity(Verbosity v) { log_output_ = v; }
111 
112  Mode mode() const { return mode_; }
113  Command mode(Mode m) && {
114  set_mode(m);
115  return std::move(*this);
116  }
117  void set_mode(Mode m) { mode_ = m; }
118 
119  Command sync() && { return std::move(*this).mode(Mode::Sync); }
120  bool is_sync() const { return mode() == Mode::Sync; }
121 
122  Command async() && { return std::move(*this).mode(Mode::Async); }
123  bool is_async() const { return mode() == Mode::Async; }
124 
125  Command detach() && { return std::move(*this).mode(Mode::Detach); }
126  bool is_detach() const { return mode() == Mode::Detach; }
127 
128  bool ignore_failure() const { return ignore_failure_; }
129  Command ignore_failure(bool v) && {
130  set_ignore_failure(v);
131  return std::move(*this);
132  }
133  void set_ignore_failure(bool v) { ignore_failure_ = v; }
134 
135  public: // Confable Overrides
136  CONFABLE_SCHEMA(Command) {
137  // clang-format off
138  using namespace schema; // NOLINT(build/namespaces)
139  return Variant{
140  Struct{
141  {"path", make_schema(&executable_, "path to executable").require().not_empty().executable()},
142  {"args", make_schema(&args_, "arguments to executable")},
143  {"mode", make_schema(&mode_, "synchronization mode to use")},
144  {"log_output", make_schema(&log_output_, "how to log command output")},
145  {"ignore_failure", make_schema(&ignore_failure_, "whether to ignore execution failure")},
146  },
147  Struct{
148  {"command", make_schema(&command_, "command to execute within shell").require().not_empty()},
149  {"mode", make_schema(&mode_, "synchronization mode to use")},
150  {"log_output", make_schema(&log_output_, "how to log command output")},
151  {"ignore_failure", make_schema(&ignore_failure_, "whether to ignore execution failure")},
152  },
153  String{&command_, "command to execute within shell"}.not_empty(),
154  };
155  // clang-format on
156  }
157 
158  void from_conf(const Conf& c) override;
159 
160  private:
161  std::filesystem::path executable_;
162  std::vector<std::string> args_;
163  std::string command_;
164  Mode mode_ = Mode::Sync;
165  Verbosity log_output_ = Verbosity::Always;
166  bool ignore_failure_ = false;
167 };
168 
169 // clang-format off
171  {Command::Mode::Sync, "sync"},
172  {Command::Mode::Async, "async"},
173  {Command::Mode::Detach, "detach"},
174 }))
175 
176 ENUM_SERIALIZATION(Command::Verbosity, ({
177  {Command::Verbosity::Never, "never"},
178  {Command::Verbosity::OnError, "on_error"},
179  {Command::Verbosity::Always, "always"},
180 }))
181 // clang-format on
182 
183 } // namespace cloe
Definition: command.hpp:51
Verbosity
Definition: command.hpp:65
@ OnError
never log anything
@ Always
log combined error when an error occurs
std::string command() const
Definition: command.cpp:63
void from_conf(const Conf &c) override
Definition: command.cpp:71
Mode
Definition: command.hpp:56
@ Async
run command and wait for completion
@ Detach
run command in background but wait for completion at destruction
std::filesystem::path executable() const
Definition: command.hpp:90
const std::vector< std::string > & args() const
Definition: command.hpp:95
Definition: sync.hpp:34
Definition: confable.hpp:43
Schema & schema()
Definition: confable.cpp:47
#define ENUM_SERIALIZATION(xType, xMap)
Definition: enum.hpp:51