• 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) {
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     int 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     int result = CreateCommandMap();
61     if (result != OHOS::ERR_OK) {
62         ANS_LOGE("failed to create command map.\n");
63     }
64     result = CreateMessageMap();
65     if (result != OHOS::ERR_OK) {
66         ANS_LOGE("failed to create message map.\n");
67     }
68     result = OnCommand();
69     if (result != OHOS::ERR_OK) {
70         ANS_LOGE("failed to execute your command.\n");
71         resultReceiver_ = "error: failed to execute your command.\n";
72     }
73     return resultReceiver_;
74 }
75 
GetCommandErrorMsg() const76 std::string ShellCommand::GetCommandErrorMsg() const
77 {
78     std::string commandErrorMsg =
79         name_ + ": '" + cmd_ + "' is not a valid " + name_ + " command. See '" + name_ + " help'.\n";
80     return commandErrorMsg;
81 }
82 
GetUnknownOptionMsg(std::string & unknownOption) const83 std::string ShellCommand::GetUnknownOptionMsg(std::string &unknownOption) const
84 {
85     std::string result = "";
86     if (optind < 0 || optind > argc_) {
87         return result;
88     }
89     result.append("error: unknown option");
90     result.append(".\n");
91     return result;
92 }
93 
GetMessageFromCode(const int32_t code) const94 std::string ShellCommand::GetMessageFromCode(const int32_t code) const
95 {
96     ANS_LOGI("[%{public}s(%{public}s)] enter", __FILE__, __FUNCTION__);
97     ANS_LOGI("code = %{public}d", code);
98     std::string result = "";
99     if (messageMap_.find(code) != messageMap_.end()) {
100         std::string message = messageMap_.at(code);
101         if (message.size() != 0) {
102             result.append(message + "\n");
103         }
104     }
105     ANS_LOGI("result = %{public}s", result.c_str());
106     return result;
107 }
108 }  // namespace Notification
109 }  // namespace OHOS