• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "shell_command.h"
17 
18 #include <getopt.h>
19 #include "ans_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace Notification {
ShellCommand(int argc,char * argv[],std::string name)23 ShellCommand::ShellCommand(int argc, char *argv[], std::string name)
24 {
25     opterr = 0;
26     argc_ = argc;
27     argv_ = argv;
28     name_ = name;
29     if (argc < MIN_ARGUMENT_NUMBER || argc > MAX_ARGUMENT_NUMBER) {
30         cmd_ = "help";
31         return;
32     }
33     cmd_ = argv[1];
34     for (int i = 2; i < argc; i++) {
35         argList_.push_back(argv[i]);
36     }
37 }
38 
~ShellCommand()39 ShellCommand::~ShellCommand()
40 {}
41 
OnCommand()42 ErrCode ShellCommand::OnCommand()
43 {
44     int32_t result = OHOS::ERR_OK;
45     auto respond = commandMap_[cmd_];
46     if (respond == nullptr) {
47         resultReceiver_.append(GetCommandErrorMsg());
48         respond = commandMap_["help"];
49     }
50     if (Init() == OHOS::ERR_OK) {
51         respond();
52     } else {
53         result = OHOS::ERR_INVALID_VALUE;
54     }
55     return result;
56 }
57 
ExecCommand()58 std::string ShellCommand::ExecCommand()
59 {
60     int32_t result = CreateCommandMap();
61     if (result != OHOS::ERR_OK) {
62         ANS_LOGE("failed to create command map.\n");
63     }
64     result = OnCommand();
65     if (result != OHOS::ERR_OK) {
66         ANS_LOGE("failed to execute your command.\n");
67         resultReceiver_ = "error: failed to execute your command.\n";
68     }
69     return resultReceiver_;
70 }
71 
GetCommandErrorMsg() const72 std::string ShellCommand::GetCommandErrorMsg() const
73 {
74     std::string commandErrorMsg =
75         name_ + ": '" + cmd_ + "' is not a valid " + name_ + " command. See '" + name_ + " help'.\n";
76     return commandErrorMsg;
77 }
78 
GetUnknownOptionMsg(std::string & unknownOption) const79 std::string ShellCommand::GetUnknownOptionMsg(std::string &unknownOption) const
80 {
81     std::string result = "";
82     if (optind < 0 || optind > argc_) {
83         return result;
84     }
85     result.append("error: unknown option");
86     result.append(".\n");
87     return result;
88 }
89 
GetMessageFromCode(const int32_t code) const90 std::string ShellCommand::GetMessageFromCode(const int32_t code) const
91 {
92     ANS_LOGI("[%{public}s(%{public}s)] enter", __FILE__, __FUNCTION__);
93     ANS_LOGI("code = %{public}d", code);
94     std::string result = "";
95     if (messageMap_.find(code) != messageMap_.end()) {
96         std::string message = messageMap_.at(code);
97         if (message.size() != 0) {
98             result.append(message + "\n");
99         }
100     }
101     ANS_LOGI("result = %{public}s", result.c_str());
102     return result;
103 }
104 }  // namespace Notification
105 }  // namespace OHOS