config: add core.mode = progress pseudo-config
[git] / test-config.c
1 #include "cache.h"
2 #include "string-list.h"
3
4 /*
5  * This program exposes the C API of the configuration mechanism
6  * as a set of simple commands in order to facilitate testing.
7  *
8  * Reads stdin and prints result of command to stdout:
9  *
10  * get_value -> prints the value with highest priority for the entered key
11  *
12  * get_value_multi -> prints all values for the entered key in increasing order
13  *                   of priority
14  *
15  * get_int -> print integer value for the entered key or die
16  *
17  * get_bool -> print bool value for the entered key or die
18  *
19  * get_string -> print string value for the entered key or die
20  *
21  * configset_get_value -> returns value with the highest priority for the entered key
22  *                      from a config_set constructed from files entered as arguments.
23  *
24  * configset_get_value_multi -> returns value_list for the entered key sorted in
25  *                              ascending order of priority from a config_set
26  *                              constructed from files entered as arguments.
27  *
28  * Examples:
29  *
30  * To print the value with highest priority for key "foo.bAr Baz.rock":
31  *      test-config get_value "foo.bAr Baz.rock"
32  *
33  */
34
35
36 int main(int argc, char **argv)
37 {
38         int i, val;
39         const char *v;
40         const struct string_list *strptr;
41         struct config_set cs;
42         git_configset_init(&cs);
43
44         if (argc < 2) {
45                 fprintf(stderr, "Please, provide a command name on the command-line\n");
46                 goto exit1;
47         } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
48                 if (!git_config_get_value(argv[2], &v)) {
49                         if (!v)
50                                 printf("(NULL)\n");
51                         else
52                                 printf("%s\n", v);
53                         goto exit0;
54                 } else {
55                         printf("Value not found for \"%s\"\n", argv[2]);
56                         goto exit1;
57                 }
58         } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
59                 strptr = git_config_get_value_multi(argv[2]);
60                 if (strptr) {
61                         for (i = 0; i < strptr->nr; i++) {
62                                 v = strptr->items[i].string;
63                                 if (!v)
64                                         printf("(NULL)\n");
65                                 else
66                                         printf("%s\n", v);
67                         }
68                         goto exit0;
69                 } else {
70                         printf("Value not found for \"%s\"\n", argv[2]);
71                         goto exit1;
72                 }
73         } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
74                 if (!git_config_get_int(argv[2], &val)) {
75                         printf("%d\n", val);
76                         goto exit0;
77                 } else {
78                         printf("Value not found for \"%s\"\n", argv[2]);
79                         goto exit1;
80                 }
81         } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
82                 if (!git_config_get_bool(argv[2], &val)) {
83                         printf("%d\n", val);
84                         goto exit0;
85                 } else {
86                         printf("Value not found for \"%s\"\n", argv[2]);
87                         goto exit1;
88                 }
89         } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
90                 if (!git_config_get_string_const(argv[2], &v)) {
91                         printf("%s\n", v);
92                         goto exit0;
93                 } else {
94                         printf("Value not found for \"%s\"\n", argv[2]);
95                         goto exit1;
96                 }
97         } else if (!strcmp(argv[1], "configset_get_value")) {
98                 for (i = 3; i < argc; i++) {
99                         int err;
100                         if ((err = git_configset_add_file(&cs, argv[i]))) {
101                                 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
102                                 goto exit2;
103                         }
104                 }
105                 if (!git_configset_get_value(&cs, argv[2], &v)) {
106                         if (!v)
107                                 printf("(NULL)\n");
108                         else
109                                 printf("%s\n", v);
110                         goto exit0;
111                 } else {
112                         printf("Value not found for \"%s\"\n", argv[2]);
113                         goto exit1;
114                 }
115         } else if (!strcmp(argv[1], "configset_get_value_multi")) {
116                 for (i = 3; i < argc; i++) {
117                         int err;
118                         if ((err = git_configset_add_file(&cs, argv[i]))) {
119                                 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
120                                 goto exit2;
121                         }
122                 }
123                 strptr = git_configset_get_value_multi(&cs, argv[2]);
124                 if (strptr) {
125                         for (i = 0; i < strptr->nr; i++) {
126                                 v = strptr->items[i].string;
127                                 if (!v)
128                                         printf("(NULL)\n");
129                                 else
130                                         printf("%s\n", v);
131                         }
132                         goto exit0;
133                 } else {
134                         printf("Value not found for \"%s\"\n", argv[2]);
135                         goto exit1;
136                 }
137         }
138
139         die("%s: Please check the syntax and the function name", argv[0]);
140
141 exit0:
142         git_configset_clear(&cs);
143         return 0;
144
145 exit1:
146         git_configset_clear(&cs);
147         return 1;
148
149 exit2:
150         git_configset_clear(&cs);
151         return 2;
152 }