1/*
2 * Copyright (c) 2021-2023 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
21namespace OHOS {
22namespace AccountSA {
23const std::string TOOL_NAME = "acm";
24const std::string HELP_MSG_NO_OPTION = "error: you must specify an option at least.";
25const 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                             "  deactivate          deactivate to a local account with options\n"
32                             "  set                 set constraints of a local account\n"
33                             "  dump                dump the info of local accounts\n";
34
35const std::string HELP_MSG_CREATE =
36    "usage: acm create <options>\n"
37    "options list:\n"
38    "  -h, --help                           list available commands\n"
39    "  -n <local-account-name> [-s] <shortName> -t <type> [-l] <disallowed-install-hap-list>\n"
40    "                                       create a local account with a name and a type\n"
41    "                                       <type>: admin, normal, guest, private\n"
42    "                                       <disallowed-install-hap-list>: can set disallowed pre-installed hap list\n";
43
44const 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
50const 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
57const 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
63const 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
69const std::string HELP_MSG_DEACTIVATE =
70    "usage: acm deactivate <options>\n"
71    "options list:\n"
72    "  -a, --all                                        deactivate all local account\n"
73    "  -h, --help                                       list available commands\n"
74    "  -i <local-account-id>                            deactivate a local account with an id\n";
75
76const std::string HELP_MSG_STOP =
77    "usage: acm stop <options>\n"
78    "options list:\n"
79    "  -h, --help                                       list available commands\n"
80    "  -i <local-account-id>                            stop a local account with an id\n";
81
82const std::string HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT = "error: option requires an argument.";
83const std::string HELP_MSG_NO_NAME_OPTION = "error: -n <local-account-name> is expected";
84const std::string HELP_MSG_NO_TYPE_OPTION = "error: -t <type> is expected";
85const std::string HELP_MSG_NO_ID_OPTION = "error: -i <local-account-id> is expected";
86const std::string HELP_MSG_NO_CONSTRAINTS_OPTION = "error: -c <constraints> is expected";
87const std::string HELP_MSG_INVALID_TYPE_ARGUMENT = "error: invalid type argument";
88const std::string HELP_MSG_INVALID_ID_ARGUMENT = "error: invalid id argument";
89
90const std::string STRING_CREATE_OS_ACCOUNT_OK = "create the local account successfully.";
91const std::string STRING_CREATE_OS_ACCOUNT_NG = "error: failed to create the local account.";
92const std::string STRING_DELETE_OS_ACCOUNT_OK = "delete the local account successfully.";
93const std::string STRING_DELETE_OS_ACCOUNT_NG = "error: failed to delete the local account.";
94const std::string STRING_DUMP_OS_ACCOUNT_NG = "error: failed to dump state.";
95const std::string STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK = "set constraints for the local account successfully.";
96const std::string STRING_SET_OS_ACCOUNT_CONSTRAINTS_NG = "error: failed to set constraints for the local account.";
97const std::string STRING_SWITCH_OS_ACCOUNT_OK = "switch the local account successfully.";
98const std::string STRING_SWITCH_OS_ACCOUNT_NG = "error: failed to switch the local account.";
99const std::string STRING_STOP_OS_ACCOUNT_OK = "stop the local account successfully.";
100const std::string STRING_STOP_OS_ACCOUNT_NG = "error: failed to stop the local account.";
101const std::string STRING_DEACTIVATE_OS_ACCOUNT_OK = "deactivate the local account successfully.";
102const std::string STRING_DEACTIVATE_OS_ACCOUNT_NG = "error: failed to deactivate the local account.";
103const std::string STRING_DEACTIVATE_ALL_OS_ACCOUNTS_OK = "deactivate all local account successfully.";
104const std::string STRING_DEACTIVATE_ALL_OS_ACCOUNTS_NG = "error: failed to deactivate all local account.";
105
106class AccountCommand {
107public:
108    AccountCommand(int argc, char *argv[]);
109    ~AccountCommand() = default;
110    std::string ExecCommand();
111
112private:
113    void CreateCommandMap();
114    void OnCommand();
115    std::string GetCommandErrorMsg() const;
116    std::string GetUnknownOptionMsg(std::string& unknownOption) const;
117
118    ErrCode RunAsHelpCommand(void);
119    ErrCode RunAsCreateCommand(void);
120    ErrCode RunAsDeleteCommand(void);
121    ErrCode RunAsSwitchCommand(void);
122    ErrCode RunAsDeactivateCommand(void);
123    ErrCode RunAsDumpCommand(void);
124    ErrCode RunAsSetCommand(void);
125
126    ErrCode RunAsCreateCommandError(void);
127    ErrCode RunAsCreateCommandMissingOptionArgument(void);
128    ErrCode RunAsCreateCommandExistentOptionArgument(const int &option, std::string &name,
129        std::string &shortName, OsAccountType &type, std::vector<std::string> &disallowedList);
130    ErrCode RunAsSetCommandError(void);
131    ErrCode RunAsSetCommandMissingOptionArgument(void);
132    ErrCode RunAsSetCommandExistentOptionArgument(
133        const int &option, int &id, std::vector<std::string> &constraints, bool &enable);
134    ErrCode RunAsCommonCommandExistentOptionArgument(const int &option, int &id);
135    ErrCode RunAsCommonCommandMissingOptionArgument(const std::string &command);
136    ErrCode RunCommandError(const std::string &command);
137
138    void ParseCommandOpt(const std::string &command, ErrCode &result, int &id);
139    void RunCommand(int &counter, ErrCode &result, bool &enable, int &id, std::vector<std::string> &constraints);
140    ErrCode ParseCreateCommandOpt(std::string &name,
141        std::string &shortName, OsAccountType &osAccountType, std::vector<std::string> &disallowedList);
142
143    ErrCode AnalyzeTypeArgument(OsAccountType &type);
144    ErrCode AnalyzeDisallowedListArgument(std::vector<std::string> &disallowedList);
145    ErrCode AnalyzeLocalIdArgument(int &id);
146    ErrCode AnalyzeConstraintArgument(std::vector<std::string> &constraints);
147
148protected:
149    int argc_ = 0;
150    char** argv_ = nullptr;
151
152    std::string cmd_;
153    std::vector<std::string> argList_;
154
155    std::string name_;
156    std::map<std::string, std::function<int()>> commandMap_;
157
158    std::string resultReceiver_;
159};
160}  // namespace AccountSA
161}  // namespace OHOS
162
163#endif  // OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H
164