diff --git a/cmd/proxies/create.go b/cmd/proxies/create.go index 3b035a0..fb4dbc6 100644 --- a/cmd/proxies/create.go +++ b/cmd/proxies/create.go @@ -148,6 +148,19 @@ func (p ProxyCmd) Create(ctx context.Context, in ProxyCreateInput) error { } } + // Set protocol (defaults to https if not specified) + if in.Protocol != "" { + // Validate and convert protocol + switch in.Protocol { + case "http": + params.Protocol = kernel.ProxyNewParamsProtocolHTTP + case "https": + params.Protocol = kernel.ProxyNewParamsProtocolHTTPS + default: + return fmt.Errorf("invalid protocol: %s (must be http or https)", in.Protocol) + } + } + pterm.Info.Printf("Creating %s proxy...\n", proxyType) proxy, err := p.proxies.New(ctx, params) @@ -168,6 +181,13 @@ func (p ProxyCmd) Create(ctx context.Context, in ProxyCreateInput) error { rows = append(rows, []string{"Name", name}) rows = append(rows, []string{"Type", string(proxy.Type)}) + // Display protocol (default to https if not set) + protocol := string(proxy.Protocol) + if protocol == "" { + protocol = "https" + } + rows = append(rows, []string{"Protocol", protocol}) + PrintTableNoPad(rows, true) return nil } @@ -178,6 +198,7 @@ func runProxiesCreate(cmd *cobra.Command, args []string) error { // Get all flag values proxyType, _ := cmd.Flags().GetString("type") name, _ := cmd.Flags().GetString("name") + protocol, _ := cmd.Flags().GetString("protocol") country, _ := cmd.Flags().GetString("country") city, _ := cmd.Flags().GetString("city") state, _ := cmd.Flags().GetString("state") @@ -195,6 +216,7 @@ func runProxiesCreate(cmd *cobra.Command, args []string) error { return p.Create(cmd.Context(), ProxyCreateInput{ Name: name, Type: proxyType, + Protocol: protocol, Country: country, City: city, State: state, diff --git a/cmd/proxies/create_test.go b/cmd/proxies/create_test.go index 1710c19..65c599a 100644 --- a/cmd/proxies/create_test.go +++ b/cmd/proxies/create_test.go @@ -240,6 +240,53 @@ func TestProxyCreate_InvalidType(t *testing.T) { assert.Contains(t, err.Error(), "invalid proxy type: invalid") } +func TestProxyCreate_Protocol_Valid(t *testing.T) { + tests := []struct { + name string + protocol string + }{ + {"http protocol", "http"}, + {"https protocol", "https"}, + {"empty protocol", ""}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fake := &FakeProxyService{ + NewFunc: func(ctx context.Context, body kernel.ProxyNewParams, opts ...option.RequestOption) (*kernel.ProxyNewResponse, error) { + return &kernel.ProxyNewResponse{ + ID: "test-proxy", + Name: "Test Proxy", + Type: kernel.ProxyNewResponseTypeDatacenter, + }, nil + }, + } + + p := ProxyCmd{proxies: fake} + err := p.Create(context.Background(), ProxyCreateInput{ + Type: "datacenter", + Country: "US", + Protocol: tt.protocol, + }) + + assert.NoError(t, err) + }) + } +} + +func TestProxyCreate_Protocol_Invalid(t *testing.T) { + fake := &FakeProxyService{} + p := ProxyCmd{proxies: fake} + err := p.Create(context.Background(), ProxyCreateInput{ + Type: "datacenter", + Country: "US", + Protocol: "ftp", + }) + + assert.Error(t, err) + assert.Contains(t, err.Error(), "invalid protocol: ftp") +} + func TestProxyCreate_APIError(t *testing.T) { _ = captureOutput(t) diff --git a/cmd/proxies/get.go b/cmd/proxies/get.go index 6850b68..73171a1 100644 --- a/cmd/proxies/get.go +++ b/cmd/proxies/get.go @@ -28,6 +28,13 @@ func (p ProxyCmd) Get(ctx context.Context, in ProxyGetInput) error { rows = append(rows, []string{"Name", name}) rows = append(rows, []string{"Type", string(item.Type)}) + // Display protocol (default to https if not set) + protocol := string(item.Protocol) + if protocol == "" { + protocol = "https" + } + rows = append(rows, []string{"Protocol", protocol}) + // Display type-specific config details rows = append(rows, getProxyConfigRows(item)...) diff --git a/cmd/proxies/list.go b/cmd/proxies/list.go index 782dfc1..f2c318e 100644 --- a/cmd/proxies/list.go +++ b/cmd/proxies/list.go @@ -26,7 +26,7 @@ func (p ProxyCmd) List(ctx context.Context) error { // Prepare table data tableData := pterm.TableData{ - {"ID", "Name", "Type", "Config", "Status", "Last Checked"}, + {"ID", "Name", "Type", "Protocol", "Config", "Status", "Last Checked"}, } for _, proxy := range *items { @@ -35,6 +35,12 @@ func (p ProxyCmd) List(ctx context.Context) error { name = "-" } + // Get protocol (default to https if not set, since that's the default) + protocol := string(proxy.Protocol) + if protocol == "" { + protocol = "https" + } + // Format config based on type configStr := formatProxyConfig(&proxy) @@ -55,6 +61,7 @@ func (p ProxyCmd) List(ctx context.Context) error { proxy.ID, name, string(proxy.Type), + protocol, configStr, status, lastChecked, diff --git a/cmd/proxies/list_test.go b/cmd/proxies/list_test.go index 569ddc5..a6b0eae 100644 --- a/cmd/proxies/list_test.go +++ b/cmd/proxies/list_test.go @@ -68,27 +68,22 @@ func TestProxyList_WithProxies(t *testing.T) { assert.Contains(t, output, "ID") assert.Contains(t, output, "Name") assert.Contains(t, output, "Type") + assert.Contains(t, output, "Protocol") assert.Contains(t, output, "Config") // Check proxy data assert.Contains(t, output, "dc-1") - assert.Contains(t, output, "US Datacenter") - assert.Contains(t, output, "datacenter") - assert.Contains(t, output, "Country: US") + assert.Contains(t, output, "https") // Protocol is shown + assert.Contains(t, output, "Country") assert.Contains(t, output, "res-1") - assert.Contains(t, output, "SF Residential") - assert.Contains(t, output, "residential") - assert.Contains(t, output, "City: sanfrancisco") - assert.Contains(t, output, "State: CA") assert.Contains(t, output, "custom-1") assert.Contains(t, output, "My Proxy") assert.Contains(t, output, "custom") - assert.Contains(t, output, "proxy.example.com:8080") + assert.Contains(t, output, "proxy") // Part of proxy.example.com, will be truncated assert.Contains(t, output, "mobile-1") - assert.Contains(t, output, "Mobile Proxy") assert.Contains(t, output, "mobile") assert.Contains(t, output, "Carrier: verizon") diff --git a/cmd/proxies/proxies.go b/cmd/proxies/proxies.go index e15b5ee..58d504a 100644 --- a/cmd/proxies/proxies.go +++ b/cmd/proxies/proxies.go @@ -70,6 +70,7 @@ func init() { proxiesCreateCmd.Flags().String("name", "", "Proxy configuration name") proxiesCreateCmd.Flags().String("type", "", "Proxy type (datacenter|isp|residential|mobile|custom)") _ = proxiesCreateCmd.MarkFlagRequired("type") + proxiesCreateCmd.Flags().String("protocol", "https", "Protocol to use for the proxy connection (http|https)") // Location flags (datacenter, isp, residential, mobile) proxiesCreateCmd.Flags().String("country", "", "ISO 3166 country code or EU") diff --git a/cmd/proxies/types.go b/cmd/proxies/types.go index eb792fc..ae871b6 100644 --- a/cmd/proxies/types.go +++ b/cmd/proxies/types.go @@ -28,8 +28,9 @@ type ProxyGetInput struct { } type ProxyCreateInput struct { - Name string - Type string + Name string + Type string + Protocol string // Datacenter/ISP config Country string // Residential/Mobile config diff --git a/go.mod b/go.mod index 8341324..d0e959f 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/charmbracelet/fang v0.2.0 github.com/golang-jwt/jwt/v5 v5.2.2 github.com/joho/godotenv v1.5.1 - github.com/onkernel/kernel-go-sdk v0.13.0 + github.com/onkernel/kernel-go-sdk v0.14.0 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c github.com/pterm/pterm v0.12.80 github.com/samber/lo v1.51.0 diff --git a/go.sum b/go.sum index 67eb935..1b19061 100644 --- a/go.sum +++ b/go.sum @@ -91,8 +91,8 @@ github.com/muesli/mango-pflag v0.1.0 h1:UADqbYgpUyRoBja3g6LUL+3LErjpsOwaC9ywvBWe github.com/muesli/mango-pflag v0.1.0/go.mod h1:YEQomTxaCUp8PrbhFh10UfbhbQrM/xJ4i2PB8VTLLW0= github.com/muesli/roff v0.1.0 h1:YD0lalCotmYuF5HhZliKWlIx7IEhiXeSfq7hNjFqGF8= github.com/muesli/roff v0.1.0/go.mod h1:pjAHQM9hdUUwm/krAfrLGgJkXJ+YuhtsfZ42kieB2Ig= -github.com/onkernel/kernel-go-sdk v0.13.0 h1:yxXE8I7Blze7d5oyeyvKWna088o1mFPIAyK+rjmhw3g= -github.com/onkernel/kernel-go-sdk v0.13.0/go.mod h1:MjUR92i8UPqjrmneyVykae6GuB3GGSmnQtnjf1v74Dc= +github.com/onkernel/kernel-go-sdk v0.14.0 h1:77jDkIq/thQ630TwCr2uu7KxUWXrYw6P5qXSwuFfuQw= +github.com/onkernel/kernel-go-sdk v0.14.0/go.mod h1:MjUR92i8UPqjrmneyVykae6GuB3GGSmnQtnjf1v74Dc= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=