Add get_zone_ids, get_active_zone_id and get_zones_status to cli
[platform/core/security/vasum.git] / cli / main.cpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Mateusz Malicki <m.malicki2@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19 /**
20  * @file
21  * @author  Mateusz Malicki (m.malicki2@samsung.com)
22  * @brief   Declaration of CommandLineInterface class
23  */
24
25 #include "command-line-interface.hpp"
26
27 #include <cstdlib>
28 #include <map>
29 #include <stdexcept>
30 #include <string>
31 #include <iostream>
32
33 using namespace vasum::cli;
34
35 namespace {
36
37 std::map<std::string, CommandLineInterface> commands = {
38     {
39         "set_active_zone", {
40             set_active_zone,
41             "set_active_zone zone_id",
42             "Set active (foreground) zone",
43             {{"zone_id", "id zone name"}}
44         }
45     },
46     {
47         "create_zone", {
48             create_zone,
49             "create_zone zone_id",
50             "Create and add zone",
51             {{"zone_id", "id zone name"}}
52         }
53     },
54     {
55         "destroy_zone", {
56             destroy_zone,
57             "destroy_zone zone_id",
58             "Destroy zone",
59             {{"zone_id", "id zone name"}}
60         }
61     },
62     {
63         "shutdown_zone", {
64             shutdown_zone,
65             "shutdown_zone zone_id",
66             "Shutdown zone",
67             {{"zone_id", "id zone name"}}
68         }
69     },
70     {
71         "start_zone", {
72             start_zone,
73             "start_zone zone_id",
74             "Start zone",
75             {{"zone_id", "id zone name"}}
76         }
77     },
78     {
79         "lock_zone", {
80             lock_zone,
81             "lock_zone zone_id",
82             "Lock zone",
83             {{"zone_id", "id zone name"}}
84         }
85     },
86     {
87         "unlock_zone", {
88             unlock_zone,
89             "unlock_zone zone_id",
90             "Unlock zone",
91             {{"zone_id", "id zone name"}}
92         }
93     },
94     {
95         "get_zones_status", {
96             get_zones_status,
97             "get_zones_status",
98             "Get list of zone with some useful informations (id, state, terminal, root path)",
99             {}
100         }
101     },
102     {
103         "get_zone_ids", {
104             get_zone_ids,
105             "get_zone_ids",
106             "Get all zone ids",
107             {}
108         }
109     },
110     {
111         "get_active_zone_id", {
112             get_active_zone_id,
113             "get_active_zone_id",
114             "Get active (foreground) zone ids",
115             {}
116         }
117     },
118     {
119         "lookup_zone_by_id", {
120             lookup_zone_by_id,
121             "lookup_zone_by_id zone_id",
122             "Prints informations about zone",
123             {{"zone_id", "id zone name"}}
124         }
125     },
126     {
127         "grant_device", {
128             grant_device,
129             "grant_device zone_id device_name",
130             "Grants access to the given device",
131             {{"zone_id", "id zone name"},
132              {"device_name", " device name"}}
133         }
134     },
135     {
136         "revoke_device", {
137             revoke_device,
138             "revoke_device zone_id device_name",
139             "Revokes access to the given device",
140             {{"zone_id", "id zone name"},
141              {"device_name", " device name"}}
142         }
143     }
144 };
145
146 void printUsage(std::ostream& out, const std::string& name)
147 {
148     out << "Usage: " << name << " [command [args]]\n\n"
149         << "command can be one of the following:\n";
150
151     for (const auto& command : commands) {
152         command.second.printUsage(out);
153     }
154 }
155
156 } // namespace
157
158 int main(const int argc, const char** argv)
159 {
160     if (argc < 2) {
161         printUsage(std::cout, argv[0]);
162         return EXIT_FAILURE;
163     }
164     if (commands.count(argv[1]) == 0) {
165         printUsage(std::cout, argv[0]);
166         return EXIT_FAILURE;
167     }
168
169     CommandLineInterface& command = commands[argv[1]];
170     try {
171         command.execute(1, argc, argv);
172     } catch (const std::runtime_error& ex) {
173         std::cerr << ex.what() << std::endl;
174         return EXIT_FAILURE;
175     }
176     return EXIT_SUCCESS;
177 }
178