Skip to content
Closed
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
20 changes: 13 additions & 7 deletions cmd/browser_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"github.com/kernel/cli/pkg/util"
"github.com/kernel/kernel-go-sdk"
"github.com/kernel/kernel-go-sdk/option"
"github.com/kernel/kernel-go-sdk/packages/pagination"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

// BrowserPoolsService defines the subset of the Kernel SDK browser pools client that we use.
type BrowserPoolsService interface {
List(ctx context.Context, opts ...option.RequestOption) (res *[]kernel.BrowserPool, err error)
List(ctx context.Context, query kernel.BrowserPoolListParams, opts ...option.RequestOption) (res *pagination.OffsetPagination[kernel.BrowserPool], err error)
New(ctx context.Context, body kernel.BrowserPoolNewParams, opts ...option.RequestOption) (res *kernel.BrowserPool, err error)
Get(ctx context.Context, id string, opts ...option.RequestOption) (res *kernel.BrowserPool, err error)
Update(ctx context.Context, id string, body kernel.BrowserPoolUpdateParams, opts ...option.RequestOption) (res *kernel.BrowserPool, err error)
Expand All @@ -37,20 +38,25 @@ func (c BrowserPoolsCmd) List(ctx context.Context, in BrowserPoolsListInput) err
return err
}

pools, err := c.client.List(ctx)
page, err := c.client.List(ctx, kernel.BrowserPoolListParams{})
if err != nil {
return util.CleanedUpSdkError{Err: err}
}

var pools []kernel.BrowserPool
if page != nil {
pools = page.Items
}

