590907d8fa225bf066b0fca8f747572d4ad19762
[src/app-framework-main.git] / src / tests / test-unit / test-unit.c
1 /*
2  Copyright (C) 2016-2019 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 processunit(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,
50                         isserv, name?:"?", issock?".socket":isserv?".service":"");
51 printf("\n##########################################################");
52 printf("\n%s\n\n",content);
53         return 0;
54 }
55
56 static int process(void *closure, const struct generatedesc *desc)
57 {
58         int i;
59 printf("\n##########################################################");
60 printf("\n###### J S O N D E S C    AFTER                    #######");
61 printf("\n##########################################################");
62 puts(json_object_to_json_string_ext(desc->desc, JSON_C_TO_STRING_PRETTY));
63         for (i = 0 ; i < desc->nunits ; i++)
64                 processunit(&desc->units[i]);
65         return 0;
66 }
67
68 static int port()
69 {
70         static int r = 10000;
71         return r++;
72 }
73
74 int main(int ac, char **av)
75 {
76         struct unitconf conf;
77         struct json_object *obj;
78         int rc;
79
80         conf.installdir = "INSTALL-DIR";
81         conf.icondir = "ICONS-DIR";
82         conf.port = port;
83         rc = unit_generator_open_template(*++av);
84         if (rc < 0)
85                 error("can't read template %s: %m",*av);
86         while(*++av) {
87                 obj = wgt_path_to_json(*av);
88                 if (!obj)
89                         error("can't read widget config at %s: %m",*av);
90
91 printf("\n##########################################################");
92 printf("\n###### J S O N D E S C    BEFORE                   #######");
93 printf("\n##########################################################");
94 puts(json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY));
95                 rc = unit_generator_process(obj, &conf, process, NULL);
96                 if (rc)
97                         error("can't apply generate units, error %d",rc);
98                 json_object_put(obj);
99         }
100         return 0;
101 }
102
103