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
94 changes: 94 additions & 0 deletions cmd/browsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type BrowsersService interface {
Get(ctx context.Context, id string, opts ...option.RequestOption) (res *kernel.BrowserGetResponse, err error)
List(ctx context.Context, query kernel.BrowserListParams, opts ...option.RequestOption) (res *pagination.OffsetPagination[kernel.BrowserListResponse], err error)
New(ctx context.Context, body kernel.BrowserNewParams, opts ...option.RequestOption) (res *kernel.BrowserNewResponse, err error)
Update(ctx context.Context, id string, body kernel.BrowserUpdateParams, opts ...option.RequestOption) (res *kernel.BrowserUpdateResponse, err error)
Delete(ctx context.Context, body kernel.BrowserDeleteParams, opts ...option.RequestOption) (err error)
DeleteByID(ctx context.Context, id string, opts ...option.RequestOption) (err error)
LoadExtensions(ctx context.Context, id string, body kernel.BrowserLoadExtensionsParams, opts ...option.RequestOption) (err error)
Expand Down Expand Up @@ -181,6 +182,13 @@ type BrowsersGetInput struct {
Output string
}

type BrowsersUpdateInput struct {
Identifier string
ProxyID string
ClearProxy bool
Output string
}

// BrowsersCmd is a cobra-independent command handler for browsers operations.
type BrowsersCmd struct {
browsers BrowsersService
Expand Down Expand Up @@ -537,6 +545,54 @@ func (b BrowsersCmd) Get(ctx context.Context, in BrowsersGetInput) error {
return nil
}

func (b BrowsersCmd) Update(ctx context.Context, in BrowsersUpdateInput) error {
if in.Output != "" && in.Output != "json" {
return fmt.Errorf("unsupported --output value: use 'json'")
}

// Validate that at least one update option is provided
if in.ProxyID == "" && !in.ClearProxy {
return fmt.Errorf("must specify --proxy-id or --clear-proxy")
}

// Cannot specify both --proxy-id and --clear-proxy
if in.ProxyID != "" && in.ClearProxy {
return fmt.Errorf("cannot specify both --proxy-id and --clear-proxy")
}

params := kernel.BrowserUpdateParams{}
if in.ClearProxy {
// Set to empty string to remove proxy
params.ProxyID = kernel.Opt("")
} else if in.ProxyID != "" {
params.ProxyID = kernel.Opt(in.ProxyID)
}

if in.Output != "json" {
if in.ClearProxy {
pterm.Info.Printf("Removing proxy from browser %s...\n", in.Identifier)
} else {
pterm.Info.Printf("Updating browser %s with proxy %s...\n", in.Identifier, in.ProxyID)
}
}

browser, err := b.browsers.Update(ctx, in.Identifier, params)
if err != nil {
return util.CleanedUpSdkError{Err: err}
}

if in.Output == "json" {
return util.PrintPrettyJSON(browser)
}

if in.ClearProxy {
pterm.Success.Printf("Removed proxy from browser %s\n", browser.SessionID)
} else {
pterm.Success.Printf("Updated browser %s with proxy %s\n", browser.SessionID, browser.ProxyID)
}
return nil
}

// Logs
type BrowsersLogsStreamInput struct {
Identifier string
Expand Down Expand Up @@ -1833,6 +1889,22 @@ var browsersGetCmd = &cobra.Command{
RunE: runBrowsersGet,
}

var browsersUpdateCmd = &cobra.Command{
Use: "update <id>",
Short: "Update a browser session",
Long: "Update a running browser session. Currently supports changing or removing the proxy.",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("missing required argument: browser ID\n\nUsage: kernel browsers update <id> [flags]")
}
if len(args) > 1 {
return fmt.Errorf("expected 1 argument (browser ID), got %d", len(args))
}
return nil
},
RunE: runBrowsersUpdate,
}

func init() {
// list flags
browsersListCmd.Flags().StringP("output", "o", "", "Output format: json for raw API response")
Expand All @@ -1846,11 +1918,17 @@ func init() {
// view flags
browsersViewCmd.Flags().StringP("output", "o", "", "Output format: json for raw API response")

// update flags
browsersUpdateCmd.Flags().StringP("output", "o", "", "Output format: json for raw API response")
browsersUpdateCmd.Flags().String("proxy-id", "", "ID of the proxy to use for the browser session")
browsersUpdateCmd.Flags().Bool("clear-proxy", false, "Remove the proxy from the browser session")

browsersCmd.AddCommand(browsersListCmd)
browsersCmd.AddCommand(browsersCreateCmd)
browsersCmd.AddCommand(browsersDeleteCmd)
browsersCmd.AddCommand(browsersViewCmd)
browsersCmd.AddCommand(browsersGetCmd)
browsersCmd.AddCommand(browsersUpdateCmd)

// logs
logsRoot := &cobra.Command{Use: "logs", Short: "Browser logs operations"}
Expand Down Expand Up @@ -2264,6 +2342,22 @@ func runBrowsersGet(cmd *cobra.Command, args []string) error {
})
}

func runBrowsersUpdate(cmd *cobra.Command, args []string) error {
client := getKernelClient(cmd)
out, _ := cmd.Flags().GetString("output")
proxyID, _ := cmd.Flags().GetString("proxy-id")
clearProxy, _ := cmd.Flags().GetBool("clear-proxy")

svc := client.Browsers
b := BrowsersCmd{browsers: &svc}
return b.Update(cmd.Context(), BrowsersUpdateInput{
Identifier: args[0],
ProxyID: proxyID,
ClearProxy: clearProxy,
Output: out,
})
}

func runBrowsersLogsStream(cmd *cobra.Command, args []string) error {
client := getKernelClient(cmd)
svc := client.Browsers
Expand Down
8 changes: 8 additions & 0 deletions cmd/browsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type FakeBrowsersService struct {
GetFunc func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.BrowserGetResponse, error)
ListFunc func(ctx context.Context, query kernel.BrowserListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.BrowserListResponse], error)
NewFunc func(ctx context.Context, body kernel.BrowserNewParams, opts ...option.RequestOption) (*kernel.BrowserNewResponse, error)
UpdateFunc func(ctx context.Context, id string, body kernel.BrowserUpdateParams, opts ...option.RequestOption) (*kernel.BrowserUpdateResponse, error)
DeleteFunc func(ctx context.Context, body kernel.BrowserDeleteParams, opts ...option.RequestOption) error
DeleteByIDFunc func(ctx context.Context, id string, opts ...option.RequestOption) error
LoadExtensionsFunc func(ctx context.Context, id string, body kernel.BrowserLoadExtensionsParams, opts ...option.RequestOption) error
Expand All @@ -83,6 +84,13 @@ func (f *FakeBrowsersService) New(ctx context.Context, body kernel.BrowserNewPar
return &kernel.BrowserNewResponse{}, nil
}

func (f *FakeBrowsersService) Update(ctx context.Context, id string, body kernel.BrowserUpdateParams, opts ...option.RequestOption) (*kernel.BrowserUpdateResponse, error) {
if f.UpdateFunc != nil {
return f.UpdateFunc(ctx, id, body, opts...)
}
return &kernel.BrowserUpdateResponse{}, nil
}

func (f *FakeBrowsersService) Delete(ctx context.Context, body kernel.BrowserDeleteParams, opts ...option.RequestOption) error {
if f.DeleteFunc != nil {
return f.DeleteFunc(ctx, body, opts...)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1
github.com/golang-jwt/jwt/v5 v5.2.2
github.com/joho/godotenv v1.5.1
github.com/kernel/kernel-go-sdk v0.25.0
github.com/kernel/kernel-go-sdk v0.26.0
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/pterm/pterm v0.12.80
github.com/samber/lo v1.51.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kernel/kernel-go-sdk v0.25.0 h1:I6EpQKcOasiuVi6gX8HcqEIxK9dAs6xMGSnlUW7AxXY=
github.com/kernel/kernel-go-sdk v0.25.0/go.mod h1:EeZzSuHZVeHKxKCPUzxou2bovNGhXaz0RXrSqKNf1AQ=
github.com/kernel/kernel-go-sdk v0.26.0 h1:IBiEohSSZN5MEZjmnfqseT3tEip6+xg7Zxr79vJYMBA=
github.com/kernel/kernel-go-sdk v0.26.0/go.mod h1:EeZzSuHZVeHKxKCPUzxou2bovNGhXaz0RXrSqKNf1AQ=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
Expand Down