1/*
2 * Copyright (c) 2024 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 <string.h>
17#include <unistd.h>
18
19#include "hnp_base.h"
20#include "hnp_pack.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26extern int HnpCliShowHelp(int argc, char *argv[]);
27
28typedef int (*HNP_CMD_PROCESS_FUNC)(int argc, char *argv[]);
29
30typedef struct NativeManagerCmdInfoStru {
31    char *cmd;
32    HNP_CMD_PROCESS_FUNC process;
33} NativeManagerCmdInfo;
34
35NativeManagerCmdInfo g_nativeManagerCmd[] = {
36    {"help", HnpCliShowHelp},
37    {"-h", HnpCliShowHelp},
38    {"pack", HnpCmdPack}
39};
40
41int HnpCliShowHelp(int argc, char *argv[])
42{
43    (void)argc;
44    (void)argv;
45
46    HNP_LOGI("\r\nusage:hnpcli <command> <args> [-i <software package dir>][-o <hnp output path>]"
47        "[-n <native package name>][-v <native package version>]\r\n"
48        "\r\nThese are common hnpcli commands used in various situations:\r\n"
49        "\r\npack:    packet native software package to .hnp file"
50        "\r\n         hnpcli pack <-i [source path]> <-o [dst path]> <-n [software name]> <-v [software version]>"
51        "\r\n         -i    : [required]    input path of software package dir"
52        "\r\n         -o    : [optional]    output path of hnp file. if not set then ouput to current directory"
53        "\r\n         -n    : [optional]    software name. if not hnp.json in input dir then must set"
54        "\r\n         -v    : [optional]    software version. if not hnp.json in input dir then must set\r\n"
55        "\r\nfor example:\r\n"
56        "\r\n    hnpcli pack -i /usr1/native_sample -o /usr1/output -n native_sample -v 1.1\r\n");
57
58    return 0;
59}
60
61static NativeManagerCmdInfo* HnpCmdCheck(const char *cmd)
62{
63    int i;
64    int cmdNum = sizeof(g_nativeManagerCmd) / sizeof(NativeManagerCmdInfo);
65
66    for (i = 0; i < cmdNum; i++) {
67        if (!strcmp(cmd, g_nativeManagerCmd[i].cmd)) {
68            return &g_nativeManagerCmd[i];
69        }
70    }
71    return NULL;
72}
73
74int main(int argc, char *argv[])
75{
76    int ret;
77    NativeManagerCmdInfo *cmdInfo = NULL;
78
79    if (argc < HNP_INDEX_2) {
80        HnpCliShowHelp(argc, argv);
81        return HNP_ERRNO_OPERATOR_ARGV_MISS;
82    }
83
84    HNP_LOGI("native manager process start.");
85
86    /* 检验用户命令,获取对应的处理函数 */
87    cmdInfo = HnpCmdCheck(argv[HNP_INDEX_1]);
88    if (cmdInfo == NULL) {
89        HNP_LOGE("invalid cmd!. cmd:%{public}s", argv[HNP_INDEX_1]);
90        return HNP_ERRNO_OPERATOR_TYPE_INVALID;
91    }
92
93    /* 执行命令 */
94    ret = cmdInfo->process(argc, argv);
95    if (ret == HNP_ERRNO_OPERATOR_ARGV_MISS) {
96        HnpCliShowHelp(argc, argv);
97    }
98
99    HNP_LOGI("native manager process exit. ret=%{public}d ", ret);
100    return ret;
101}
102
103#ifdef __cplusplus
104}
105#endif