1afe134bbbec87adf511c56274bb8ed89d286ac4
[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         "lookup_zone_by_id", {
96             lookup_zone_by_id,
97             "lookup_zone_by_id zone_id",
98             "Prints informations about zone",
99             {{"zone_id", "id zone name"}}
100         }
101     },
102     {
103         "grant_device", {
104             grant_device,
105             "grant_device zone_id device_name",
106             "Grants access to the given device",
107             {{"zone_id", "id zone name"},
108              {"device_name", " device name"}}
109         }
110     },
111     {
112         "revoke_device", {
113             revoke_device,
114             "revoke_device zone_id device_name",
115             "Revokes access to the given device",
116             {{"zone_id", "id zone name"},
117              {"device_name", " device name"}}
118         }
119     }
120 };
121
122 void printUsage(std::ostream& out, const std::string& name)
123 {
124     out << "Usage: " << name << " [command [args]]\n\n"
125         << "command can be one of the following:\n";
126
127     for (const auto& command : commands) {
128         command.second.printUsage(out);
129     }
130 }
131
132 } // namespace
133
134 int main(const int argc, const char** argv)
135 {
136     if (argc < 2) {
137         printUsage(std::cout, argv[0]);
138         return EXIT_FAILURE;
139     }
140     if (commands.count(argv[1]) == 0) {
141         printUsage(std::cout, argv[0]);
142         return EXIT_FAILURE;
143     }
144
145     CommandLineInterface& command = commands[argv[1]];
146     try {
147         command.execute(1, argc, argv);
148     } catch (const std::runtime_error& ex) {
149         std::cerr << ex.what() << std::endl;
150         return EXIT_FAILURE;
151     }
152     return EXIT_SUCCESS;
153 }
154