Android-cuttlefish cvd tool
cmd_runner.h
Go to the documentation of this file.
1//
2// Copyright (C) 2023 The Android Open Source Project
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#pragma once
17
18#include <memory>
19#include <string>
20#include <type_traits>
21#include <vector>
22
28
29namespace cuttlefish {
30
31class CmdResult {
32 public:
33 CmdResult(const std::string& stdout, const std::string& stderr,
34 const int ret_code);
35 const std::string& Stdout() const { return stdout_; }
36 const std::string& Stderr() const { return stderr_; }
37 int Code() const { return code_; }
38 bool Success() const { return code_ == 0; }
39
40 private:
41 std::string stdout_;
42 std::string stderr_;
43 int code_;
44};
45
46class CmdRunner {
47 public:
48 template <
49 typename... CmdArgs,
50 typename std::enable_if<(sizeof...(CmdArgs) >= 1), bool>::type = true>
51 static CmdResult Run(const std::string& exec, const cvd_common::Envs& envs,
52 CmdArgs&&... cmd_args) {
54 args.reserve(sizeof...(CmdArgs));
55 (args.emplace_back(std::forward<CmdArgs>(cmd_args)), ...);
56 CmdRunner cmd_runner(Command(exec), args, envs);
57 return cmd_runner.Run();
58 }
59 static CmdResult Run(const cvd_common::Args& args,
60 const cvd_common::Envs& envs);
61 static CmdResult Run(const std::string& args, const cvd_common::Envs& envs);
62
63 private:
64 CmdRunner(Command&& cmd, const cvd_common::Args& args,
65 const cvd_common::Envs& envs);
66
67 CmdResult Run();
68
70};
71
72} // namespace cuttlefish
Definition: cmd_runner.h:31
int Code() const
Definition: cmd_runner.h:37
const std::string & Stdout() const
Definition: cmd_runner.h:35
std::string stderr_
Definition: cmd_runner.h:42
CmdResult(const std::string &stdout, const std::string &stderr, const int ret_code)
Definition: cmd_runner.cpp:22
std::string stdout_
Definition: cmd_runner.h:41
const std::string & Stderr() const
Definition: cmd_runner.h:36
int code_
Definition: cmd_runner.h:43
bool Success() const
Definition: cmd_runner.h:38
Definition: cmd_runner.h:46
CmdResult Run()
Definition: cmd_runner.cpp:53
static CmdResult Run(const std::string &exec, const cvd_common::Envs &envs, CmdArgs &&... cmd_args)
Definition: cmd_runner.h:51
Command cmd_
Definition: cmd_runner.h:69
CmdRunner(Command &&cmd, const cvd_common::Args &args, const cvd_common::Envs &envs)
Definition: cmd_runner.cpp:42
Definition: subprocess.h:139
std::vector< std::string > Args
Definition: types.h:25
std::unordered_map< std::string, std::string > Envs
Definition: types.h:26
Definition: alloc_utils.cpp:23
uint8_t type
Definition: pairing_connection.h:0