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 17 #pragma once 18 19 #include <sys/types.h> 20 21 #include <optional> 22 #include <string> 23 #include <unordered_map> 24 #include <unordered_set> 25 #include <vector> 26 27 #include "common/libs/utils/result.h" 28 #include "host/commands/cvd/types.h" 29 30 namespace cuttlefish { 31 namespace selector { 32 33 class SelectorCommonParser { 34 public: 35 // parses common selector options, and drop the used selector_args 36 static Result<SelectorCommonParser> Parse(const uid_t client_uid, 37 cvd_common::Args& selector_args, 38 const cvd_common::Envs& envs); 39 GroupName()40 std::optional<std::string> GroupName() const { return group_name_; } 41 PerInstanceNames()42 std::optional<std::vector<std::string>> PerInstanceNames() const { 43 return instance_names_; 44 } 45 46 // CF_ERR --> unknown, true --> overridden, false --> not overridden. 47 Result<bool> HomeOverridden() const; 48 std::optional<std::string> Home() const; 49 50 /* 51 * returns if selector flags has device select options: e.g. --group_name 52 * 53 * this is mainly to see if cvd start is about the default instance. 54 */ HasDeviceSelectOption()55 bool HasDeviceSelectOption() const { return group_name_ || instance_names_; } 56 57 private: 58 SelectorCommonParser(const std::string& client_user_home, 59 const cvd_common::Envs& envs); 60 61 Result<void> ParseOptions(cvd_common::Args& selector_args); 62 struct ParsedNameFlags { 63 std::optional<std::string> group_name; 64 std::optional<std::vector<std::string>> instance_names; 65 }; 66 struct NameFlagsParam { 67 std::optional<std::string> group_name; 68 std::optional<std::string> instance_names; 69 }; 70 Result<ParsedNameFlags> HandleNameOpts( 71 const NameFlagsParam& name_flags) const; 72 Result<std::string> HandleGroupName( 73 const std::optional<std::string>& group_name) const; 74 Result<std::vector<std::string>> HandleInstanceNames( 75 const std::optional<std::string>& per_instance_names) const; 76 77 // temporarily keeps the leftover of the input cmd_args 78 // Will be never used after parsing is done 79 std::string client_user_home_; 80 const cvd_common::Envs& envs_; 81 82 // processed result 83 std::optional<std::string> group_name_; 84 std::optional<std::vector<std::string>> instance_names_; 85 }; 86 87 } // namespace selector 88 } // namespace cuttlefish 89