Skip to content

Commit 342ec18

Browse files
committed
Refactor selector argument parsing in cvd
Avoid redundant parsing and string conversion of selector flags by passing SelectorOptions directly from SeparateArguments to CommandRequest. Assisted-by: Jetski
1 parent a070843 commit 342ec18

20 files changed

Lines changed: 64 additions & 84 deletions

base/cvd/cuttlefish/host/commands/cvd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cf_cc_library(
1616
"//cuttlefish/host/commands/cvd/cli:command_request",
1717
"//cuttlefish/host/commands/cvd/cli:frontline_parser",
1818
"//cuttlefish/host/commands/cvd/cli:nesting_commands",
19+
"//cuttlefish/host/commands/cvd/cli/selector:selector_common_parser",
1920
"//cuttlefish/host/commands/cvd/instances",
2021
"//cuttlefish/host/commands/cvd/instances:instance_manager",
2122
"//cuttlefish/host/commands/cvd/instances/lock",

base/cvd/cuttlefish/host/commands/cvd/cli/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cf_cc_library(
2424
deps = [
2525
":types",
2626
"//cuttlefish/host/commands/cvd/cli/selector:arguments_separator",
27+
"//cuttlefish/host/commands/cvd/cli/selector:selector_common_parser",
2728
"//cuttlefish/result",
2829
"//libbase",
2930
],
@@ -114,6 +115,7 @@ cf_cc_library(
114115
"//cuttlefish/host/commands/cvd/cli/parser:load_config_cc_proto",
115116
"//cuttlefish/host/commands/cvd/cli/parser:load_configs_parser",
116117
"//cuttlefish/host/commands/cvd/cli/selector:creation_analyzer",
118+
"//cuttlefish/host/commands/cvd/cli/selector:selector_common_parser",
117119
"//cuttlefish/host/commands/cvd/fetch:fetch_cvd",
118120
"//cuttlefish/host/commands/cvd/instances",
119121
"//cuttlefish/host/commands/cvd/instances:cvd_persistent_data",

base/cvd/cuttlefish/host/commands/cvd/cli/command_request.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ CommandRequestBuilder CommandRequestBuilder::AddArguments(
6666
return AddArguments(std::vector<std::string_view>(args));
6767
}
6868

69-
CommandRequestBuilder& CommandRequestBuilder::AddSelectorArguments(
70-
std::initializer_list<std::string_view> args) & {
71-
return AddSelectorArguments(std::vector<std::string_view>(args));
72-
}
73-
74-
CommandRequestBuilder CommandRequestBuilder::AddSelectorArguments(
75-
std::initializer_list<std::string_view> args) && {
76-
return AddSelectorArguments(std::vector<std::string_view>(args));
77-
}
78-
7969
CommandRequestBuilder& CommandRequestBuilder::SetEnv(cvd_common::Envs env) & {
8070
env_ = std::move(env);
8171
return *this;
@@ -99,10 +89,8 @@ CommandRequestBuilder CommandRequestBuilder::AddEnvVar(std::string key,
9989
}
10090

10191
Result<CommandRequest> CommandRequestBuilder::Build() && {
102-
return CommandRequest(
103-
std::move(args_), std::move(env_),
104-
CF_EXPECT(selector::ParseCommonSelectorArguments(selector_args_),
105-
"Failed to parse selector arguments"));
92+
return CommandRequest(std::move(args_), std::move(env_),
93+
std::move(selector_options_));
10694
}
10795

10896
} // namespace cuttlefish

base/cvd/cuttlefish/host/commands/cvd/cli/command_request.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,18 @@ class CommandRequestBuilder {
7979
CommandRequestBuilder AddArguments(
8080
std::initializer_list<std::string_view>) &&;
8181

82-
template <typename T>
83-
CommandRequestBuilder& AddSelectorArguments(T&& args) & {
84-
for (auto&& arg : args) {
85-
selector_args_.emplace_back(arg);
86-
}
82+
CommandRequestBuilder& SetSelectorOptions(
83+
selector::SelectorOptions selectors) & {
84+
selector_options_ = std::move(selectors);
8785
return *this;
8886
}
8987

90-
template <typename T>
91-
CommandRequestBuilder AddSelectorArguments(T&& args) && {
92-
for (auto&& arg : args) {
93-
selector_args_.emplace_back(arg);
94-
}
88+
CommandRequestBuilder SetSelectorOptions(
89+
selector::SelectorOptions selectors) && {
90+
selector_options_ = std::move(selectors);
9591
return *this;
9692
}
9793

98-
CommandRequestBuilder& AddSelectorArguments(
99-
std::initializer_list<std::string_view>) &;
100-
CommandRequestBuilder AddSelectorArguments(
101-
std::initializer_list<std::string_view>) &&;
102-
10394
CommandRequestBuilder& SetEnv(cvd_common::Envs) &;
10495
CommandRequestBuilder SetEnv(cvd_common::Envs) &&;
10596

@@ -111,7 +102,7 @@ class CommandRequestBuilder {
111102
private:
112103
cvd_common::Args args_;
113104
cvd_common::Envs env_;
114-
cvd_common::Args selector_args_;
105+
selector::SelectorOptions selector_options_;
115106
};
116107

117108
} // namespace cuttlefish

base/cvd/cuttlefish/host/commands/cvd/cli/commands/create.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "cuttlefish/host/commands/cvd/cli/commands/load_configs.h"
4646
#include "cuttlefish/host/commands/cvd/cli/commands/start.h"
4747
#include "cuttlefish/host/commands/cvd/cli/selector/creation_analyzer.h"
48+
#include "cuttlefish/host/commands/cvd/cli/selector/selector_common_parser.h"
4849
#include "cuttlefish/host/commands/cvd/cli/types.h"
4950
#include "cuttlefish/host/commands/cvd/instances/cvd_persistent_data.pb.h"
5051
#include "cuttlefish/host/commands/cvd/instances/instance_manager.h"
@@ -149,13 +150,15 @@ Result<CommandRequest> CreateLoadCommand(const CommandRequest& request,
149150
Result<CommandRequest> CreateStartCommand(const LocalInstanceGroup& group,
150151
const cvd_common::Args& args,
151152
const cvd_common::Envs& envs) {
152-
return CF_EXPECT(
153-
CommandRequestBuilder()
154-
.SetEnv(envs)
155-
.AddArguments({"cvd", "start"})
156-
.AddArguments(args)
157-
.AddSelectorArguments({"--group_name", group.GroupName()})
158-
.Build());
153+
selector::SelectorOptions selector_options{
154+
.group_name = group.GroupName(),
155+
};
156+
return CF_EXPECT(CommandRequestBuilder()
157+
.SetEnv(envs)
158+
.AddArguments({"cvd", "start"})
159+
.AddArguments(args)
160+
.SetSelectorOptions(std::move(selector_options))
161+
.Build());
159162
}
160163

161164
Result<cvd_common::Envs> GetEnvs(const CommandRequest& request) {

base/cvd/cuttlefish/host/commands/cvd/cli/commands/help.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class CvdHelpHandler : public CvdCommandHandler {
150150
.AddArguments(args)
151151
.AddArguments({"--help"})
152152
.SetEnv(request.Env())
153-
.AddSelectorArguments(request.Selectors().AsArgs())
153+
.SetSelectorOptions(request.Selectors())
154154
.Build());
155155

156156
std::stringstream help_message;

base/cvd/cuttlefish/host/commands/cvd/cli/commands/load_configs.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "cuttlefish/host/commands/cvd/cli/commands/start.h"
3434
#include "cuttlefish/host/commands/cvd/cli/parser/load_config.pb.h"
3535
#include "cuttlefish/host/commands/cvd/cli/parser/load_configs_parser.h"
36+
#include "cuttlefish/host/commands/cvd/cli/selector/selector_common_parser.h"
3637
#include "cuttlefish/host/commands/cvd/cli/types.h"
3738
#include "cuttlefish/host/commands/cvd/fetch/fetch_cvd.h"
3839
#include "cuttlefish/host/commands/cvd/instances/cvd_persistent_data.pb.h"
@@ -224,6 +225,9 @@ class LoadConfigsCommand : public CvdCommandHandler {
224225
fmt::format("--system_image_dir={}", group.ProductOutPath());
225226
env.erase(kAndroidProductOut);
226227

228+
selector::SelectorOptions selector_options = cvd_flags.selector_flags;
229+
selector_options.group_name = group.GroupName();
230+
227231
return CF_EXPECT(
228232
CommandRequestBuilder()
229233
.SetEnv(env)
@@ -233,8 +237,7 @@ class LoadConfigsCommand : public CvdCommandHandler {
233237
*/
234238
.AddArguments({"cvd", "start", "--daemon", system_build_arg})
235239
.AddArguments(cvd_flags.launch_cvd_flags)
236-
.AddSelectorArguments(cvd_flags.selector_flags)
237-
.AddSelectorArguments({"--group_name", group.GroupName()})
240+
.SetSelectorOptions(std::move(selector_options))
238241
.Build());
239242
}
240243

base/cvd/cuttlefish/host/commands/cvd/cli/frontline_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace cuttlefish {
3030
using selector::SeparateArguments;
3131
using selector::SeparatedArguments;
3232

33-
Result<cvd_common::Args> ExtractCvdArgs(cvd_common::Args& args) {
33+
Result<selector::SelectorOptions> ExtractCvdArgs(cvd_common::Args& args) {
3434
CF_EXPECT(!args.empty());
3535

3636
SeparatedArguments separated_arguments = CF_EXPECT(SeparateArguments(args));

base/cvd/cuttlefish/host/commands/cvd/cli/frontline_parser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
#pragma once
1818

19+
#include "cuttlefish/host/commands/cvd/cli/selector/selector_common_parser.h"
1920
#include "cuttlefish/host/commands/cvd/cli/types.h"
2021
#include "cuttlefish/result/result.h"
2122

2223
namespace cuttlefish {
2324

2425
/* Returns `cvd` arguments before the subcommand, removing them from `args`. */
25-
Result<cvd_common::Args> ExtractCvdArgs(cvd_common::Args& args);
26+
Result<selector::SelectorOptions> ExtractCvdArgs(cvd_common::Args& args);
2627

2728
} // namespace cuttlefish

base/cvd/cuttlefish/host/commands/cvd/cli/frontline_parser_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ TEST(FrontlineParserTest, SelectorArgs) {
2828
auto result = ExtractCvdArgs(input);
2929

3030
EXPECT_THAT(result, IsOk());
31-
ASSERT_EQ(*result, std::vector<std::string>{"--instance_name=1"});
31+
ASSERT_TRUE(result->instance_names.has_value());
32+
ASSERT_EQ(result->instance_names.value(), std::vector<std::string>{"1"});
3233
ASSERT_EQ(input, (std::vector<std::string>{"cvd", "status"}));
3334
}
3435

0 commit comments

Comments
 (0)