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 * configset_get_value -> returns value with the highest priority for the entered key 20 * from a config_set constructed from files entered as arguments. 21 * 22 * configset_get_value_multi -> returns value_list for the entered key sorted in 23 * ascending order of priority from a config_set 24 * constructed from files entered as arguments. 25 * 26 * Examples: 27 * 28 * To print the value with highest priority for key "foo.bAr Baz.rock": 29 * test-config get_value "foo.bAr Baz.rock" 30 * 31 */ 32 33 34intmain(int argc,char**argv) 35{ 36int i, val; 37const char*v; 38const struct string_list *strptr; 39struct config_set cs; 40git_configset_init(&cs); 41 42if(argc <2) { 43fprintf(stderr,"Please, provide a command name on the command-line\n"); 44goto exit1; 45}else if(argc ==3&& !strcmp(argv[1],"get_value")) { 46if(!git_config_get_value(argv[2], &v)) { 47if(!v) 48printf("(NULL)\n"); 49else 50printf("%s\n", v); 51goto exit0; 52}else{ 53printf("Value not found for\"%s\"\n", argv[2]); 54goto exit1; 55} 56}else if(argc ==3&& !strcmp(argv[1],"get_value_multi")) { 57 strptr =git_config_get_value_multi(argv[2]); 58if(strptr) { 59for(i =0; i < strptr->nr; i++) { 60 v = strptr->items[i].string; 61if(!v) 62printf("(NULL)\n"); 63else 64printf("%s\n", v); 65} 66goto exit0; 67}else{ 68printf("Value not found for\"%s\"\n", argv[2]); 69goto exit1; 70} 71}else if(argc ==3&& !strcmp(argv[1],"get_int")) { 72if(!git_config_get_int(argv[2], &val)) { 73printf("%d\n", val); 74goto exit0; 75}else{ 76printf("Value not found for\"%s\"\n", argv[2]); 77goto exit1; 78} 79}else if(argc ==3&& !strcmp(argv[1],"get_bool")) { 80if(!git_config_get_bool(argv[2], &val)) { 81printf("%d\n", val); 82goto exit0; 83}else{ 84printf("Value not found for\"%s\"\n", argv[2]); 85goto exit1; 86} 87}else if(!strcmp(argv[1],"configset_get_value")) { 88for(i =3; i < argc; i++) { 89int err; 90if((err =git_configset_add_file(&cs, argv[i]))) { 91fprintf(stderr,"Error (%d) reading configuration file%s.\n", err, argv[i]); 92goto exit2; 93} 94} 95if(!git_configset_get_value(&cs, argv[2], &v)) { 96if(!v) 97printf("(NULL)\n"); 98else 99printf("%s\n", v); 100goto exit0; 101}else{ 102printf("Value not found for\"%s\"\n", argv[2]); 103goto exit1; 104} 105}else if(!strcmp(argv[1],"configset_get_value_multi")) { 106for(i =3; i < argc; i++) { 107int err; 108if((err =git_configset_add_file(&cs, argv[i]))) { 109fprintf(stderr,"Error (%d) reading configuration file%s.\n", err, argv[i]); 110goto exit2; 111} 112} 113 strptr =git_configset_get_value_multi(&cs, argv[2]); 114if(strptr) { 115for(i =0; i < strptr->nr; i++) { 116 v = strptr->items[i].string; 117if(!v) 118printf("(NULL)\n"); 119else 120printf("%s\n", v); 121} 122goto exit0; 123}else{ 124printf("Value not found for\"%s\"\n", argv[2]); 125goto exit1; 126} 127} 128 129die("%s: Please check the syntax and the function name", argv[0]); 130 131exit0: 132git_configset_clear(&cs); 133return0; 134 135exit1: 136git_configset_clear(&cs); 137return1; 138 139exit2: 140git_configset_clear(&cs); 141return2; 142}