1616use Symfony \Component \Console \Command \Command ;
1717use Symfony \Component \Console \Helper \Table ;
1818use Symfony \Component \Console \Input \InputInterface ;
19+ use Symfony \Component \Console \Input \InputOption ;
1920use Symfony \Component \Console \Output \OutputInterface ;
2021
2122class ListDaemons extends Command {
@@ -30,6 +31,7 @@ public function __construct(
3031 protected function configure (): void {
3132 $ this ->setName ('app_api:daemon:list ' );
3233 $ this ->setDescription ('List registered daemons ' );
34+ $ this ->addOption ('output ' , 'o ' , InputOption::VALUE_REQUIRED , 'Output format (json, table) ' , 'table ' );
3335 }
3436
3537 protected function execute (InputInterface $ input , OutputInterface $ output ): int {
@@ -41,27 +43,59 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4143
4244 $ defaultDaemonName = $ this ->appConfig ->getValueString (Application::APP_ID , 'default_daemon_config ' , lazy: true );
4345
44- $ output ->writeln ('Registered ExApp daemon configs: ' );
45- $ table = new Table ($ output );
46- $ table ->setHeaders (['Def ' , 'Name ' , 'Display name ' , 'Deploy ID ' , 'Protocol ' , 'Host ' , 'NC Url ' , 'Is HaRP ' , 'HaRP FRP Address ' , 'HaRP Docker Socket Port ' ]);
47- $ rows = [];
48-
49- foreach ($ daemonConfigs as $ daemon ) {
50- $ rows [] = [
51- $ daemon ->getName () === $ defaultDaemonName ? '* ' : '' ,
52- $ daemon ->getName (), $ daemon ->getDisplayName (),
53- $ daemon ->getAcceptsDeployId (),
54- $ daemon ->getProtocol (),
55- $ daemon ->getHost (),
56- $ daemon ->getDeployConfig ()['nextcloud_url ' ],
57- isset ($ daemon ->getDeployConfig ()['harp ' ]) ? 'yes ' : 'no ' ,
58- $ daemon ->getDeployConfig ()['harp ' ]['frp_address ' ] ?? '(none) ' ,
59- $ daemon ->getDeployConfig ()['harp ' ]['docker_socket_port ' ] ?? '(none) ' ,
60- ];
46+ $ outputFormat = $ input ->getOption ('output ' );
47+ if (!in_array ($ outputFormat , ['json ' , 'table ' ], true )) {
48+ $ output ->writeln (sprintf ('<error>Invalid output format "%s". Valid options are: json, table</error> ' , $ outputFormat ));
49+ return 1 ;
6150 }
6251
63- $ table ->setRows ($ rows );
64- $ table ->render ();
52+ $ output ->writeln ('Registered ExApp daemon configs: ' );
53+
54+ if ($ outputFormat === 'json ' ) {
55+ $ allDaemonInfo = [];
56+ foreach ($ daemonConfigs as $ daemon ) {
57+ $ deployConfig = $ daemon ->getDeployConfig ();
58+
59+ if (isset ($ deployConfig ['haproxy_password ' ])) {
60+ $ deployConfig ['haproxy_password ' ] = '*** ' ;
61+ }
62+
63+ $ allDaemonInfo [] = [
64+ 'name ' => $ daemon ->getName (),
65+ 'display_name ' => $ daemon ->getDisplayName (),
66+ 'deploy_id ' => $ daemon ->getAcceptsDeployId (),
67+ 'protocol ' => $ daemon ->getProtocol (),
68+ 'host ' => $ daemon ->getHost (),
69+ 'deploy_config ' => $ deployConfig ,
70+ ];
71+
72+ }
73+ $ output ->writeln (json_encode ($ allDaemonInfo , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ));
74+ } else {
75+ $ table = new Table ($ output );
76+ $ headers = ['Def ' , 'Name ' , 'Display name ' , 'Deploy ID ' , 'Protocol ' , 'Host ' , 'NC Url ' , 'Is HaRP ' , 'HaRP FRP Address ' , 'HaRP Docker Socket Port ' ];
77+ $ table ->setHeaders ($ headers );
78+
79+ $ rows = [];
80+ foreach ($ daemonConfigs as $ daemon ) {
81+ $ deployConfig = $ daemon ->getDeployConfig ();
82+ $ rows [] = [
83+ $ daemon ->getName () === $ defaultDaemonName ? '* ' : '' ,
84+ $ daemon ->getName (),
85+ $ daemon ->getDisplayName (),
86+ $ daemon ->getAcceptsDeployId (),
87+ $ daemon ->getProtocol (),
88+ $ daemon ->getHost (),
89+ $ deployConfig ['nextcloud_url ' ],
90+ isset ($ deployConfig ['harp ' ]) ? 'yes ' : 'no ' ,
91+ $ deployConfig ['harp ' ]['frp_address ' ] ?? '(none) ' ,
92+ $ deployConfig ['harp ' ]['docker_socket_port ' ] ?? '(none) ' ,
93+ ];
94+ }
95+
96+ $ table ->setRows ($ rows );
97+ $ table ->render ();
98+ }
6599
66100 return 0 ;
67101 }
0 commit comments