Prepare the Integration with systemd
[src/app-framework-main.git] / src / tests / test-unit / test-unit.c
1 /*
2  Copyright 2016, 2017 IoT.bzh
3
4  author: José Bollo <jose.bollo@iot.bzh>
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 #include <stdlib.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #include <json-c/json.h>
27
28 #include <wgt.h>
29 #include <utils-json.h>
30 #include <wgt-json.h>
31 #include <wgtpkg-mustach.h>
32 #include <wgtpkg-unit.h>
33
34
35 #define error(...) fprintf(stderr,__VA_ARGS__),exit(1)
36
37
38
39 static int process1(const struct unitdesc *desc)
40 {
41         int isuser = desc->scope == unitscope_user;
42         int issystem = desc->scope == unitscope_system;
43         int issock = desc->type == unittype_socket;
44         int isserv = desc->type == unittype_service;
45         const char *name = desc->name;
46         const char *content = desc->content;
47
48 printf("\n##########################################################");
49 printf("\n### usr=%d sys=%d soc=%d srv=%d    name  %s%s", isuser, issystem, issock, isserv, name?:"?", issock?".socket":isserv?".service":"");
50 printf("\n##########################################################");
51 printf("\n%s\n\n",content);
52         return 0;
53 }
54
55 static int process(void *closure, const struct unitdesc descs[], unsigned count)
56 {
57         while (count--)
58                 process1(descs++);
59         return 0;
60 }
61
62 int main(int ac, char **av)
63 {
64         struct json_object *obj;
65         int rc;
66
67         rc = unit_generator_on(*++av);
68         if (rc < 0)
69                 error("can't read template %s: %m",*av);
70         while(*++av) {
71                 obj = wgt_path_to_json(*av);
72                 if (!obj)
73                         error("can't read widget config at %s: %m",*av);
74
75                 j_add_string_m(obj, "#metadata.install-dir", "INSTALL-DIR");
76                 j_add_string_m(obj, "#metadata.app-data-dir", "%h/app-data");
77                 j_add_string_m(obj, "#metadata.icons-dir", "ICONS-DIR");
78                 j_add_string_m(obj, "#metadata.http-port", "HTTP-PORT");
79
80                 puts(json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY));
81                 rc = unit_generator_process(obj, process, NULL);
82                 if (rc)
83                         error("can't apply generate units, error %d",rc);
84                 json_object_put(obj);
85         }
86         return 0;
87 }
88
89