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 ) {
@@ -378,6 +379,21 @@ static int set_config(const char *fmt, ...)
378379 return res ;
379380}
380381
382+ static int list_cache_server_urls (struct json_iterator * it )
383+ {
384+ const char * p ;
385+ char * q ;
386+ long l ;
387+
388+ if (it -> type == JSON_STRING &&
389+ skip_iprefix (it -> key .buf , ".CacheServers[" , & p ) &&
390+ (l = strtol (p , & q , 10 )) >= 0 && p != q &&
391+ !strcasecmp (q , "].Url" ))
392+ printf ("#%ld: %s\n" , l , it -> string_value .buf );
393+
394+ return 0 ;
395+ }
396+
381397/* Find N for which .CacheServers[N].GlobalDefault == true */
382398static int get_cache_server_index (struct json_iterator * it )
383399{
@@ -448,6 +464,18 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
448464 JSON_ITERATOR_INIT (out .buf , get_cache_server_index , & l );
449465 struct cache_server_url_data data = { .url = NULL };
450466
467+ if (!cache_server_url ) {
468+ it .fn = list_cache_server_urls ;
469+ if (iterate_json (& it ) < 0 ) {
470+ reset_iterator (& it );
471+ strbuf_release (& out );
472+ return error ("JSON parse error" );
473+ }
474+ reset_iterator (& it );
475+ strbuf_release (& out );
476+ return 0 ;
477+ }
478+
451479 if (iterate_json (& it ) < 0 ) {
452480 reset_iterator (& it );
453481 strbuf_release (& out );
@@ -468,7 +496,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
468496 return 1 ;
469497 }
470498 strbuf_release (& out );
471- return 0 ; /* error out quietly */
499+ /* error out quietly, unless we wanted to list URLs */
500+ return cache_server_url ?
501+ 0 : error (_ ("Could not access gvfs/config endpoint" ));
472502}
473503
474504static char * default_cache_root (const char * root )
@@ -1345,6 +1375,68 @@ static int cmd_version(int argc, const char **argv)
13451375 return 0 ;
13461376}
13471377
1378+ static int cmd_cache_server (int argc , const char * * argv )
1379+ {
1380+ int get = 0 ;
1381+ const char * set = NULL , * list = NULL ;
1382+ struct option options [] = {
1383+ OPT_CMDMODE (0 , "get" , & get ,
1384+ N_ ("get the configured cache-server URL" ), 1 ),
1385+ OPT_STRING (0 , "set" , & set , N_ ("URL" ),
1386+ N_ ("configure the cache-server to use" )),
1387+ OPT_STRING (0 , "list" , & list , N_ ("remote" ),
1388+ N_ ("list the possible cache-server URLs" )),
1389+ OPT_END (),
1390+ };
1391+ const char * const usage [] = {
1392+ N_ ("scalar cache-server "
1393+ "[--get | --set <url> | --list <remote>] [<enlistment>]" ),
1394+ NULL
1395+ };
1396+ int res = 0 ;
1397+
1398+ argc = parse_options (argc , argv , NULL , options ,
1399+ usage , 0 );
1400+
1401+ if (get + !!set + !!list > 1 )
1402+ usage_msg_opt (_ ("--get/--set/--list are mutually exclusive" ),
1403+ usage , options );
1404+
1405+ setup_enlistment_directory (argc , argv , usage , options , NULL );
1406+
1407+ if (list ) {
1408+ const char * name = list , * url = list ;
1409+
1410+ if (!strchr (list , '/' )) {
1411+ struct remote * remote ;
1412+
1413+ /* Look up remote */
1414+ remote = remote_get (list );
1415+ if (!remote ) {
1416+ error ("no such remote: '%s'" , name );
1417+ return 1 ;
1418+ }
1419+ if (!remote -> url .nr ) {
1420+ return error (_ ("remote '%s' has no URLs" ),
1421+ name );
1422+ }
1423+ url = remote -> url .v [0 ];
1424+ }
1425+ res = supports_gvfs_protocol (url , NULL );
1426+ } else if (set ) {
1427+ res = set_config ("gvfs.cache-server=%s" , set );
1428+ } else {
1429+ char * url = NULL ;
1430+
1431+ printf ("Using cache server: %s\n" ,
1432+ repo_config_get_string (the_repository , "gvfs.cache-server" , & url ) ?
1433+ "(undefined)" : url );
1434+ free (url );
1435+ }
1436+
1437+ return !!res ;
1438+ }
1439+
13481440static struct {
13491441 const char * name ;
13501442 int (* fn )(int , const char * * );
@@ -1359,6 +1451,7 @@ static struct {
13591451 { "help" , cmd_help },
13601452 { "version" , cmd_version },
13611453 { "diagnose" , cmd_diagnose },
1454+ { "cache-server" , cmd_cache_server },
13621455 { NULL , NULL },
13631456};
13641457
0 commit comments