• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #include "common/libs/utils/contains.h"
24 #include "common/libs/utils/environment.h"
25 #include "common/libs/utils/files.h"
26 #include "common/libs/utils/subprocess.h"
27 #include "host/commands/cvd/types.h"
28 
29 namespace cuttlefish {
30 
31 class CmdResult {
32  public:
33   CmdResult(const std::string& stdout, const std::string& stderr,
34             const int ret_code);
Stdout()35   const std::string& Stdout() const { return stdout_; }
Stderr()36   const std::string& Stderr() const { return stderr_; }
Code()37   int Code() const { return code_; }
Success()38   bool Success() const { return code_ == 0; }
39 
40  private:
41   std::string stdout_;
42   std::string stderr_;
43   int code_;
44 };
45 
46 class CmdRunner {
47  public:
48   template <
49       typename... CmdArgs,
50       typename std::enable_if<(sizeof...(CmdArgs) >= 1), bool>::type = true>
Run(const std::string & exec,const cvd_common::Envs & envs,CmdArgs &&...cmd_args)51   static CmdResult Run(const std::string& exec, const cvd_common::Envs& envs,
52                        CmdArgs&&... cmd_args) {
53     cvd_common::Args 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 
69   Command cmd_;
70 };
71 
72 }  // namespace cuttlefish
73