1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H 17 #define OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H 18 19 #include "os_account.h" 20 #include "shell_command.h" 21 22 namespace OHOS { 23 namespace AccountSA { 24 const std::string TOOL_NAME = "acm"; 25 const std::string HELP_MSG = "usage: acm <command> [<options>]\n" 26 "These are acm commands list:\n" 27 " help list available commands\n" 28 " create create a local account with options\n" 29 " delete delete a local account with options\n" 30 " switch switch to a local account with options\n" 31 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS 32 " stop stop the local accounts\n" 33 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS 34 " set set constraints of a local account\n" 35 " dump dump the info of local accounts\n"; 36 37 const std::string HELP_MSG_CREATE = 38 "usage: acm create <options>\n" 39 "options list:\n" 40 " -h, --help list available commands\n" 41 " -n <local-account-name> -t <type> create a local account with a name and a type\n" 42 " <type>: admin, normal, guest\n"; 43 44 const std::string HELP_MSG_DELETE = 45 "usage: acm delete <options>\n" 46 "options list:\n" 47 " -h, --help list available commands\n" 48 " -i <local-account-id> delete a local account with an id\n"; 49 50 const std::string HELP_MSG_DUMP = 51 "usage: acm dump <options>\n" 52 "options list:\n" 53 " -h, --help list available commands\n" 54 " -a, --all dump all local accounts\n" 55 " -i <local-account-id> dump a local account with an id\n"; 56 57 const std::string HELP_MSG_SET = 58 "usage: acm set <options>\n" 59 "options list:\n" 60 " -h, --help list available commands\n" 61 " -i <local-account-id> -c <constraints> [-e] set constraints for a local account\n"; 62 63 const std::string HELP_MSG_SWITCH = 64 "usage: acm switch <options>\n" 65 "options list:\n" 66 " -h, --help list available commands\n" 67 " -i <local-account-id> switch a local account with an id\n"; 68 69 const std::string HELP_MSG_STOP = 70 "usage: acm stop <options>\n" 71 "options list:\n" 72 " -h, --help list available commands\n" 73 " -i <local-account-id> stop a local account with an id\n"; 74 75 const std::string HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT = "error: option requires an argument."; 76 const std::string HELP_MSG_NO_NAME_OPTION = "error: -n <local-account-name> is expected"; 77 const std::string HELP_MSG_NO_TYPE_OPTION = "error: -t <type> is expected"; 78 const std::string HELP_MSG_NO_ID_OPTION = "error: -i <local-account-id> is expected"; 79 const std::string HELP_MSG_NO_CONSTRAINTS_OPTION = "error: -c <constraints> is expected"; 80 const std::string HELP_MSG_INVALID_TYPE_ARGUMENT = "error: invalid type argument"; 81 const std::string HELP_MSG_INVALID_ID_ARGUMENT = "error: invalid id argument"; 82 83 const std::string STRING_CREATE_OS_ACCOUNT_OK = "create the local account successfully."; 84 const std::string STRING_CREATE_OS_ACCOUNT_NG = "error: failed to create the local account."; 85 const std::string STRING_DELETE_OS_ACCOUNT_OK = "delete the local account successfully."; 86 const std::string STRING_DELETE_OS_ACCOUNT_NG = "error: failed to delete the local account."; 87 const std::string STRING_DUMP_OS_ACCOUNT_NG = "error: failed to dump state."; 88 const std::string STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK = "set constraints for the local account successfully."; 89 const std::string STRING_SET_OS_ACCOUNT_CONSTRAINTS_NG = "error: failed to set constraints for the local account."; 90 const std::string STRING_SWITCH_OS_ACCOUNT_OK = "switch the local account successfully."; 91 const std::string STRING_SWITCH_OS_ACCOUNT_NG = "error: failed to switch the local account."; 92 const std::string STRING_STOP_OS_ACCOUNT_OK = "stop the local account successfully."; 93 const std::string STRING_STOP_OS_ACCOUNT_NG = "error: failed to stop the local account."; 94 95 class AccountCommand : public OHOS::AAFwk::ShellCommand { 96 public: 97 AccountCommand(int argc, char *argv[]); 98 ~AccountCommand() override = default; 99 100 private: 101 ErrCode CreateCommandMap() override; 102 ErrCode CreateMessageMap() override; 103 ErrCode init() override; 104 105 ErrCode RunAsHelpCommand(void); 106 ErrCode RunAsCreateCommand(void); 107 ErrCode RunAsDeleteCommand(void); 108 ErrCode RunAsSwitchCommand(void); 109 ErrCode RunAsStopCommand(void); 110 ErrCode RunAsDumpCommand(void); 111 ErrCode RunAsSetCommand(void); 112 113 ErrCode RunAsCreateCommandError(void); 114 ErrCode RunAsCreateCommandMissingOptionArgument(void); 115 ErrCode RunAsCreateCommandExistentOptionArgument(const int &option, std::string &name, OsAccountType &type); 116 ErrCode RunAsSetCommandError(void); 117 ErrCode RunAsSetCommandMissingOptionArgument(void); 118 ErrCode RunAsSetCommandExistentOptionArgument( 119 const int &option, int &id, std::vector<std::string> &constraints, bool &enable); 120 ErrCode RunAsCommonCommandExistentOptionArgument(const int &option, int &id); 121 ErrCode RunAsCommonCommandMissingOptionArgument(const std::string &command); 122 ErrCode RunCommandError(const std::string &command); 123 124 void ParseCommandOpt(const std::string &command, ErrCode &result, int &id); 125 void RunCommand(int &counter, ErrCode &result, bool &enable, int &id, std::vector<std::string> &constraints); 126 127 ErrCode AnalyzeTypeArgument(OsAccountType &type); 128 ErrCode AnalyzeLocalIdArgument(int &id); 129 ErrCode AnalyzeConstraintArgument(std::vector<std::string> &constraints); 130 }; 131 } // namespace AccountSA 132 } // namespace OHOS 133 134 #endif // OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H 135