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 "work_sched_hilog.h" 19 20 namespace OHOS { 21 namespace WorkScheduler { ShellCommand(int argc,char * argv[],std::string name)22ShellCommand::ShellCommand(int argc, char *argv[], std::string name) 23 { 24 argc_ = argc; 25 argv_ = argv; 26 name_ = name; 27 28 if (argc < MIN_ARGUMENT_NUMBER) { 29 cmd_ = "help"; 30 return; 31 } 32 cmd_ = argv[1]; 33 for (int i = 2; i < argc; i++) { 34 argList_.push_back(argv[i]); 35 } 36 } 37 OnCommand()38ErrCode ShellCommand::OnCommand() 39 { 40 int result = OHOS::ERR_OK; 41 42 auto respond = commandMap_[cmd_]; 43 if (respond == nullptr) { 44 resultReceiver_.append(GetCommandErrorMsg()); 45 respond = commandMap_["help"]; 46 } 47 48 if (init() == OHOS::ERR_OK) { 49 respond(); 50 } else { 51 result = OHOS::ERR_INVALID_VALUE; 52 } 53 54 return result; 55 } 56 ExecCommand()57std::string ShellCommand::ExecCommand() 58 { 59 int result = CreateCommandMap(); 60 if (result != OHOS::ERR_OK) { 61 WS_HILOGE("failed to create command map."); 62 } 63 64 result = CreateMessageMap(); 65 if (result != OHOS::ERR_OK) { 66 WS_HILOGE("failed to create message map."); 67 } 68 69 result = OnCommand(); 70 if (result != OHOS::ERR_OK) { 71 WS_HILOGE("failed to execute your command."); 72 resultReceiver_ = "error: failed to execute your command.\n"; 73 } 74 75 return resultReceiver_; 76 } 77 GetCommandErrorMsg() const78std::string ShellCommand::GetCommandErrorMsg() const 79 { 80 std::string commandErrorMsg = 81 name_ + ": '" + cmd_ + "' is not a valid " + name_ + " command. See '" + name_ + " help'.\n"; 82 return commandErrorMsg; 83 } 84 } // namespace WorkScheduler 85 } // namespace OHOS