@@ -210,6 +210,18 @@ def add_deregister_args(parser):
210210 registerp .add_argument ('service_id' , help = 'The service registration id' )
211211
212212
213+ def add_services_args (parser ):
214+ """Add the services command and arguments.
215+
216+ :param argparse.Subparser parser: parser
217+
218+ """
219+ # Service registration
220+ registerp = parser .add_parser ('services' ,
221+ help = 'List services for this node' )
222+
223+ registerp .add_argument ('-i' , '--indent' , type = int , default = None , help = 'The indent level for output' )
224+
213225def parse_cli_args ():
214226 """Create the argument parser and add the arguments"""
215227 parser = argparse .ArgumentParser (description = 'CLI utilities for Consul' ,
@@ -239,6 +251,7 @@ def parse_cli_args():
239251 add_register_args (sparser )
240252 add_deregister_args (sparser )
241253 add_run_once_args (sparser )
254+ add_services_args (sparser )
242255 return parser .parse_args ()
243256
244257
@@ -570,6 +583,21 @@ def run_once(consul, args):
570583 on_error (error_msg , error_code )
571584
572585
586+ def services (consul , args ):
587+ """Dump the list of services registered with Consul
588+
589+ :param consulate.api.Consul consul: The Consul instance
590+ :param argparser.namespace args: The cli args
591+
592+ """
593+
594+ svcs = consul .agent .services ()
595+ print (json .dumps (svcs ,
596+ sort_keys = True ,
597+ indent = args .indent ,
598+ separators = (',' , ': ' )) + '\n ' )
599+
600+
573601def main ():
574602 """Entrypoint for the consulate cli application"""
575603 args = parse_cli_args ()
@@ -600,5 +628,7 @@ def main():
600628 register (consul , args )
601629 elif args .command == 'deregister' :
602630 deregister (consul , args )
631+ elif args .command == 'services' :
632+ services (consul , args )
603633 elif args .command == 'run_once' :
604634 run_once (consul , args )
0 commit comments