if in.Output == "json" {
if pools == nil || len(*pools) == 0 {
if len(pools) == 0 {
fmt.Println("[]")
return nil
}
return util.PrintPrettyJSONSlice(*pools)
return util.PrintPrettyJSONSlice(pools)
}

if pools == nil || len(*pools) == 0 {
if len(pools) == 0 {
pterm.Info.Println("No browser pools found")
return nil
}
Expand All @@ -59,7 +65,7 @@ func (c BrowserPoolsCmd) List(ctx context.Context, in BrowserPoolsListInput) err
{"ID", "Name", "Available", "Acquired", "Created At", "Size"},
}

for _, p := range *pools {
for _, p := range pools {
tableData = append(tableData, []string{
p.ID,
util.OrDash(p.Name),
Expand Down Expand Up @@ -250,7 +256,7 @@ func (c BrowserPoolsCmd) Update(ctx context.Context, in BrowserPoolsUpdateInput)
params.Name = kernel.String(in.Name)
}
if in.Size > 0 {
params.Size = in.Size
params.Size = kernel.Int(in.Size)
}
if in.FillRate > 0 {
params.FillRatePerMinute = kernel.Int(in.FillRate)
Expand Down
18 changes: 12 additions & 6 deletions cmd/credential_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/kernel/cli/pkg/util"
"github.com/kernel/kernel-go-sdk"
"github.com/kernel/kernel-go-sdk/option"
"github.com/kernel/kernel-go-sdk/packages/pagination"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
Expand All @@ -17,7 +18,7 @@ type CredentialProvidersService interface {
New(ctx context.Context, body kernel.CredentialProviderNewParams, opts ...option.RequestOption) (res *kernel.CredentialProvider, err error)
Get(ctx context.Context, id string, opts ...option.RequestOption) (res *kernel.CredentialProvider, err error)
Update(ctx context.Context, id string, body kernel.CredentialProviderUpdateParams, opts ...option.RequestOption) (res *kernel.CredentialProvider, err error)
List(ctx context.Context, opts ...option.RequestOption) (res *[]kernel.CredentialProvider, err error)
List(ctx context.Context, query kernel.CredentialProviderListParams, opts ...option.RequestOption) (res *pagination.OffsetPagination[kernel.CredentialProvider], err error)
Delete(ctx context.Context, id string, opts ...option.RequestOption) (err error)
Test(ctx context.Context, id string, opts ...option.RequestOption) (res *kernel.CredentialProviderTestResult, err error)
ListItems(ctx context.Context, id string, opts ...option.RequestOption) (res *kernel.CredentialProviderListItemsResponse, err error)
Expand Down Expand Up @@ -75,26 +76,31 @@ func (c CredentialProvidersCmd) List(ctx context.Context, in CredentialProviders
return err
}

providers, err := c.providers.List(ctx)
page, err := c.providers.List(ctx, kernel.CredentialProviderListParams{})
if err != nil {
return util.CleanedUpSdkError{Err: err}
}

var providers []kernel.CredentialProvider
if page != nil {
providers = page.Items
}

if in.Output == "json" {
if providers == nil || len(*providers) == 0 {
if len(providers) == 0 {
fmt.Println("[]")
return nil
}
return util.PrintPrettyJSONSlice(*providers)
return util.PrintPrettyJSONSlice(providers)
}

if providers == nil || len(*providers) == 0 {
if len(providers) == 0 {
pterm.Info.Println("No credential providers found")
return nil
}

tableData := pterm.TableData{{"ID", "Provider Type", "Enabled", "Priority", "Created At"}}
for _, p := range *providers {
for _, p := range providers {
tableData = append(tableData, []string{
p.ID,
string(p.ProviderType),
Expand Down
18 changes: 12 additions & 6 deletions cmd/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/kernel/cli/pkg/util"
"github.com/kernel/kernel-go-sdk"
"github.com/kernel/kernel-go-sdk/option"
"github.com/kernel/kernel-go-sdk/packages/pagination"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -43,7 +44,7 @@ var defaultExtensionExclusions = util.ZipOptions{

// ExtensionsService defines the subset of the Kernel SDK extension client that we use.
type ExtensionsService interface {
List(ctx context.Context, opts ...option.RequestOption) (res *[]kernel.ExtensionListResponse, err error)
List(ctx context.Context, query kernel.ExtensionListParams, opts ...option.RequestOption) (res *pagination.OffsetPagination[kernel.ExtensionListResponse], err error)
Delete(ctx context.Context, idOrName string, opts ...option.RequestOption) (err error)
Download(ctx context.Context, idOrName string, opts ...option.RequestOption) (res *http.Response, err error)
DownloadFromChromeStore(ctx context.Context, query kernel.ExtensionDownloadFromChromeStoreParams, opts ...option.RequestOption) (res *http.Response, err error)
Expand Down Expand Up @@ -89,25 +90,30 @@ func (e ExtensionsCmd) List(ctx context.Context, in ExtensionsListInput) error {
if in.Output != "json" {
pterm.Info.Println("Fetching extensions...")
}
items, err := e.extensions.List(ctx)
page, err := e.extensions.List(ctx, kernel.ExtensionListParams{})
if err != nil {
return util.CleanedUpSdkError{Err: err}
}

var items []kernel.ExtensionListResponse
if page != nil {
items = page.Items
}

if in.Output == "json" {
if items == nil || len(*items) == 0 {
if len(items) == 0 {
fmt.Println("[]")
return nil
}
return util.PrintPrettyJSONSlice(*items)
return util.PrintPrettyJSONSlice(items)
}

if items == nil || len(*items) == 0 {
if len(items) == 0 {
pterm.Info.Println("No extensions found")
return nil
}
rows := pterm.TableData{{"Extension ID", "Name", "Created At", "Size (bytes)", "Last Used At"}}
for _, it := range *items {
for _, it := range items {
name := it.Name
if name == "" {
name = "-"
Expand Down
14 changes: 7 additions & 7 deletions cmd/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ import (

"github.com/kernel/kernel-go-sdk"
"github.com/kernel/kernel-go-sdk/option"
"github.com/kernel/kernel-go-sdk/packages/pagination"
"github.com/stretchr/testify/assert"
)

// FakeExtensionsService implements ExtensionsService
type FakeExtensionsService struct {
ListFunc func(ctx context.Context, opts ...option.RequestOption) (*[]kernel.ExtensionListResponse, error)
ListFunc func(ctx context.Context, query kernel.ExtensionListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ExtensionListResponse], error)
DeleteFunc func(ctx context.Context, idOrName string, opts ...option.RequestOption) error
DownloadFunc func(ctx context.Context, idOrName string, opts ...option.RequestOption) (*http.Response, error)
DownloadFromChromeStoreFn func(ctx context.Context, query kernel.ExtensionDownloadFromChromeStoreParams, opts ...option.RequestOption) (*http.Response, error)
UploadFunc func(ctx context.Context, body kernel.ExtensionUploadParams, opts ...option.RequestOption) (*kernel.ExtensionUploadResponse, error)
}

func (f *FakeExtensionsService) List(ctx context.Context, opts ...option.RequestOption) (*[]kernel.ExtensionListResponse, error) {
func (f *FakeExtensionsService) List(ctx context.Context, query kernel.ExtensionListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ExtensionListResponse], error) {
if f.ListFunc != nil {
return f.ListFunc(ctx, opts...)
return f.ListFunc(ctx, query, opts...)
}
empty := []kernel.ExtensionListResponse{}
return &empty, nil
return &pagination.OffsetPagination[kernel.ExtensionListResponse]{}, nil
}
func (f *FakeExtensionsService) Delete(ctx context.Context, idOrName string, opts ...option.RequestOption) error {
if f.DeleteFunc != nil {
Expand Down Expand Up @@ -70,8 +70,8 @@ func TestExtensionsList_WithRows(t *testing.T) {
buf := capturePtermOutput(t)
created := time.Unix(0, 0)
rows := []kernel.ExtensionListResponse{{ID: "e1", Name: "alpha", CreatedAt: created, SizeBytes: 10}, {ID: "e2", Name: "", CreatedAt: created, SizeBytes: 20}}
fake := &FakeExtensionsService{ListFunc: func(ctx context.Context, opts ...option.RequestOption) (*[]kernel.ExtensionListResponse, error) {
return &rows, nil
fake := &FakeExtensionsService{ListFunc: func(ctx context.Context, query kernel.ExtensionListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ExtensionListResponse], error) {
return &pagination.OffsetPagination[kernel.ExtensionListResponse]{Items: rows}, nil
}}
e := ExtensionsCmd{extensions: fake}
_ = e.List(context.Background(), ExtensionsListInput{})
Expand Down
3 changes: 0 additions & 3 deletions cmd/proxies/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ func getProxyCheckConfigRows(proxy *kernel.ProxyCheckResponse) [][]string {
if config.Asn != "" {
rows = append(rows, []string{"ASN", config.Asn})
}
if config.Carrier != "" {
rows = append(rows, []string{"Carrier", config.Carrier})
}
case kernel.ProxyCheckResponseTypeCustom:
if config.Host != "" {
rows = append(rows, []string{"Host", config.Host})
Expand Down
15 changes: 10 additions & 5 deletions cmd/proxies/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/kernel/kernel-go-sdk"
"github.com/kernel/kernel-go-sdk/option"
"github.com/kernel/kernel-go-sdk/packages/pagination"
"github.com/pterm/pterm"
)

Expand Down Expand Up @@ -37,19 +38,23 @@ func captureOutput(t *testing.T) *bytes.Buffer {

// FakeProxyService implements ProxyService for testing
type FakeProxyService struct {
ListFunc func(ctx context.Context, opts ...option.RequestOption) (*[]kernel.ProxyListResponse, error)
ListFunc func(ctx context.Context, query kernel.ProxyListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ProxyListResponse], error)
GetFunc func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ProxyGetResponse, error)
NewFunc func(ctx context.Context, body kernel.ProxyNewParams, opts ...option.RequestOption) (*kernel.ProxyNewResponse, error)
DeleteFunc func(ctx context.Context, id string, opts ...option.RequestOption) error
CheckFunc func(ctx context.Context, id string, body kernel.ProxyCheckParams, opts ...option.RequestOption) (*kernel.ProxyCheckResponse, error)
}

func (f *FakeProxyService) List(ctx context.Context, opts ...option.RequestOption) (*[]kernel.ProxyListResponse, error) {
func (f *FakeProxyService) List(ctx context.Context, query kernel.ProxyListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ProxyListResponse], error) {
if f.ListFunc != nil {
return f.ListFunc(ctx, opts...)
return f.ListFunc(ctx, query, opts...)
}
empty := []kernel.ProxyListResponse{}
return &empty, nil
return &pagination.OffsetPagination[kernel.ProxyListResponse]{}, nil
}

// proxyListPage wraps proxies in an OffsetPagination page for test fakes.
func proxyListPage(proxies []kernel.ProxyListResponse) *pagination.OffsetPagination[kernel.ProxyListResponse] {
return &pagination.OffsetPagination[kernel.ProxyListResponse]{Items: proxies}
}

func (f *FakeProxyService) Get(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ProxyGetResponse, error) {
Expand Down
32 changes: 10 additions & 22 deletions cmd/proxies/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,25 @@ func (p ProxyCmd) Create(ctx context.Context, in ProxyCreateInput) error {
// Build config based on type
switch proxyType {
case kernel.ProxyNewParamsTypeDatacenter:
config := kernel.ProxyNewParamsConfigDatacenterProxyConfig{}
config := kernel.ProxyNewParamsConfigDatacenter{}
if in.Country != "" {
config.Country = kernel.Opt(in.Country)
}
params.Config = kernel.ProxyNewParamsConfigUnion{
OfProxyNewsConfigDatacenterProxyConfig: &config,
OfDatacenter: &config,
}

case kernel.ProxyNewParamsTypeIsp:
config := kernel.ProxyNewParamsConfigIspProxyConfig{}
config := kernel.ProxyNewParamsConfigIsp{}
if in.Country != "" {
config.Country = kernel.Opt(in.Country)
}
params.Config = kernel.ProxyNewParamsConfigUnion{
OfProxyNewsConfigIspProxyConfig: &config,
OfIsp: &config,
}

case kernel.ProxyNewParamsTypeResidential:
config := kernel.ProxyNewParamsConfigResidentialProxyConfig{}
config := kernel.ProxyNewParamsConfigResidential{}

// Validate that if city is provided, country must also be provided
if in.City != "" && in.Country == "" {
Expand Down Expand Up @@ -98,11 +98,11 @@ func (p ProxyCmd) Create(ctx context.Context, in ProxyCreateInput) error {
}
}
params.Config = kernel.ProxyNewParamsConfigUnion{
OfProxyNewsConfigResidentialProxyConfig: &config,
OfResidential: &config,
}

case kernel.ProxyNewParamsTypeMobile:
config := kernel.ProxyNewParamsConfigMobileProxyConfig{}
config := kernel.ProxyNewParamsConfigMobile{}

// Validate that if city is provided, country must also be provided
if in.City != "" && in.Country == "" {
Expand All @@ -118,18 +118,8 @@ func (p ProxyCmd) Create(ctx context.Context, in ProxyCreateInput) error {
if in.State != "" {
config.State = kernel.Opt(in.State)
}
if in.Zip != "" {
config.Zip = kernel.Opt(in.Zip)
}
if in.ASN != "" {
config.Asn = kernel.Opt(in.ASN)
}
if in.Carrier != "" {
// The API will validate the carrier value
config.Carrier = in.Carrier
}
params.Config = kernel.ProxyNewParamsConfigUnion{
OfProxyNewsConfigMobileProxyConfig: &config,
OfMobile: &config,
}

case kernel.ProxyNewParamsTypeCustom:
Expand All @@ -140,7 +130,7 @@ func (p ProxyCmd) Create(ctx context.Context, in ProxyCreateInput) error {
return fmt.Errorf("--port is required for custom proxy type")
}

config := kernel.ProxyNewParamsConfigCreateCustomProxyConfig{
config := kernel.ProxyNewParamsConfigCustom{
Host: in.Host,
Port: int64(in.Port),
}
Expand All @@ -151,7 +141,7 @@ func (p ProxyCmd) Create(ctx context.Context, in ProxyCreateInput) error {
config.Password = kernel.Opt(in.Password)
}
params.Config = kernel.ProxyNewParamsConfigUnion{
OfProxyNewsConfigCreateCustomProxyConfig: &config,
OfCustom: &config,
}
}

Expand Down Expand Up @@ -219,7 +209,6 @@ func runProxiesCreate(cmd *cobra.Command, args []string) error {
zip, _ := cmd.Flags().GetString("zip")
asn, _ := cmd.Flags().GetString("asn")
os, _ := cmd.Flags().GetString("os")
carrier, _ := cmd.Flags().GetString("carrier")
host, _ := cmd.Flags().GetString("host")
port, _ := cmd.Flags().GetInt("port")
username, _ := cmd.Flags().GetString("username")
Expand All @@ -241,7 +230,6 @@ func runProxiesCreate(cmd *cobra.Command, args []string) error {
Zip: zip,
ASN: asn,
OS: os,
Carrier: carrier,
Host: host,
Port: port,
Username: username,
Expand Down
Loading
Loading