Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions cmd/app.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -107,12 +106,7 @@ func runAppList(cmd *cobra.Command, args []string) error {
fmt.Println("[]")
return nil
}
bs, err := json.MarshalIndent(apps.Items, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSONSlice(apps.Items)
}

if apps == nil || len(apps.Items) == 0 {
Expand Down Expand Up @@ -242,12 +236,7 @@ func runAppHistory(cmd *cobra.Command, args []string) error {
fmt.Println("[]")
return nil
}
bs, err := json.MarshalIndent(deployments.Items, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSONSlice(deployments.Items)
}

if deployments == nil || len(deployments.Items) == 0 {
Expand Down
38 changes: 6 additions & 32 deletions cmd/browser_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -44,16 +43,11 @@ func (c BrowserPoolsCmd) List(ctx context.Context, in BrowserPoolsListInput) err
}

if in.Output == "json" {
if pools == nil {
if pools == nil || len(*pools) == 0 {
fmt.Println("[]")
return nil
}
bs, err := json.MarshalIndent(*pools, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSONSlice(*pools)
}

if pools == nil || len(*pools) == 0 {
Expand Down Expand Up @@ -155,12 +149,7 @@ func (c BrowserPoolsCmd) Create(ctx context.Context, in BrowserPoolsCreateInput)
}

if in.Output == "json" {
bs, err := json.MarshalIndent(pool, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(pool)
}

if pool.Name != "" {
Expand All @@ -187,12 +176,7 @@ func (c BrowserPoolsCmd) Get(ctx context.Context, in BrowserPoolsGetInput) error
}

if in.Output == "json" {
bs, err := json.MarshalIndent(pool, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(pool)
}

cfg := pool.BrowserPoolConfig
Expand Down Expand Up @@ -301,12 +285,7 @@ func (c BrowserPoolsCmd) Update(ctx context.Context, in BrowserPoolsUpdateInput)
}

if in.Output == "json" {
bs, err := json.MarshalIndent(pool, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(pool)
}

if pool.Name != "" {
Expand Down Expand Up @@ -364,12 +343,7 @@ func (c BrowserPoolsCmd) Acquire(ctx context.Context, in BrowserPoolsAcquireInpu
}

if in.Output == "json" {
bs, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(resp)
}

tableData := pterm.TableData{
Expand Down
84 changes: 17 additions & 67 deletions cmd/browsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,7 @@ func (b BrowsersCmd) List(ctx context.Context, in BrowsersListInput) error {
}

if in.Output == "json" {
if len(browsers) == 0 {
fmt.Println("[]")
return nil
}
bs, err := json.MarshalIndent(browsers, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSONSlice(browsers)
}

if len(browsers) == 0 {
Expand Down Expand Up @@ -371,12 +362,7 @@ func (b BrowsersCmd) Create(ctx context.Context, in BrowsersCreateInput) error {
}

if in.Output == "json" {
bs, err := json.MarshalIndent(browser, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(browser)
}

printBrowserSessionResult(browser.SessionID, browser.CdpWsURL, browser.BrowserLiveViewURL, browser.Persistence, browser.Profile)
Expand Down Expand Up @@ -482,9 +468,13 @@ func (b BrowsersCmd) View(ctx context.Context, in BrowsersViewInput) error {
}

if in.Output == "json" {
result := map[string]string{"liveViewUrl": browser.BrowserLiveViewURL}
bs, _ := json.MarshalIndent(result, "", " ")
fmt.Println(string(bs))
// View command returns a custom response, not the full browser object
// Use json.Marshal to ensure proper JSON escaping of the URL
urlBytes, err := json.Marshal(browser.BrowserLiveViewURL)
if err != nil {
return err
}
fmt.Printf("{\n \"liveViewUrl\": %s\n}\n", urlBytes)
return nil
}

Expand All @@ -511,12 +501,7 @@ func (b BrowsersCmd) Get(ctx context.Context, in BrowsersGetInput) error {
return util.CleanedUpSdkError{Err: err}
}
if in.Output == "json" {
bs, err := json.MarshalIndent(browser, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(browser)
}

// Build table starting with common browser fields
Expand Down Expand Up @@ -922,12 +907,7 @@ func (b BrowsersCmd) ReplaysList(ctx context.Context, in BrowsersReplaysListInpu
fmt.Println("[]")
return nil
}
bs, err := json.MarshalIndent(*items, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSONSlice(*items)
}

if items == nil || len(*items) == 0 {
Expand Down Expand Up @@ -964,12 +944,7 @@ func (b BrowsersCmd) ReplaysStart(ctx context.Context, in BrowsersReplaysStartIn
}

if in.Output == "json" {
bs, err := json.MarshalIndent(res, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(res)
}

rows := pterm.TableData{{"Property", "Value"}, {"Replay ID", res.ReplayID}, {"View URL", res.ReplayViewURL}, {"Started At", util.FormatLocal(res.StartedAt)}}
Expand Down Expand Up @@ -1148,12 +1123,7 @@ func (b BrowsersCmd) ProcessExec(ctx context.Context, in BrowsersProcessExecInpu
}

if in.Output == "json" {
bs, err := json.MarshalIndent(res, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(res)
}

rows := pterm.TableData{{"Property", "Value"}, {"Exit Code", fmt.Sprintf("%d", res.ExitCode)}, {"Duration (ms)", fmt.Sprintf("%d", res.DurationMs)}}
Expand Down Expand Up @@ -1220,12 +1190,7 @@ func (b BrowsersCmd) ProcessSpawn(ctx context.Context, in BrowsersProcessSpawnIn
}

if in.Output == "json" {
bs, err := json.MarshalIndent(res, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(res)
}

rows := pterm.TableData{{"Property", "Value"}, {"Process ID", res.ProcessID}, {"PID", fmt.Sprintf("%d", res.Pid)}, {"Started At", util.FormatLocal(res.StartedAt)}}
Expand Down Expand Up @@ -1508,12 +1473,7 @@ func (b BrowsersCmd) FSFileInfo(ctx context.Context, in BrowsersFSFileInfoInput)
}

if in.Output == "json" {
bs, err := json.MarshalIndent(res, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(res)
}

rows := pterm.TableData{{"Property", "Value"}, {"Path", res.Path}, {"Name", res.Name}, {"Mode", res.Mode}, {"IsDir", fmt.Sprintf("%t", res.IsDir)}, {"SizeBytes", fmt.Sprintf("%d", res.SizeBytes)}, {"ModTime", util.FormatLocal(res.ModTime)}}
Expand Down Expand Up @@ -1544,12 +1504,7 @@ func (b BrowsersCmd) FSListFiles(ctx context.Context, in BrowsersFSListFilesInpu
fmt.Println("[]")
return nil
}
bs, err := json.MarshalIndent(*res, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSONSlice(*res)
}

if res == nil || len(*res) == 0 {
Expand Down Expand Up @@ -2227,12 +2182,7 @@ func runBrowsersCreate(cmd *cobra.Command, args []string) error {
return nil
}
if output == "json" {
bs, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(resp)
}
printBrowserSessionResult(resp.SessionID, resp.CdpWsURL, resp.BrowserLiveViewURL, resp.Persistence, resp.Profile)
return nil
Expand Down
11 changes: 7 additions & 4 deletions cmd/browsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,13 @@ func TestBrowsersGet_JSONOutput(t *testing.T) {

fake := &FakeBrowsersService{
GetFunc: func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.BrowserGetResponse, error) {
return &kernel.BrowserGetResponse{
SessionID: "sess-json",
CdpWsURL: "ws://cdp",
}, nil
// Unmarshal JSON to populate RawJSON() properly
jsonData := `{"session_id": "sess-json", "cdp_ws_url": "ws://cdp", "created_at": "2024-01-01T00:00:00Z", "headless": false, "stealth": false, "timeout_seconds": 60}`
var resp kernel.BrowserGetResponse
if err := json.Unmarshal([]byte(jsonData), &resp); err != nil {
t.Fatalf("failed to unmarshal test response: %v", err)
}
return &resp, nil
},
}
b := BrowsersCmd{browsers: fake}
Expand Down
7 changes: 1 addition & 6 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,7 @@ func runDeployHistory(cmd *cobra.Command, args []string) error {
fmt.Println("[]")
return nil
}
bs, err := json.MarshalIndent(deployments.Items, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSONSlice(deployments.Items)
}

if deployments == nil || len(deployments.Items) == 0 {
Expand Down
15 changes: 2 additions & 13 deletions cmd/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -76,12 +75,7 @@ func (e ExtensionsCmd) List(ctx context.Context, in ExtensionsListInput) error {
fmt.Println("[]")
return nil
}
bs, err := json.MarshalIndent(*items, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSONSlice(*items)
}

if items == nil || len(*items) == 0 {
Expand Down Expand Up @@ -325,12 +319,7 @@ func (e ExtensionsCmd) Upload(ctx context.Context, in ExtensionsUploadInput) err
}

if in.Output == "json" {
bs, err := json.MarshalIndent(item, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSON(item)
}

name := item.Name
Expand Down
7 changes: 1 addition & 6 deletions cmd/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,7 @@ func runInvocationHistory(cmd *cobra.Command, args []string) error {
fmt.Println("[]")
return nil
}
bs, err := json.MarshalIndent(invocations.Items, "", " ")
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
return util.PrintPrettyJSONSlice(invocations.Items)
}

table := pterm.TableData{{"Invocation ID", "App Name", "Action", "Version", "Status", "Started At", "Duration", "Output"}}
Expand Down
Loading