2424#include "wrapper.h"
2525#include "trace2.h"
2626#include "json-parser.h"
27+ #include "remote.h"
2728#include "path.h"
2829
2930static int is_unattended (void ) {
@@ -377,6 +378,21 @@ static int set_config(const char *fmt, ...)
377378 return res ;
378379}
379380
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+
380396/* Find N for which .CacheServers[N].GlobalDefault == true */
381397static int get_cache_server_index (struct json_iterator * it )
382398{
@@ -447,6 +463,18 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
447463 JSON_ITERATOR_INIT (out .buf , get_cache_server_index , & l );
448464 struct cache_server_url_data data = { .url = NULL };
449465
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+
450478 if (iterate_json (& it ) < 0 ) {
451479 reset_iterator (& it );
452480 strbuf_release (& out );
@@ -467,7 +495,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
467495 return 1 ;
468496 }
469497 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" ));
471501}
472502
473503static char * default_cache_root (const char * root )
@@ -1339,6 +1369,68 @@ static int cmd_version(int argc, const char **argv)
13391369 return 0 ;
13401370}
13411371
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+
13421434static struct {
13431435 const char * name ;
13441436 int (* fn )(int , const char * * );
@@ -1353,6 +1445,7 @@ static struct {
13531445 { "help" , cmd_help },
13541446 { "version" , cmd_version },
13551447 { "diagnose" , cmd_diagnose },
1448+ { "cache-server" , cmd_cache_server },
13561449 { NULL , NULL },
13571450};
13581451
0 commit comments