22
33namespace App \Commands ;
44
5- use Illuminate \Console \Scheduling \Schedule ;
5+ use Hypernode \Api \Exception \HypernodeApiClientException ;
6+ use Hypernode \Api \Exception \HypernodeApiServerException ;
67use LaravelZero \Framework \Commands \Command ;
78use Hypernode \Api \HypernodeClientFactory ;
9+ use Symfony \Component \Console \Helper \Table ;
810
911class ListBrancherNodes extends Command
1012{
@@ -13,7 +15,7 @@ class ListBrancherNodes extends Command
1315 *
1416 * @var string
1517 */
16- protected $ signature = 'brancher:list {hypernode} ' ;
18+ protected $ signature = 'brancher:list {hypernode} {--output= : Output format (json, default: table)} ' ;
1719
1820 /**
1921 * The description of the command.
@@ -26,6 +28,8 @@ class ListBrancherNodes extends Command
2628 * Execute the console command.
2729 *
2830 * @return mixed
31+ * @throws HypernodeApiClientException
32+ * @throws HypernodeApiServerException
2933 */
3034 public function handle ()
3135 {
@@ -35,17 +39,32 @@ public function handle()
3539
3640 $ client ->brancherApp ->list ($ originHypernode );
3741
38- print_r ($ client ->brancherApp ->list ($ originHypernode ));
39- }
42+ $ branchers = $ client ->brancherApp ->list ($ originHypernode );
4043
41- /**
42- * Define the command's schedule.
43- *
44- * @param \Illuminate\Console\Scheduling\Schedule $schedule
45- * @return void
46- */
47- public function schedule (Schedule $ schedule ): void
48- {
49- // $schedule->command(static::class)->everyMinute();
44+ if ($ this ->option ('output ' ) === 'json ' ) {
45+ $ this ->line (json_encode ($ branchers , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ));
46+ return Command::SUCCESS ;
47+ }
48+
49+ $ headers = array_keys ($ branchers [array_key_first ($ branchers )]);
50+
51+ $ tableData = [];
52+ foreach ($ branchers as $ brancher ) {
53+ $ brancher ['labels ' ] = array_keys ($ brancher ['labels ' ]);
54+ foreach ($ brancher as $ key => $ value ) {
55+ if (is_array ($ value )) $ brancher [$ key ] = implode (', ' , $ value );
56+ }
57+ $ tableData [] = $ brancher ;
58+ }
59+
60+ $ table = new Table ($ this ->output );
61+ $ table
62+ ->setHeaders (array_map ('ucwords ' , $ headers ))
63+ ->setRows ($ tableData );
64+ $ table ->render ();
65+
66+ return Command::SUCCESS ;
5067 }
68+
69+
5170}
0 commit comments