diff --git a/base/cvd/cuttlefish/host/commands/cvd/cli/commands/create.cpp b/base/cvd/cuttlefish/host/commands/cvd/cli/commands/create.cpp index 71c8afcfc6..987d970273 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cli/commands/create.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/cli/commands/create.cpp @@ -47,6 +47,10 @@ namespace cuttlefish { namespace { +using selector::AnalyzeCreation; +using selector::CreationAnalyzerParam; +using selector::GroupCreationInfo; + constexpr char kSummaryHelpText[] = "Create a Cuttlefish virtual device or environment"; @@ -260,15 +264,14 @@ void CvdCreateCommandHandler::MarkLockfiles( Result CvdCreateCommandHandler::GetOrCreateGroup( const std::vector& subcmd_args, const cvd_common::Envs& envs, const CommandRequest& request, bool acquire_file_locks) { - using CreationAnalyzerParam = - selector::CreationAnalyzer::CreationAnalyzerParam; - CreationAnalyzerParam analyzer_param{ - .cmd_args = subcmd_args, .envs = envs, .selectors = request.Selectors()}; - - auto analyzer = CF_EXPECT( - selector::CreationAnalyzer::Create(analyzer_param, lock_manager_)); - auto group_creation_info = - CF_EXPECT(analyzer.ExtractGroupInfo(acquire_file_locks)); + GroupCreationInfo group_creation_info = CF_EXPECT(AnalyzeCreation( + { + .cmd_args = subcmd_args, + .envs = envs, + .selectors = request.Selectors(), + .acquire_file_locks = acquire_file_locks, + }, + lock_manager_)); std::vector lock_files; for (auto& instance : group_creation_info.instances) { diff --git a/base/cvd/cuttlefish/host/commands/cvd/cli/selector/creation_analyzer.cpp b/base/cvd/cuttlefish/host/commands/cvd/cli/selector/creation_analyzer.cpp index efbe106b18..9d65db99ad 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cli/selector/creation_analyzer.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/cli/selector/creation_analyzer.cpp @@ -32,6 +32,68 @@ namespace cuttlefish { namespace selector { +namespace { + +class CreationAnalyzer { + public: + struct GroupInfo { + std::string group_name; + const bool default_group; + }; + + static Result Create(const CreationAnalyzerParam& param, + InstanceLockFileManager&); + + Result ExtractGroupInfo(bool acquire_file_locks); + + private: + using IdAllocator = UniqueResourceAllocator; + + CreationAnalyzer(const CreationAnalyzerParam& param, + StartSelectorParser&& selector_options_parser, + InstanceLockFileManager& instance_lock_file_manager); + + /** + * calculate n_instances_ and instance_ids_ + */ + Result> AnalyzeInstanceIds( + bool acquire_file_locks); + + /* + * When group name is nil, it is auto-generated using instance ids + * + * If the instanc group is the default one, the group name is cvd. Otherwise, + * for given instance ids, {i}, the group name will be cvd_i. + */ + Result ExtractGroup(const std::vector&) const; + + /** + * Figures out the HOME directory + * + * The issue is that many times, HOME is anyway implicitly given. Thus, only + * if the HOME value is not equal to the HOME directory recognized by the + * system, it can be safely regarded as overridden by the user. + * + * If that is not the case, we use a automatically generated value as HOME. + * If the group instance is the default one, we still use the user's system- + * widely recognized home. If not, we populate them user /tmp/.cf// + * + */ + Result AnalyzeHome() const; + + Result> AnalyzeInstanceIdsInternal( + bool acquire_file_locks); + Result> AnalyzeInstanceIdsInternal( + const std::vector& requested_instance_ids, + bool acquire_file_locks); + + // inputs + std::unordered_map envs_; + + // internal, temporary + StartSelectorParser selector_options_parser_; + InstanceLockFileManager& instance_lock_file_manager_; +}; Result CreationAnalyzer::Create( const CreationAnalyzerParam& param, @@ -218,5 +280,15 @@ Result CreationAnalyzer::AnalyzeHome() const { return auto_generated_home; } +} // namespace + +Result AnalyzeCreation( + const CreationAnalyzerParam& params, + InstanceLockFileManager& lock_file_manager) { + CreationAnalyzer analyzer = + CF_EXPECT(CreationAnalyzer::Create(params, lock_file_manager)); + return CF_EXPECT(analyzer.ExtractGroupInfo(params.acquire_file_locks)); +} + } // namespace selector } // namespace cuttlefish diff --git a/base/cvd/cuttlefish/host/commands/cvd/cli/selector/creation_analyzer.h b/base/cvd/cuttlefish/host/commands/cvd/cli/selector/creation_analyzer.h index c10d14cac5..4a586e88a4 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cli/selector/creation_analyzer.h +++ b/base/cvd/cuttlefish/host/commands/cvd/cli/selector/creation_analyzer.h @@ -68,6 +68,13 @@ struct GroupCreationInfo { std::vector instances; }; +struct CreationAnalyzerParam { + const std::vector& cmd_args; + const std::unordered_map& envs; + const SelectorOptions& selectors; + bool acquire_file_locks; +}; + /** * Instance IDs: * Use the InstanceNumCalculator's logic @@ -96,73 +103,8 @@ struct GroupCreationInfo { * instance ids --> per_instance_name * */ -class CreationAnalyzer { - public: - struct CreationAnalyzerParam { - const std::vector& cmd_args; - const std::unordered_map& envs; - const SelectorOptions& selectors; - }; - - struct GroupInfo { - std::string group_name; - const bool default_group; - }; - - static Result Create( - const CreationAnalyzerParam& param, - InstanceLockFileManager& instance_lock_file_manager); - - Result ExtractGroupInfo(bool acquire_file_locks); - - private: - using IdAllocator = UniqueResourceAllocator; - - CreationAnalyzer(const CreationAnalyzerParam& param, - StartSelectorParser&& selector_options_parser, - InstanceLockFileManager& instance_lock_file_manager); - - /** - * calculate n_instances_ and instance_ids_ - */ - Result> AnalyzeInstanceIds( - bool acquire_file_locks); - - /* - * When group name is nil, it is auto-generated using instance ids - * - * If the instanc group is the default one, the group name is cvd. Otherwise, - * for given instance ids, {i}, the group name will be cvd_i. - */ - Result ExtractGroup(const std::vector&) const; - - /** - * Figures out the HOME directory - * - * The issue is that many times, HOME is anyway implicitly given. Thus, only - * if the HOME value is not equal to the HOME directory recognized by the - * system, it can be safely regarded as overridden by the user. - * - * If that is not the case, we use a automatically generated value as HOME. - * If the group instance is the default one, we still use the user's system- - * widely recognized home. If not, we populate them user /tmp/.cf// - * - */ - Result AnalyzeHome() const; - - Result> AnalyzeInstanceIdsInternal( - bool acquire_file_locks); - Result> AnalyzeInstanceIdsInternal( - const std::vector& requested_instance_ids, - bool acquire_file_locks); - - // inputs - std::unordered_map envs_; - - // internal, temporary - StartSelectorParser selector_options_parser_; - InstanceLockFileManager& instance_lock_file_manager_; -}; +Result AnalyzeCreation(const CreationAnalyzerParam&, + InstanceLockFileManager&); } // namespace selector } // namespace cuttlefish