t1007: factor out repeated setup
[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
43         setup_git_directory();
44
45         git_configset_init(&cs);
46
47         if (argc < 2) {
48                 fprintf(stderr, "Please, provide a command name on the command-line\n");
49                 goto exit1;
50         } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
51                 if (!git_config_get_value(argv[2], &v)) {
52                         if (!v)
53                                 printf("(NULL)\n");
54                         else
55                                 printf("%s\n", v);
56                         goto exit0;
57                 } else {
58                         printf("Value not found for \"%s\"\n", argv[2]);
59                         goto exit1;
60                 }
61         } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
62                 strptr = git_config_get_value_multi(argv[2]);
63                 if (strptr) {
64                         for (i = 0; i < strptr->nr; i++) {
65                                 v = strptr->items[i].string;
66                                 if (!v)
67                                         printf("(NULL)\n");
68                                 else
69                                         printf("%s\n", v);
70                         }
71                         goto exit0;
72                 } else {
73                         printf("Value not found for \"%s\"\n", argv[2]);
74                         goto exit1;
75                 }
76         } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
77                 if (!git_config_get_int(argv[2], &val)) {
78                         printf("%d\n", val);
79                         goto exit0;
80                 } else {
81                         printf("Value not found for \"%s\"\n", argv[2]);
82                         goto exit1;
83                 }
84         } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
85                 if (!git_config_get_bool(argv[2], &val)) {
86                         printf("%d\n", val);
87                         goto exit0;
88                 } else {
89                         printf("Value not found for \"%s\"\n", argv[2]);
90                         goto exit1;
91                 }
92         } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
93                 if (!git_config_get_string_const(argv[2], &v)) {
94                         printf("%s\n", v);
95                         goto exit0;
96                 } else {
97                         printf("Value not found for \"%s\"\n", argv[2]);
98                         goto exit1;
99                 }
100         } else if (!strcmp(argv[1], "configset_get_value")) {
101                 for (i = 3; i < argc; i++) {
102                         int err;
103                         if ((err = git_configset_add_file(&cs, argv[i]))) {
104                                 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
105                                 goto exit2;
106                         }
107                 }
108                 if (!git_configset_get_value(&cs, argv[2], &v)) {
109                         if (!v)
110                                 printf("(NULL)\n");
111                         else
112                                 printf("%s\n", v);
113                         goto exit0;
114                 } else {
115                         printf("Value not found for \"%s\"\n", argv[2]);
116                         goto exit1;
117                 }
118         } else if (!strcmp(argv[1], "configset_get_value_multi")) {
119                 for (i = 3; i < argc; i++) {
120                         int err;
121                         if ((err = git_configset_add_file(&cs, argv[i]))) {
122                                 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
123                                 goto exit2;
124                         }
125                 }
126                 strptr = git_configset_get_value_multi(&cs, argv[2]);
127                 if (strptr) {
128                         for (i = 0; i < strptr->nr; i++) {
129                                 v = strptr->items[i].string;
130                                 if (!v)
131                                         printf("(NULL)\n");
132                                 else
133                                         printf("%s\n", v);
134                         }
135                         goto exit0;
136                 } else {
137                         printf("Value not found for \"%s\"\n", argv[2]);
138                         goto exit1;
139                 }
140         }
141
142         die("%s: Please check the syntax and the function name", argv[0]);
143
144 exit0:
145         git_configset_clear(&cs);
146         return 0;
147
148 exit1:
149         git_configset_clear(&cs);
150         return 1;
151
152 exit2:
153         git_configset_clear(&cs);
154         return 2;
155 }