Skip to content

Commit bb8503f

Browse files
committed
refactor(browsers): reuse table builder for session info
1 parent dea7297 commit bb8503f

File tree

1 file changed

+18
-35
lines changed

1 file changed

+18
-35
lines changed

cmd/browsers.go

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ func (b BrowsersCmd) Create(ctx context.Context, in BrowsersCreateInput) error {
365365
}
366366

367367
func printBrowserSessionResult(sessionID, cdpURL, liveViewURL string, persistence kernel.BrowserPersistence, profile kernel.Profile) {
368+
tableData := buildBrowserTableData(sessionID, cdpURL, liveViewURL, persistence, profile)
369+
PrintTableNoPad(tableData, true)
370+
}
371+
372+
// buildBrowserTableData creates a base table with common browser session fields.
373+
func buildBrowserTableData(sessionID, cdpURL, liveViewURL string, persistence kernel.BrowserPersistence, profile kernel.Profile) pterm.TableData {
368374
tableData := pterm.TableData{
369375
{"Property", "Value"},
370376
{"Session ID", sessionID},
@@ -374,7 +380,7 @@ func printBrowserSessionResult(sessionID, cdpURL, liveViewURL string, persistenc
374380
tableData = append(tableData, []string{"Live View URL", liveViewURL})
375381
}
376382
if persistence.ID != "" {
377-
tableData = append(tableData, []string{"Persistent ID", persistence.ID})
383+
tableData = append(tableData, []string{"Persistence ID", persistence.ID})
378384
}
379385
if profile.ID != "" || profile.Name != "" {
380386
profVal := profile.Name
@@ -383,8 +389,7 @@ func printBrowserSessionResult(sessionID, cdpURL, liveViewURL string, persistenc
383389
}
384390
tableData = append(tableData, []string{"Profile", profVal})
385391
}
386-
387-
PrintTableNoPad(tableData, true)
392+
return tableData
388393
}
389394

390395
func (b BrowsersCmd) Delete(ctx context.Context, in BrowsersDeleteInput) error {
@@ -497,53 +502,31 @@ func (b BrowsersCmd) Get(ctx context.Context, in BrowsersGetInput) error {
497502
return nil
498503
}
499504

500-
// Build table with all browser details
501-
tableData := pterm.TableData{
502-
{"Property", "Value"},
503-
{"Session ID", browser.SessionID},
504-
{"Created At", util.FormatLocal(browser.CreatedAt)},
505-
{"CDP WebSocket URL", browser.CdpWsURL},
506-
}
507-
508-
if browser.BrowserLiveViewURL != "" {
509-
tableData = append(tableData, []string{"Live View URL", browser.BrowserLiveViewURL})
510-
}
505+
// Build table starting with common browser fields
506+
tableData := buildBrowserTableData(
507+
browser.SessionID,
508+
browser.CdpWsURL,
509+
browser.BrowserLiveViewURL,
510+
browser.Persistence,
511+
browser.Profile,
512+
)
511513

514+
// Append additional detailed fields
515+
tableData = append(tableData, []string{"Created At", util.FormatLocal(browser.CreatedAt)})
512516
tableData = append(tableData, []string{"Timeout (seconds)", fmt.Sprintf("%d", browser.TimeoutSeconds)})
513517
tableData = append(tableData, []string{"Headless", fmt.Sprintf("%t", browser.Headless)})
514518
tableData = append(tableData, []string{"Stealth", fmt.Sprintf("%t", browser.Stealth)})
515519
tableData = append(tableData, []string{"Kiosk Mode", fmt.Sprintf("%t", browser.KioskMode)})
516-
517-
// Viewport
518520
if browser.Viewport.Width > 0 && browser.Viewport.Height > 0 {
519521
viewportStr := fmt.Sprintf("%dx%d", browser.Viewport.Width, browser.Viewport.Height)
520522
if browser.Viewport.RefreshRate > 0 {
521523
viewportStr = fmt.Sprintf("%s@%d", viewportStr, browser.Viewport.RefreshRate)
522524
}
523525
tableData = append(tableData, []string{"Viewport", viewportStr})
524526
}
525-
526-
// Persistence
527-
if browser.Persistence.ID != "" {
528-
tableData = append(tableData, []string{"Persistence ID", browser.Persistence.ID})
529-
}
530-
531-
// Profile
532-
if browser.Profile.ID != "" || browser.Profile.Name != "" {
533-
if browser.Profile.Name != "" {
534-
tableData = append(tableData, []string{"Profile Name", browser.Profile.Name})
535-
}
536-
if browser.Profile.ID != "" {
537-
tableData = append(tableData, []string{"Profile ID", browser.Profile.ID})
538-
}
539-
}
540-
541-
// Proxy
542527
if browser.ProxyID != "" {
543528
tableData = append(tableData, []string{"Proxy ID", browser.ProxyID})
544529
}
545-
546-
// Deleted at (if soft-deleted)
547530
if !browser.DeletedAt.IsZero() {
548531
tableData = append(tableData, []string{"Deleted At", util.FormatLocal(browser.DeletedAt)})
549532
}

0 commit comments

Comments
 (0)