1/*
2 * Copyright (c) 2022-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#include "shell_command.h"
17
18#include <getopt.h>
19#include "ans_log_wrapper.h"
20
21namespace OHOS {
22namespace Notification {
23ShellCommand::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
39ShellCommand::~ShellCommand()
40{}
41
42ErrCode 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        ANS_LOGD("Init is ERR_OK.");
52        respond();
53    } else {
54        result = OHOS::ERR_INVALID_VALUE;
55    }
56    return result;
57}
58
59std::string ShellCommand::ExecCommand()
60{
61    int32_t result = CreateCommandMap();
62    if (result != OHOS::ERR_OK) {
63        ANS_LOGE("failed to create command map.\n");
64    }
65    result = OnCommand();
66    if (result != OHOS::ERR_OK) {
67        ANS_LOGE("failed to execute your command.\n");
68        resultReceiver_ = "error: failed to execute your command.\n";
69    }
70    return resultReceiver_;
71}
72
73std::string ShellCommand::GetCommandErrorMsg() const
74{
75    ANS_LOGD("enter");
76    std::string commandErrorMsg =
77        name_ + ": '" + cmd_ + "' is not a valid " + name_ + " command. See '" + name_ + " help'.\n";
78    return commandErrorMsg;
79}
80
81std::string ShellCommand::GetUnknownOptionMsg(std::string &unknownOption) const
82{
83    std::string result = "";
84    if (optind < 0 || optind > argc_) {
85        return result;
86    }
87    result.append("error: unknown option");
88    result.append(".\n");
89    return result;
90}
91
92std::string ShellCommand::GetMessageFromCode(const int32_t code) const
93{
94    ANS_LOGI("[%{public}s(%{public}s)] enter", __FILE__, __FUNCTION__);
95    ANS_LOGI("code = %{public}d", code);
96    std::string result = "";
97    if (messageMap_.find(code) != messageMap_.end()) {
98        std::string message = messageMap_.at(code);
99        if (message.size() != 0) {
100            result.append(message + "\n");
101        }
102    }
103    ANS_LOGI("result = %{public}s", result.c_str());
104    return result;
105}
106}  // namespace Notification
107}  // namespace OHOS