$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 <string> // for string
26 #include <vector> // for vector<>
27 
28 #include <boost/filesystem/path.hpp> // for path
29 #include <fable/schema.hpp> // for Schema, Struct, Variant
30 #include <fable/schema/boost_path.hpp> // for make_schema, Path
31 
32 #include <cloe/core.hpp> // for Confable, Schema
33 
34 namespace cloe {
35 
52 class Command : public Confable {
53  public: // Types and Constructors
57  enum class Mode {
58  Sync,
59  Async,
60  Detach,
61  };
62 
66  enum class Verbosity {
67  Never,
68  OnError,
69  Always,
70  };
71 
72  Command() = default;
73 
74  explicit Command(const std::string& command) {
75  from_conf(Json{
76  {"command", command},
77  });
78  }
79 
80  Command(boost::filesystem::path executable, std::initializer_list<std::string> args) {
81  from_conf(Json{
82  {"executable", executable.native()},
83  {"args", args},
84  });
85  }
86 
87  public: // Special
91  boost::filesystem::path executable() const { return executable_; }
92 
96  const std::vector<std::string>& args() const { return args_; }
97 
104  std::string command() const;
105 
106  Verbosity verbosity() const { return log_output_; }
107  Command verbosity(Verbosity v) && {
108  set_verbosity(v);
109  return std::move(*this);
110  }
111  void set_verbosity(Verbosity v) { log_output_ = v; }
112 
113  Mode mode() const { return mode_; }
114  Command mode(Mode m) && {
115  set_mode(m);
116  return std::move(*this);
117  }
118  void set_mode(Mode m) { mode_ = m; }
119 
120  Command sync() && { return std::move(*this).mode(Mode::Sync); }
121  bool is_sync() const { return mode() == Mode::Sync; }
122 
123  Command async() && { return std::move(*this).mode(Mode::Async); }
124  bool is_async() const { return mode() == Mode::Async; }
125 
126  Command detach() && { return std::move(*this).mode(Mode::Detach); }
127  bool is_detach() const { return mode() == Mode::Detach; }
128 
129  bool ignore_failure() const { return ignore_failure_; }
130  Command ignore_failure(bool v) && {
131  set_ignore_failure(v);
132  return std::move(*this);
133  }
134  void set_ignore_failure(bool v) { ignore_failure_ = v; }
135 
136  public: // Confable Overrides
137  CONFABLE_SCHEMA(Command) {
138  // clang-format off
139  using namespace schema; // NOLINT(build/namespaces)
140  return Variant{
141  Struct{
142  {"path", make_schema(&executable_, "path to executable").require().not_empty().executable()},
143  {"args", make_schema(&args_, "arguments to executable")},
144  {"mode", make_schema(&mode_, "synchronization mode to use")},
145  {"log_output", make_schema(&log_output_, "how to log command output")},
146  {"ignore_failure", make_schema(&ignore_failure_, "whether to ignore execution failure")},
147  },
148  Struct{
149  {"command", make_schema(&command_, "command to execute within shell").require().not_empty()},
150  {"mode", make_schema(&mode_, "synchronization mode to use")},
151  {"log_output", make_schema(&log_output_, "how to log command output")},
152  {"ignore_failure", make_schema(&ignore_failure_, "whether to ignore execution failure")},
153  },
154  String{&command_, "command to execute within shell"}.not_empty(),
155  };
156  // clang-format on
157  }
158 
159  void from_conf(const Conf& c) override;
160 
161  private:
162  boost::filesystem::path executable_;
163  std::vector<std::string> args_;
164  std::string command_;
165  Mode mode_ = Mode::Sync;
166  Verbosity log_output_ = Verbosity::Always;
167  bool ignore_failure_ = false;
168 };
169 
170 // clang-format off
172  {Command::Mode::Sync, "sync"},
173  {Command::Mode::Async, "async"},
174  {Command::Mode::Detach, "detach"},
175 }))
176 
177 ENUM_SERIALIZATION(Command::Verbosity, ({
178  {Command::Verbosity::Never, "never"},
179  {Command::Verbosity::OnError, "on_error"},
180  {Command::Verbosity::Always, "always"},
181 }))
182 // clang-format on
183 
184 } // namespace cloe
Definition: command.hpp:52
Verbosity
Definition: command.hpp:66
@ OnError
never log anything
@ Always
log combined error when an error occurs
std::string command() const
Definition: command.cpp:62
void from_conf(const Conf &c) override
Definition: command.cpp:70
Mode
Definition: command.hpp:57
@ Async
run command and wait for completion
@ Detach
run command in background but wait for completion at destruction
const std::vector< std::string > & args() const
Definition: command.hpp:96
boost::filesystem::path executable() const
Definition: command.hpp:91
Definition: sync.hpp:34
Definition: confable.hpp:98
Schema & schema()
Definition: confable.cpp:47
#define ENUM_SERIALIZATION(xType, xMap)
Definition: enum.hpp:51