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