24
24
#include "wrapper.h"
25
25
#include "trace2.h"
26
26
#include "json-parser.h"
27
+ #include "remote.h"
27
28
#include "path.h"
28
29
29
30
static int is_unattended (void ) {
@@ -377,6 +378,21 @@ static int set_config(const char *fmt, ...)
377
378
return res ;
378
379
}
379
380
381
+ static int list_cache_server_urls (struct json_iterator * it )
382
+ {
383
+ const char * p ;
384
+ char * q ;
385
+ long l ;
386
+
387
+ if (it -> type == JSON_STRING &&
388
+ skip_iprefix (it -> key .buf , ".CacheServers[" , & p ) &&
389
+ (l = strtol (p , & q , 10 )) >= 0 && p != q &&
390
+ !strcasecmp (q , "].Url" ))
391
+ printf ("#%ld: %s\n" , l , it -> string_value .buf );
392
+
393
+ return 0 ;
394
+ }
395
+
380
396
/* Find N for which .CacheServers[N].GlobalDefault == true */
381
397
static int get_cache_server_index (struct json_iterator * it )
382
398
{
@@ -447,6 +463,18 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
447
463
JSON_ITERATOR_INIT (out .buf , get_cache_server_index , & l );
448
464
struct cache_server_url_data data = { .url = NULL };
449
465
466
+ if (!cache_server_url ) {
467
+ it .fn = list_cache_server_urls ;
468
+ if (iterate_json (& it ) < 0 ) {
469
+ reset_iterator (& it );
470
+ strbuf_release (& out );
471
+ return error ("JSON parse error" );
472
+ }
473
+ reset_iterator (& it );
474
+ strbuf_release (& out );
475
+ return 0 ;
476
+ }
477
+
450
478
if (iterate_json (& it ) < 0 ) {
451
479
reset_iterator (& it );
452
480
strbuf_release (& out );
@@ -467,7 +495,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
467
495
return 1 ;
468
496
}
469
497
strbuf_release (& out );
470
- return 0 ; /* error out quietly */
498
+ /* error out quietly, unless we wanted to list URLs */
499
+ return cache_server_url ?
500
+ 0 : error (_ ("Could not access gvfs/config endpoint" ));
471
501
}
472
502
473
503
static char * default_cache_root (const char * root )
@@ -1339,6 +1369,68 @@ static int cmd_version(int argc, const char **argv)
1339
1369
return 0 ;
1340
1370
}
1341
1371
1372
+ static int cmd_cache_server (int argc , const char * * argv )
1373
+ {
1374
+ int get = 0 ;
1375
+ const char * set = NULL , * list = NULL ;
1376
+ struct option options [] = {
1377
+ OPT_CMDMODE (0 , "get" , & get ,
1378
+ N_ ("get the configured cache-server URL" ), 1 ),
1379
+ OPT_STRING (0 , "set" , & set , N_ ("URL" ),
1380
+ N_ ("configure the cache-server to use" )),
1381
+ OPT_STRING (0 , "list" , & list , N_ ("remote" ),
1382
+ N_ ("list the possible cache-server URLs" )),
1383
+ OPT_END (),
1384
+ };
1385
+ const char * const usage [] = {
1386
+ N_ ("scalar cache-server "
1387
+ "[--get | --set <url> | --list <remote>] [<enlistment>]" ),
1388
+ NULL
1389
+ };
1390
+ int res = 0 ;
1391
+
1392
+ argc = parse_options (argc , argv , NULL , options ,
1393
+ usage , 0 );
1394
+
1395
+ if (get + !!set + !!list > 1 )
1396
+ usage_msg_opt (_ ("--get/--set/--list are mutually exclusive" ),
1397
+ usage , options );
1398
+
1399
+ setup_enlistment_directory (argc , argv , usage , options , NULL );
1400
+
1401
+ if (list ) {
1402
+ const char * name = list , * url = list ;
1403
+
1404
+ if (!strchr (list , '/' )) {
1405
+ struct remote * remote ;
1406
+
1407
+ /* Look up remote */
1408
+ remote = remote_get (list );
1409
+ if (!remote ) {
1410
+ error ("no such remote: '%s'" , name );
1411
+ return 1 ;
1412
+ }
1413
+ if (!remote -> url .nr ) {
1414
+ return error (_ ("remote '%s' has no URLs" ),
1415
+ name );
1416
+ }
1417
+ url = remote -> url .v [0 ];
1418
+ }
1419
+ res = supports_gvfs_protocol (url , NULL );
1420
+ } else if (set ) {
1421
+ res = set_config ("gvfs.cache-server=%s" , set );
1422
+ } else {
1423
+ char * url = NULL ;
1424
+
1425
+ printf ("Using cache server: %s\n" ,
1426
+ repo_config_get_string (the_repository , "gvfs.cache-server" , & url ) ?
1427
+ "(undefined)" : url );
1428
+ free (url );
1429
+ }
1430
+
1431
+ return !!res ;
1432
+ }
1433
+
1342
1434
static struct {
1343
1435
const char * name ;
1344
1436
int (* fn )(int , const char * * );
@@ -1353,6 +1445,7 @@ static struct {
1353
1445
{ "help" , cmd_help },
1354
1446
{ "version" , cmd_version },
1355
1447
{ "diagnose" , cmd_diagnose },
1448
+ { "cache-server" , cmd_cache_server },
1356
1449
{ NULL , NULL },
1357
1450
};
1358
1451
0 commit comments