Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

refactor(cli): remove dead legacy action functions and tabs sub-router

Bosch68b380b7a92c
diff --git a/cmd/pinchtab/cmd_cli.go b/cmd/pinchtab/cmd_cli.go
index 7f272996..3f816e42 100644
--- a/cmd/pinchtab/cmd_cli.go
+++ b/cmd/pinchtab/cmd_cli.go
@@ -31,7 +31,7 @@ var navCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.NavigateWithFlags(client, base, token, args[0], cmd)
+ browseractions.Navigate(client, base, token, args[0], cmd)
})
},
}
@@ -42,7 +42,7 @@ var snapCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.SnapshotWithFlags(client, base, token, cmd)
+ browseractions.Snapshot(client, base, token, cmd)
})
},
}
@@ -58,7 +58,7 @@ var clickCmd = &cobra.Command{
}
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.ActionWithFlags(client, base, token, "click", ref, cmd)
+ browseractions.Action(client, base, token, "click", ref, cmd)
})
},
}
@@ -70,7 +70,7 @@ var typeCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.ActionSimpleWithFlags(client, base, token, "type", args, cmd)
+ browseractions.ActionSimple(client, base, token, "type", args, cmd)
})
},
}
@@ -81,7 +81,7 @@ var screenshotCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.ScreenshotWithFlags(client, base, token, cmd)
+ browseractions.Screenshot(client, base, token, cmd)
})
},
}
@@ -92,7 +92,7 @@ var tabsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.Tabs(client, base, token, nil)
+ browseractions.TabList(client, base, token)
})
},
}
@@ -126,7 +126,7 @@ var pressCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.ActionSimpleWithFlags(client, base, token, "press", args, cmd)
+ browseractions.ActionSimple(client, base, token, "press", args, cmd)
})
},
}
@@ -138,7 +138,7 @@ var fillCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.ActionSimpleWithFlags(client, base, token, "fill", args, cmd)
+ browseractions.ActionSimple(client, base, token, "fill", args, cmd)
})
},
}
@@ -154,7 +154,7 @@ var hoverCmd = &cobra.Command{
}
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.ActionWithFlags(client, base, token, "hover", ref, cmd)
+ browseractions.Action(client, base, token, "hover", ref, cmd)
})
},
}
@@ -166,7 +166,7 @@ var scrollCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.ActionSimpleWithFlags(client, base, token, "scroll", args, cmd)
+ browseractions.ActionSimple(client, base, token, "scroll", args, cmd)
})
},
}
@@ -178,7 +178,7 @@ var evalCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.EvaluateWithFlags(client, base, token, args, cmd)
+ browseractions.Evaluate(client, base, token, args, cmd)
})
},
}
@@ -189,7 +189,7 @@ var pdfCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.PDFWithFlags(client, base, token, cmd)
+ browseractions.PDF(client, base, token, cmd)
})
},
}
@@ -200,7 +200,7 @@ var textCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.TextWithFlags(client, base, token, cmd)
+ browseractions.Text(client, base, token, cmd)
})
},
}
@@ -254,7 +254,7 @@ var findCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.FindWithFlags(client, base, token, args[0], cmd)
+ browseractions.Find(client, base, token, args[0], cmd)
})
},
}
@@ -266,7 +266,7 @@ var selectCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.ActionSimpleWithFlags(client, base, token, "select", args, cmd)
+ browseractions.ActionSimple(client, base, token, "select", args, cmd)
})
},
}
@@ -426,7 +426,7 @@ func init() {
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.InstanceStartWithFlags(client, base, token, cmd)
+ browseractions.InstanceStart(client, base, token, cmd)
})
},
}
diff --git a/internal/cli/actions/actions_element.go b/internal/cli/actions/actions_element.go
index 140d3265..a4602d6f 100644
--- a/internal/cli/actions/actions_element.go
+++ b/internal/cli/actions/actions_element.go
@@ -10,7 +10,7 @@ import (
"strings"
)
-func ActionWithFlags(client *http.Client, base, token, kind, refArg string, cmd *cobra.Command) {
+func Action(client *http.Client, base, token, kind, refArg string, cmd *cobra.Command) {
body := map[string]any{"kind": kind}
css, _ := cmd.Flags().GetString("css")
@@ -36,7 +36,7 @@ func ActionWithFlags(client *http.Client, base, token, kind, refArg string, cmd
apiclient.DoPost(client, base, token, path, body)
}
-func ActionSimpleWithFlags(client *http.Client, base, token, kind string, args []string, cmd *cobra.Command) {
+func ActionSimple(client *http.Client, base, token, kind string, args []string, cmd *cobra.Command) {
body := map[string]any{"kind": kind}
switch kind {
@@ -84,85 +84,4 @@ func ActionSimpleWithFlags(client *http.Client, base, token, kind string, args [
apiclient.DoPost(client, base, token, path, body)
}
-func Action(client *http.Client, base, token, kind string, args []string) {
- body := map[string]any{"kind": kind}
- switch kind {
- case "click", "hover", "focus":
- var cssSelector string
- var refArg string
- for i := 0; i < len(args); i++ {
- switch args[i] {
- case "--css":
- if i+1 < len(args) {
- i++
- cssSelector = args[i]
- }
- case "--wait-nav":
- body["waitNav"] = true
- default:
- if refArg == "" {
- refArg = args[i]
- }
- }
- }
- if cssSelector != "" {
- body["selector"] = cssSelector
- } else if refArg != "" {
- body["ref"] = refArg
- } else {
- cli.Fatal("Usage: pinchtab %s <ref> [--css <selector>] [--wait-nav]", kind)
- }
- case "type":
- if len(args) < 2 {
- cli.Fatal("Usage: pinchtab type <ref> <text>")
- }
- body["ref"] = args[0]
- body["text"] = strings.Join(args[1:], " ")
- case "fill":
- if len(args) < 2 {
- cli.Fatal("Usage: pinchtab fill <ref|selector> <text>")
- }
- if strings.HasPrefix(args[0], "e") {
- body["ref"] = args[0]
- } else {
- body["selector"] = args[0]
- }
- body["text"] = strings.Join(args[1:], " ")
- case "press":
- if len(args) < 1 {
- cli.Fatal("Usage: pinchtab press <key> (e.g. Enter, Tab, Escape)")
- }
- body["key"] = args[0]
- case "scroll":
- if len(args) < 1 {
- cli.Fatal("Usage: pinchtab scroll <ref|pixels|direction> (e.g. e5, 800, or down)")
- }
- if strings.HasPrefix(args[0], "e") {
- body["ref"] = args[0]
- } else if px, err := strconv.Atoi(args[0]); err == nil {
- body["scrollY"] = px
- } else {
- switch strings.ToLower(args[0]) {
- case "down":
- body["scrollY"] = 800
- case "up":
- body["scrollY"] = -800
- case "right":
- body["scrollX"] = 800
- case "left":
- body["scrollX"] = -800
- default:
- cli.Fatal("Usage: pinchtab scroll <ref|pixels|direction> (e.g. e5, 800, or down)")
- }
- }
- case "select":
- if len(args) < 2 {
- cli.Fatal("Usage: pinchtab select <ref> <value>")
- }
- body["ref"] = args[0]
- body["value"] = args[1]
- }
-
- apiclient.DoPost(client, base, token, "/action", body)
-}
diff --git a/internal/cli/actions/actions_element_test.go b/internal/cli/actions/actions_element_test.go
index f410a2d7..822f1066 100644
--- a/internal/cli/actions/actions_element_test.go
+++ b/internal/cli/actions/actions_element_test.go
@@ -3,14 +3,31 @@ package actions
import (
"encoding/json"
"testing"
+
+ "github.com/spf13/cobra"
)
+func newActionCmd() *cobra.Command {
+ cmd := &cobra.Command{}
+ cmd.Flags().String("css", "", "")
+ cmd.Flags().Bool("wait-nav", false, "")
+ cmd.Flags().String("tab", "", "")
+ return cmd
+}
+
+func newSimpleCmd() *cobra.Command {
+ cmd := &cobra.Command{}
+ cmd.Flags().String("tab", "", "")
+ return cmd
+}
+
func TestClick(t *testing.T) {
m := newMockServer()
defer m.close()
client := m.server.Client()
- Action(client, m.base(), "", "click", []string{"e5"})
+ cmd := newActionCmd()
+ Action(client, m.base(), "", "click", "e5", cmd)
if m.lastPath != "/action" {
t.Errorf("expected /action, got %s", m.lastPath)
}
@@ -29,7 +46,9 @@ func TestClickWaitNav(t *testing.T) {
defer m.close()
client := m.server.Client()
- Action(client, m.base(), "", "click", []string{"e5", "--wait-nav"})
+ cmd := newActionCmd()
+ cmd.Flags().Set("wait-nav", "true")
+ Action(client, m.base(), "", "click", "e5", cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["waitNav"] != true {
@@ -42,7 +61,8 @@ func TestType(t *testing.T) {
defer m.close()
client := m.server.Client()
- Action(client, m.base(), "", "type", []string{"e12", "hello", "world"})
+ cmd := newSimpleCmd()
+ ActionSimple(client, m.base(), "", "type", []string{"e12", "hello", "world"}, cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["kind"] != "type" {
@@ -61,7 +81,8 @@ func TestPress(t *testing.T) {
defer m.close()
client := m.server.Client()
- Action(client, m.base(), "", "press", []string{"Enter"})
+ cmd := newSimpleCmd()
+ ActionSimple(client, m.base(), "", "press", []string{"Enter"}, cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["key"] != "Enter" {
@@ -74,7 +95,9 @@ func TestClickWithCSS(t *testing.T) {
defer m.close()
client := m.server.Client()
- Action(client, m.base(), "", "click", []string{"--css", "button.submit"})
+ cmd := newActionCmd()
+ cmd.Flags().Set("css", "button.submit")
+ Action(client, m.base(), "", "click", "", cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["selector"] != "button.submit" {
@@ -90,7 +113,10 @@ func TestClickWithCSS_AndWaitNav(t *testing.T) {
defer m.close()
client := m.server.Client()
- Action(client, m.base(), "", "click", []string{"--wait-nav", "--css", "#login-btn"})
+ cmd := newActionCmd()
+ cmd.Flags().Set("wait-nav", "true")
+ cmd.Flags().Set("css", "#login-btn")
+ Action(client, m.base(), "", "click", "", cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["selector"] != "#login-btn" {
@@ -106,7 +132,9 @@ func TestHoverWithCSS(t *testing.T) {
defer m.close()
client := m.server.Client()
- Action(client, m.base(), "", "hover", []string{"--css", ".nav-item"})
+ cmd := newActionCmd()
+ cmd.Flags().Set("css", ".nav-item")
+ Action(client, m.base(), "", "hover", "", cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["selector"] != ".nav-item" {
@@ -119,7 +147,9 @@ func TestFocusWithCSS(t *testing.T) {
defer m.close()
client := m.server.Client()
- Action(client, m.base(), "", "focus", []string{"--css", "input[name='email']"})
+ cmd := newActionCmd()
+ cmd.Flags().Set("css", "input[name='email']")
+ Action(client, m.base(), "", "focus", "", cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["selector"] != "input[name='email']" {
@@ -132,7 +162,8 @@ func TestClickRefStillWorks(t *testing.T) {
defer m.close()
client := m.server.Client()
- Action(client, m.base(), "", "click", []string{"e42"})
+ cmd := newActionCmd()
+ Action(client, m.base(), "", "click", "e42", cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["ref"] != "e42" {
@@ -148,8 +179,8 @@ func TestFill(t *testing.T) {
defer m.close()
client := m.server.Client()
- // Fill with ref
- Action(client, m.base(), "", "fill", []string{"e3", "test value"})
+ cmd := newSimpleCmd()
+ ActionSimple(client, m.base(), "", "fill", []string{"e3", "test value"}, cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["ref"] != "e3" {
@@ -159,8 +190,7 @@ func TestFill(t *testing.T) {
t.Errorf("expected text='test value', got %v", body["text"])
}
- // Fill with selector
- Action(client, m.base(), "", "fill", []string{"#email", "[email protected]"})
+ ActionSimple(client, m.base(), "", "fill", []string{"#email", "[email protected]"}, cmd)
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["selector"] != "#email" {
t.Errorf("expected selector=#email, got %v", body["selector"])
@@ -172,23 +202,21 @@ func TestScroll(t *testing.T) {
defer m.close()
client := m.server.Client()
- // Scroll by ref
- Action(client, m.base(), "", "scroll", []string{"e20"})
+ cmd := newSimpleCmd()
+ ActionSimple(client, m.base(), "", "scroll", []string{"e20"}, cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["ref"] != "e20" {
t.Errorf("expected ref=e20, got %v", body["ref"])
}
- // Scroll by pixels
- Action(client, m.base(), "", "scroll", []string{"800"})
+ ActionSimple(client, m.base(), "", "scroll", []string{"800"}, cmd)
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["scrollY"] != float64(800) {
t.Errorf("expected scrollY=800, got %v", body["scrollY"])
}
- // Scroll by direction
- Action(client, m.base(), "", "scroll", []string{"down"})
+ ActionSimple(client, m.base(), "", "scroll", []string{"down"}, cmd)
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["scrollY"] != float64(800) {
t.Errorf("expected scrollY=800 for direction=down, got %v", body["scrollY"])
@@ -200,7 +228,8 @@ func TestSelect(t *testing.T) {
defer m.close()
client := m.server.Client()
- Action(client, m.base(), "", "select", []string{"e10", "option2"})
+ cmd := newSimpleCmd()
+ ActionSimple(client, m.base(), "", "select", []string{"e10", "option2"}, cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["ref"] != "e10" {
diff --git a/internal/cli/actions/actions_evaluate.go b/internal/cli/actions/actions_evaluate.go
index b973074a..fd6575db 100644
--- a/internal/cli/actions/actions_evaluate.go
+++ b/internal/cli/actions/actions_evaluate.go
@@ -2,24 +2,13 @@ package actions
import (
"fmt"
- "github.com/pinchtab/pinchtab/internal/cli"
"github.com/pinchtab/pinchtab/internal/cli/apiclient"
"github.com/spf13/cobra"
"net/http"
"strings"
)
-func Evaluate(client *http.Client, base, token string, args []string) {
- if len(args) < 1 {
- cli.Fatal("Usage: pinchtab eval <expression>")
- }
- expr := strings.Join(args, " ")
- apiclient.DoPost(client, base, token, "/evaluate", map[string]any{
- "expression": expr,
- })
-}
-
-func EvaluateWithFlags(client *http.Client, base, token string, args []string, cmd *cobra.Command) {
+func Evaluate(client *http.Client, base, token string, args []string, cmd *cobra.Command) {
body := map[string]any{"expression": strings.Join(args, " ")}
tabID, _ := cmd.Flags().GetString("tab")
path := "/evaluate"
diff --git a/internal/cli/actions/actions_evaluate_test.go b/internal/cli/actions/actions_evaluate_test.go
index 744bb4a8..262d3ada 100644
--- a/internal/cli/actions/actions_evaluate_test.go
+++ b/internal/cli/actions/actions_evaluate_test.go
@@ -3,15 +3,24 @@ package actions
import (
"encoding/json"
"testing"
+
+ "github.com/spf13/cobra"
)
+func newEvalCmd() *cobra.Command {
+ cmd := &cobra.Command{}
+ cmd.Flags().String("tab", "", "")
+ return cmd
+}
+
func TestEvaluate(t *testing.T) {
m := newMockServer()
m.response = `{"result":"Example Domain"}`
defer m.close()
client := m.server.Client()
- Evaluate(client, m.base(), "", []string{"document.title"})
+ cmd := newEvalCmd()
+ Evaluate(client, m.base(), "", []string{"document.title"}, cmd)
if m.lastPath != "/evaluate" {
t.Errorf("expected /evaluate, got %s", m.lastPath)
}
@@ -27,7 +36,8 @@ func TestEvaluateMultiWord(t *testing.T) {
defer m.close()
client := m.server.Client()
- Evaluate(client, m.base(), "", []string{"1", "+", "2"})
+ cmd := newEvalCmd()
+ Evaluate(client, m.base(), "", []string{"1", "+", "2"}, cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["expression"] != "1 + 2" {
diff --git a/internal/cli/actions/actions_find.go b/internal/cli/actions/actions_find.go
index 629081af..397a372a 100644
--- a/internal/cli/actions/actions_find.go
+++ b/internal/cli/actions/actions_find.go
@@ -9,65 +9,7 @@ import (
"github.com/spf13/cobra"
)
-func Find(client *http.Client, base, token string, args []string) {
- if len(args) == 0 {
- cli.Fatal("Usage: pinchtab find <query> [--tab <id>] [--threshold <n>] [--explain] [--ref-only]")
- }
-
- query := args[0]
- tabID := ""
- threshold := ""
- explain := false
- refOnly := false
-
- for i := 1; i < len(args); i++ {
- switch args[i] {
- case "--tab":
- if i+1 < len(args) {
- i++
- tabID = args[i]
- }
- case "--threshold":
- if i+1 < len(args) {
- i++
- threshold = args[i]
- }
- case "--explain":
- explain = true
- case "--ref-only":
- refOnly = true
- }
- }
-
- body := map[string]any{"query": query}
- if tabID != "" {
- body["tabId"] = tabID
- }
- if threshold != "" {
- body["threshold"] = threshold
- }
- if explain {
- body["explain"] = true
- }
-
- path := "/find"
- if tabID != "" {
- path = fmt.Sprintf("/tabs/%s/find", tabID)
- delete(body, "tabId")
- }
-
- result := apiclient.DoPost(client, base, token, path, body)
-
- if refOnly {
- if ref, ok := result["best_ref"].(string); ok && ref != "" {
- fmt.Println(ref)
- return
- }
- cli.Fatal("No element found")
- }
-}
-
-func FindWithFlags(client *http.Client, base, token string, query string, cmd *cobra.Command) {
+func Find(client *http.Client, base, token string, query string, cmd *cobra.Command) {
tabID, _ := cmd.Flags().GetString("tab")
threshold, _ := cmd.Flags().GetString("threshold")
explain, _ := cmd.Flags().GetBool("explain")
diff --git a/internal/cli/actions/actions_instance.go b/internal/cli/actions/actions_instance.go
index 0eb7bd5b..e6f188d7 100644
--- a/internal/cli/actions/actions_instance.go
+++ b/internal/cli/actions/actions_instance.go
@@ -8,57 +8,7 @@ import (
"net/http"
)
-func Instance(client *http.Client, base, token string, args []string) {
- if len(args) < 1 {
- cli.Fatal("Usage: pinchtab instance <subcommand> [options]\nSubcommands: start, launch (alias), navigate, logs, stop")
- }
-
- subCmd := args[0]
- subArgs := args[1:]
-
- switch subCmd {
- case "start", "launch": // "start" is new Phase 2 API, "launch" is legacy
- InstanceStart(client, base, token, subArgs)
- case "navigate":
- InstanceNavigate(client, base, token, subArgs)
- case "logs":
- InstanceLogs(client, base, token, subArgs)
- case "stop":
- InstanceStop(client, base, token, subArgs)
- default:
- cli.Fatal("Unknown subcommand: %s", subCmd)
- }
-}
-
-func InstanceStart(client *http.Client, base, token string, args []string) {
- body := map[string]any{}
-
- for i := 0; i < len(args); i++ {
- switch args[i] {
- case "--profileId":
- if i+1 < len(args) {
- body["profileId"] = args[i+1]
- i++
- }
- case "--mode":
- if i+1 < len(args) {
- body["mode"] = args[i+1]
- i++
- }
- case "--port":
- if i+1 < len(args) {
- body["port"] = args[i+1]
- i++
- }
- }
- }
-
- // Use new /instances/start endpoint if available, fall back to /instances/launch for backward compat
- endpoint := "/instances/start"
- apiclient.DoPost(client, base, token, endpoint, body)
-}
-
-func InstanceStartWithFlags(client *http.Client, base, token string, cmd *cobra.Command) {
+func InstanceStart(client *http.Client, base, token string, cmd *cobra.Command) {
body := map[string]any{}
if v, _ := cmd.Flags().GetString("profileId"); v != "" {
body["profileId"] = v
diff --git a/internal/cli/actions/actions_navigate.go b/internal/cli/actions/actions_navigate.go
index 9f809fa6..43aa8109 100644
--- a/internal/cli/actions/actions_navigate.go
+++ b/internal/cli/actions/actions_navigate.go
@@ -2,32 +2,12 @@ package actions
import (
"fmt"
- "github.com/pinchtab/pinchtab/internal/cli"
"github.com/pinchtab/pinchtab/internal/cli/apiclient"
"github.com/spf13/cobra"
"net/http"
)
-func Navigate(client *http.Client, base, token string, args []string) {
- if len(args) < 1 {
- cli.Fatal("Usage: pinchtab nav <url> [--new-tab] [--block-images] [--block-ads]")
- }
- body := map[string]any{"url": args[0]}
- for _, a := range args[1:] {
- switch a {
- case "--new-tab":
- body["newTab"] = true
- case "--block-images":
- body["blockImages"] = true
- case "--block-ads":
- body["blockAds"] = true
- }
- }
- result := apiclient.DoPost(client, base, token, "/navigate", body)
- apiclient.SuggestNextAction("navigate", result)
-}
-
-func NavigateWithFlags(client *http.Client, base, token string, url string, cmd *cobra.Command) {
+func Navigate(client *http.Client, base, token string, url string, cmd *cobra.Command) {
body := map[string]any{"url": url}
if v, _ := cmd.Flags().GetBool("new-tab"); v {
body["newTab"] = true
diff --git a/internal/cli/actions/actions_navigate_test.go b/internal/cli/actions/actions_navigate_test.go
index 3276697d..a5d61058 100644
--- a/internal/cli/actions/actions_navigate_test.go
+++ b/internal/cli/actions/actions_navigate_test.go
@@ -3,14 +3,26 @@ package actions
import (
"encoding/json"
"testing"
+
+ "github.com/spf13/cobra"
)
+func newNavigateCmd() *cobra.Command {
+ cmd := &cobra.Command{}
+ cmd.Flags().Bool("new-tab", false, "")
+ cmd.Flags().Bool("block-images", false, "")
+ cmd.Flags().Bool("block-ads", false, "")
+ cmd.Flags().String("tab", "", "")
+ return cmd
+}
+
func TestNavigate(t *testing.T) {
m := newMockServer()
defer m.close()
client := m.server.Client()
- Navigate(client, m.base(), "", []string{"https://pinchtab.com"})
+ cmd := newNavigateCmd()
+ Navigate(client, m.base(), "", "https://pinchtab.com", cmd)
if m.lastMethod != "POST" {
t.Errorf("expected POST, got %s", m.lastMethod)
}
@@ -24,12 +36,15 @@ func TestNavigate(t *testing.T) {
}
}
-func TestNavigateWithFlags(t *testing.T) {
+func TestNavigateWithAllFlags(t *testing.T) {
m := newMockServer()
defer m.close()
client := m.server.Client()
- Navigate(client, m.base(), "", []string{"https://pinchtab.com", "--new-tab", "--block-images"})
+ cmd := newNavigateCmd()
+ cmd.Flags().Set("new-tab", "true")
+ cmd.Flags().Set("block-images", "true")
+ Navigate(client, m.base(), "", "https://pinchtab.com", cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["newTab"] != true {
@@ -45,7 +60,9 @@ func TestNavigateWithBlockAds(t *testing.T) {
defer m.close()
client := m.server.Client()
- Navigate(client, m.base(), "", []string{"https://pinchtab.com", "--block-ads"})
+ cmd := newNavigateCmd()
+ cmd.Flags().Set("block-ads", "true")
+ Navigate(client, m.base(), "", "https://pinchtab.com", cmd)
var body map[string]any
_ = json.Unmarshal([]byte(m.lastBody), &body)
if body["blockAds"] != true {
diff --git a/internal/cli/actions/actions_pdf.go b/internal/cli/actions/actions_pdf.go
index 12ec6584..4de47f6c 100644
--- a/internal/cli/actions/actions_pdf.go
+++ b/internal/cli/actions/actions_pdf.go
@@ -12,122 +12,7 @@ import (
"github.com/spf13/cobra"
)
-func PDF(client *http.Client, base, token string, args []string) {
- params := url.Values{}
- params.Set("raw", "true")
- outFile := ""
- tabID := ""
- for i := 0; i < len(args); i++ {
- switch args[i] {
- case "-o", "--output":
- if i+1 < len(args) {
- i++
- outFile = args[i]
- }
- case "--landscape":
- params.Set("landscape", "true")
- case "--scale":
- if i+1 < len(args) {
- i++
- params.Set("scale", args[i])
- }
- case "--tab":
- if i+1 < len(args) {
- i++
- tabID = args[i]
- }
- // Paper dimensions
- case "--paper-width":
- if i+1 < len(args) {
- i++
- params.Set("paperWidth", args[i])
- }
- case "--paper-height":
- if i+1 < len(args) {
- i++
- params.Set("paperHeight", args[i])
- }
- // Margins
- case "--margin-top":
- if i+1 < len(args) {
- i++
- params.Set("marginTop", args[i])
- }
- case "--margin-bottom":
- if i+1 < len(args) {
- i++
- params.Set("marginBottom", args[i])
- }
- case "--margin-left":
- if i+1 < len(args) {
- i++
- params.Set("marginLeft", args[i])
- }
- case "--margin-right":
- if i+1 < len(args) {
- i++
- params.Set("marginRight", args[i])
- }
- // Content options
- case "--page-ranges":
- if i+1 < len(args) {
- i++
- params.Set("pageRanges", args[i])
- }
- case "--prefer-css-page-size":
- params.Set("preferCSSPageSize", "true")
- // Header/Footer
- case "--display-header-footer":
- params.Set("displayHeaderFooter", "true")
- case "--header-template":
- if i+1 < len(args) {
- i++
- params.Set("headerTemplate", args[i])
- }
- case "--footer-template":
- if i+1 < len(args) {
- i++
- params.Set("footerTemplate", args[i])
- }
- // Accessibility
- case "--generate-tagged-pdf":
- params.Set("generateTaggedPDF", "true")
- case "--generate-document-outline":
- params.Set("generateDocumentOutline", "true")
- // Output options
- case "--file-output":
- params.Del("raw")
- params.Set("output", "file")
- case "--path":
- if i+1 < len(args) {
- i++
- params.Set("path", args[i])
- }
- case "--raw":
- params.Set("raw", "true")
- }
- }
-
- if outFile == "" {
- outFile = fmt.Sprintf("page-%s.pdf", time.Now().Format("20060102-150405"))
- }
-
- var data []byte
- if tabID != "" {
- data = apiclient.DoGetRaw(client, base, token, fmt.Sprintf("/tabs/%s/pdf", tabID), params)
- } else {
- data = apiclient.DoGetRaw(client, base, token, "/pdf", params)
- }
- if data == nil {
- return
- }
- if err := os.WriteFile(outFile, data, 0600); err != nil {
- cli.Fatal("Write failed: %v", err)
- }
- fmt.Println(cli.StyleStdout(cli.SuccessStyle, fmt.Sprintf("Saved %s (%d bytes)", outFile, len(data))))
-}
-
-func PDFWithFlags(client *http.Client, base, token string, cmd *cobra.Command) {
+func PDF(client *http.Client, base, token string, cmd *cobra.Command) {
params := url.Values{}
params.Set("raw", "true")
diff --git a/internal/cli/actions/actions_pdf_test.go b/internal/cli/actions/actions_pdf_test.go
index 0d84d329..2113515c 100644
--- a/internal/cli/actions/actions_pdf_test.go
+++ b/internal/cli/actions/actions_pdf_test.go
@@ -5,8 +5,34 @@ import (
"path/filepath"
"strings"
"testing"
+
+ "github.com/spf13/cobra"
)
+func newPDFCmd() *cobra.Command {
+ cmd := &cobra.Command{}
+ cmd.Flags().String("output", "", "")
+ cmd.Flags().String("tab", "", "")
+ cmd.Flags().Bool("landscape", false, "")
+ cmd.Flags().String("scale", "", "")
+ cmd.Flags().String("paper-width", "", "")
+ cmd.Flags().String("paper-height", "", "")
+ cmd.Flags().String("margin-top", "", "")
+ cmd.Flags().String("margin-bottom", "", "")
+ cmd.Flags().String("margin-left", "", "")
+ cmd.Flags().String("margin-right", "", "")
+ cmd.Flags().String("page-ranges", "", "")
+ cmd.Flags().Bool("prefer-css-page-size", false, "")
+ cmd.Flags().Bool("display-header-footer", false, "")
+ cmd.Flags().String("header-template", "", "")
+ cmd.Flags().String("footer-template", "", "")
+ cmd.Flags().Bool("generate-tagged-pdf", false, "")
+ cmd.Flags().Bool("generate-document-outline", false, "")
+ cmd.Flags().Bool("file-output", false, "")
+ cmd.Flags().String("path", "", "")
+ return cmd
+}
+
func TestPDF(t *testing.T) {
m := newMockServer()
m.response = "FAKEPDFDATA"
@@ -14,7 +40,12 @@ func TestPDF(t *testing.T) {
client := m.server.Client()
outFile := filepath.Join(t.TempDir(), "test.pdf")
- PDF(client, m.base(), "", []string{"-o", outFile, "--tab", "tab-abc", "--landscape", "--scale", "0.8"})
+ cmd := newPDFCmd()
+ cmd.Flags().Set("output", outFile)
+ cmd.Flags().Set("tab", "tab-abc")
+ cmd.Flags().Set("landscape", "true")
+ cmd.Flags().Set("scale", "0.8")
+ PDF(client, m.base(), "", cmd)
if m.lastPath != "/tabs/tab-abc/pdf" {
t.Errorf("expected /tabs/tab-abc/pdf, got %s", m.lastPath)
}
@@ -40,32 +71,30 @@ func TestPDFAllOptions(t *testing.T) {
client := m.server.Client()
outFile := filepath.Join(t.TempDir(), "test.pdf")
- args := []string{
- "-o", outFile,
- "--landscape",
- "--scale", "1.5",
- "--paper-width", "11",
- "--paper-height", "8.5",
- "--margin-top", "1",
- "--margin-bottom", "1",
- "--margin-left", "0.5",
- "--margin-right", "0.5",
- "--page-ranges", "1-3,5",
- "--prefer-css-page-size",
- "--display-header-footer",
- "--header-template", "<span class='title'></span>",
- "--footer-template", "<span class='pageNumber'></span>",
- "--generate-tagged-pdf",
- "--generate-document-outline",
- "--tab", "tab-123",
- }
+ cmd := newPDFCmd()
+ cmd.Flags().Set("output", outFile)
+ cmd.Flags().Set("tab", "tab-123")
+ cmd.Flags().Set("landscape", "true")
+ cmd.Flags().Set("scale", "1.5")
+ cmd.Flags().Set("paper-width", "11")
+ cmd.Flags().Set("paper-height", "8.5")
+ cmd.Flags().Set("margin-top", "1")
+ cmd.Flags().Set("margin-bottom", "1")
+ cmd.Flags().Set("margin-left", "0.5")
+ cmd.Flags().Set("margin-right", "0.5")
+ cmd.Flags().Set("page-ranges", "1-3,5")
+ cmd.Flags().Set("prefer-css-page-size", "true")
+ cmd.Flags().Set("display-header-footer", "true")
+ cmd.Flags().Set("header-template", "<span class='title'></span>")
+ cmd.Flags().Set("footer-template", "<span class='pageNumber'></span>")
+ cmd.Flags().Set("generate-tagged-pdf", "true")
+ cmd.Flags().Set("generate-document-outline", "true")
- PDF(client, m.base(), "", args)
+ PDF(client, m.base(), "", cmd)
if m.lastPath != "/tabs/tab-123/pdf" {
t.Errorf("expected /tabs/tab-123/pdf, got %s", m.lastPath)
}
- // Check all parameters were set correctly
expectedParams := []string{
"landscape=true",
"scale=1.5",
@@ -75,7 +104,6 @@ func TestPDFAllOptions(t *testing.T) {
"marginBottom=1",
"marginLeft=0.5",
"marginRight=0.5",
- "pageRanges=1-3%2C5", // URL encoded
"preferCSSPageSize=true",
"displayHeaderFooter=true",
"generateTaggedPDF=true",
diff --git a/internal/cli/actions/actions_screenshot.go b/internal/cli/actions/actions_screenshot.go
index d90a2ba9..20091411 100644
--- a/internal/cli/actions/actions_screenshot.go
+++ b/internal/cli/actions/actions_screenshot.go
@@ -11,45 +11,7 @@ import (
"time"
)
-func Screenshot(client *http.Client, base, token string, args []string) {
- params := url.Values{}
- params.Set("raw", "true")
- outFile := ""
- for i := 0; i < len(args); i++ {
- switch args[i] {
- case "-o", "--output":
- if i+1 < len(args) {
- i++
- outFile = args[i]
- }
- case "--quality", "-q":
- if i+1 < len(args) {
- i++
- params.Set("quality", args[i])
- }
- case "--tab":
- if i+1 < len(args) {
- i++
- params.Set("tabId", args[i])
- }
- }
- }
-
- if outFile == "" {
- outFile = fmt.Sprintf("screenshot-%s.jpg", time.Now().Format("20060102-150405"))
- }
-
- data := apiclient.DoGetRaw(client, base, token, "/screenshot", params)
- if data == nil {
- return
- }
- if err := os.WriteFile(outFile, data, 0600); err != nil {
- cli.Fatal("Write failed: %v", err)
- }
- fmt.Println(cli.StyleStdout(cli.SuccessStyle, fmt.Sprintf("Saved %s (%d bytes)", outFile, len(data))))
-}
-
-func ScreenshotWithFlags(client *http.Client, base, token string, cmd *cobra.Command) {
+func Screenshot(client *http.Client, base, token string, cmd *cobra.Command) {
params := url.Values{}
params.Set("raw", "true")
diff --git a/internal/cli/actions/actions_screenshot_test.go b/internal/cli/actions/actions_screenshot_test.go
index 0c4cd5e2..bec0a036 100644
--- a/internal/cli/actions/actions_screenshot_test.go
+++ b/internal/cli/actions/actions_screenshot_test.go
@@ -5,6 +5,8 @@ import (
"path/filepath"
"strings"
"testing"
+
+ "github.com/spf13/cobra"
)
func TestScreenshot(t *testing.T) {
@@ -14,7 +16,11 @@ func TestScreenshot(t *testing.T) {
client := m.server.Client()
outFile := filepath.Join(t.TempDir(), "test.jpg")
- Screenshot(client, m.base(), "", []string{"-o", outFile, "-q", "50"})
+ cmd := &cobra.Command{}
+ cmd.Flags().String("output", outFile, "")
+ cmd.Flags().String("quality", "50", "")
+ cmd.Flags().String("tab", "", "")
+ Screenshot(client, m.base(), "", cmd)
if m.lastPath != "/screenshot" {
t.Errorf("expected /screenshot, got %s", m.lastPath)
}
diff --git a/internal/cli/actions/actions_snapshot.go b/internal/cli/actions/actions_snapshot.go
index b168685e..3f2e38a1 100644
--- a/internal/cli/actions/actions_snapshot.go
+++ b/internal/cli/actions/actions_snapshot.go
@@ -7,45 +7,7 @@ import (
"net/url"
)
-func Snapshot(client *http.Client, base, token string, args []string) {
- params := url.Values{}
- for i := 0; i < len(args); i++ {
- switch args[i] {
- case "--interactive", "-i":
- params.Set("filter", "interactive")
- case "--compact", "-c":
- params.Set("format", "compact")
- case "--text":
- params.Set("format", "text")
- case "--diff", "-d":
- params.Set("diff", "true")
- case "--selector", "-s":
- if i+1 < len(args) {
- i++
- params.Set("selector", args[i])
- }
- case "--max-tokens":
- if i+1 < len(args) {
- i++
- params.Set("maxTokens", args[i])
- }
- case "--depth":
- if i+1 < len(args) {
- i++
- params.Set("depth", args[i])
- }
- case "--tab":
- if i+1 < len(args) {
- i++
- params.Set("tabId", args[i])
- }
- }
- }
- result := apiclient.DoGet(client, base, token, "/snapshot", params)
- apiclient.SuggestNextAction("snapshot", result)
-}
-
-func SnapshotWithFlags(client *http.Client, base, token string, cmd *cobra.Command) {
+func Snapshot(client *http.Client, base, token string, cmd *cobra.Command) {
params := url.Values{}
if v, _ := cmd.Flags().GetBool("interactive"); v {
params.Set("filter", "interactive")
diff --git a/internal/cli/actions/actions_snapshot_test.go b/internal/cli/actions/actions_snapshot_test.go
index 5d398892..bebbe03b 100644
--- a/internal/cli/actions/actions_snapshot_test.go
+++ b/internal/cli/actions/actions_snapshot_test.go
@@ -3,15 +3,33 @@ package actions
import (
"strings"
"testing"
+
+ "github.com/spf13/cobra"
)
+func newSnapshotCmd() *cobra.Command {
+ cmd := &cobra.Command{}
+ cmd.Flags().Bool("interactive", false, "")
+ cmd.Flags().Bool("compact", false, "")
+ cmd.Flags().Bool("text", false, "")
+ cmd.Flags().Bool("diff", false, "")
+ cmd.Flags().String("selector", "", "")
+ cmd.Flags().String("max-tokens", "", "")
+ cmd.Flags().String("depth", "", "")
+ cmd.Flags().String("tab", "", "")
+ return cmd
+}
+
func TestSnapshot(t *testing.T) {
m := newMockServer()
m.response = `[{"ref":"e0","role":"button","name":"Submit"}]`
defer m.close()
client := m.server.Client()
- Snapshot(client, m.base(), "", []string{"-i", "-c"})
+ cmd := newSnapshotCmd()
+ cmd.Flags().Set("interactive", "true")
+ cmd.Flags().Set("compact", "true")
+ Snapshot(client, m.base(), "", cmd)
if m.lastMethod != "GET" {
t.Errorf("expected GET, got %s", m.lastMethod)
}
@@ -31,7 +49,12 @@ func TestSnapshotDiff(t *testing.T) {
defer m.close()
client := m.server.Client()
- Snapshot(client, m.base(), "", []string{"--diff", "--selector", "main", "--max-tokens", "2000", "--depth", "5"})
+ cmd := newSnapshotCmd()
+ cmd.Flags().Set("diff", "true")
+ cmd.Flags().Set("selector", "main")
+ cmd.Flags().Set("max-tokens", "2000")
+ cmd.Flags().Set("depth", "5")
+ Snapshot(client, m.base(), "", cmd)
if !strings.Contains(m.lastQuery, "diff=true") {
t.Errorf("expected diff=true, got %s", m.lastQuery)
}
@@ -51,7 +74,9 @@ func TestSnapshotTabId(t *testing.T) {
defer m.close()
client := m.server.Client()
- Snapshot(client, m.base(), "", []string{"--tab", "ABC123"})
+ cmd := newSnapshotCmd()
+ cmd.Flags().Set("tab", "ABC123")
+ Snapshot(client, m.base(), "", cmd)
if !strings.Contains(m.lastQuery, "tabId=ABC123") {
t.Errorf("expected tabId=ABC123, got %s", m.lastQuery)
}
diff --git a/internal/cli/actions/actions_tabs.go b/internal/cli/actions/actions_tabs.go
index 80fd1b64..d5693b27 100644
--- a/internal/cli/actions/actions_tabs.go
+++ b/internal/cli/actions/actions_tabs.go
@@ -5,291 +5,12 @@ import (
"github.com/pinchtab/pinchtab/internal/cli"
"github.com/pinchtab/pinchtab/internal/cli/apiclient"
"net/http"
- "net/url"
"os"
- "strconv"
- "strings"
- "time"
)
-func Tabs(client *http.Client, base, token string, args []string) {
- if len(args) == 0 {
- // List all tabs
- apiclient.DoGet(client, base, token, "/tabs", nil)
- return
- }
-
- cmd := args[0]
- subArgs := args[1:]
-
- // Check if this is a tab operation (navigate, snapshot, click, etc.)
- // Pattern: pinchtab tab <operation> <tabId> [args...]
- if isTabOperation(cmd) {
- TabOperation(client, base, token, cmd, subArgs)
- return
- }
-
- // Legacy: pinchtab tab new/close
- switch cmd {
- case "new":
- url := ""
- if len(subArgs) > 0 {
- url = subArgs[0]
- }
-
- // Check if any instances are running
- instances := getInstances(client, base, token)
- if len(instances) == 0 {
- fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.WarningStyle, "No instances running, launching default..."))
- launchInstance(client, base, token, "default")
- fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.SuccessStyle, "Instance launched"))
- }
-
- body := map[string]any{"action": "new"}
- if url != "" {
- body["url"] = url
- }
- apiclient.DoPost(client, base, token, "/tab", body)
-
- case "close":
- if len(subArgs) < 1 {
- cli.Fatal("Usage: pinchtab tab close <tabId>")
- }
- apiclient.DoPost(client, base, token, "/tab", map[string]any{
- "action": "close",
- "tabId": subArgs[0],
- })
-
- default:
- TabOperation(client, base, token, cmd, subArgs)
- }
-}
-
-func isTabOperation(op string) bool {
- ops := map[string]bool{
- "navigate": true, "snapshot": true, "screenshot": true,
- "click": true, "type": true, "press": true, "fill": true,
- "hover": true, "scroll": true, "select": true, "focus": true,
- "text": true, "eval": true, "evaluate": true, "pdf": true,
- "cookies": true, "lock": true, "unlock": true, "locks": true,
- "fingerprint": true, "info": true,
- }
- return ops[op]
-}
-
-func TabOperation(client *http.Client, base, token string, op string, args []string) {
- if len(args) < 1 {
- cli.Fatal("Usage: pinchtab tab %s <tabId> [args...]", op)
- }
-
- tabID := args[0]
- restArgs := args[1:]
-
- switch op {
- case "navigate":
- if len(restArgs) < 1 {
- cli.Fatal("Usage: pinchtab tab navigate <tabId> <url> [--timeout N] [--block-images]")
- }
- body := map[string]any{"url": restArgs[0]}
- for i := 1; i < len(restArgs); i++ {
- switch restArgs[i] {
- case "--timeout":
- if i+1 < len(restArgs) {
- body["timeout"] = restArgs[i+1]
- i++
- }
- case "--block-images":
- body["blockImages"] = true
- case "--block-ads":
- body["blockAds"] = true
- }
- }
- apiclient.DoPost(client, base, token, fmt.Sprintf("/tabs/%s/navigate", tabID), body)
-
- case "snapshot":
- params := url.Values{}
- for _, arg := range restArgs {
- switch arg {
- case "-i", "--interactive":
- params.Set("interactive", "true")
- case "-c", "--compact":
- params.Set("compact", "true")
- case "-d", "--diff":
- params.Set("diff", "true")
- }
- }
- apiclient.DoGet(client, base, token, fmt.Sprintf("/tabs/%s/snapshot", tabID), params)
-
- case "screenshot", "ss":
- params := url.Values{}
- outFile := ""
- for i := 0; i < len(restArgs); i++ {
- switch restArgs[i] {
- case "-o", "--output":
- if i+1 < len(restArgs) {
- outFile = restArgs[i+1]
- i++
- }
- case "-q", "--quality":
- if i+1 < len(restArgs) {
- params.Set("quality", restArgs[i+1])
- i++
- }
- }
- }
- params.Set("raw", "true")
- data := apiclient.DoGetRaw(client, base, token, fmt.Sprintf("/tabs/%s/screenshot", tabID), params)
- if outFile == "" {
- outFile = fmt.Sprintf("screenshot-%s.png", time.Now().Format("20060102-150405"))
- }
- if data != nil {
- if err := os.WriteFile(outFile, data, 0600); err != nil {
- cli.Fatal("Write failed: %v", err)
- }
- fmt.Println(cli.StyleStdout(cli.SuccessStyle, fmt.Sprintf("Saved %s (%d bytes)", outFile, len(data))))
- }
-
- case "click", "hover", "focus":
- if len(restArgs) < 1 {
- cli.Fatal("Usage: pinchtab tab %s <tabId> <ref>", op)
- }
- body := map[string]any{"kind": op, "ref": restArgs[0]}
- apiclient.DoPost(client, base, token, fmt.Sprintf("/tabs/%s/action", tabID), body)
-
- case "type":
- if len(restArgs) < 2 {
- cli.Fatal("Usage: pinchtab tab type <tabId> <ref> <text>")
- }
- body := map[string]any{"kind": "type", "ref": restArgs[0], "text": strings.Join(restArgs[1:], " ")}
- apiclient.DoPost(client, base, token, fmt.Sprintf("/tabs/%s/action", tabID), body)
-
- case "fill":
- if len(restArgs) < 2 {
- cli.Fatal("Usage: pinchtab tab fill <tabId> <ref> <text>")
- }
- body := map[string]any{"kind": "fill", "ref": restArgs[0], "text": strings.Join(restArgs[1:], " ")}
- apiclient.DoPost(client, base, token, fmt.Sprintf("/tabs/%s/action", tabID), body)
-
- case "press":
- if len(restArgs) < 1 {
- cli.Fatal("Usage: pinchtab tab press <tabId> <key>")
- }
- body := map[string]any{"kind": "press", "key": restArgs[0]}
- apiclient.DoPost(client, base, token, fmt.Sprintf("/tabs/%s/action", tabID), body)
-
- case "scroll":
- if len(restArgs) < 1 {
- cli.Fatal("Usage: pinchtab tab scroll <tabId> <direction|pixels>")
- }
- body := map[string]any{}
- if v, err := strconv.Atoi(restArgs[0]); err == nil {
- body["kind"] = "scroll"
- body["scrollY"] = v
- } else {
- body["kind"] = "scroll"
- body["direction"] = restArgs[0]
- }
- apiclient.DoPost(client, base, token, fmt.Sprintf("/tabs/%s/action", tabID), body)
-
- case "select":
- if len(restArgs) < 2 {
- cli.Fatal("Usage: pinchtab tab select <tabId> <ref> <value>")
- }
- body := map[string]any{"kind": "select", "ref": restArgs[0], "value": restArgs[1]}
- apiclient.DoPost(client, base, token, fmt.Sprintf("/tabs/%s/action", tabID), body)
-
- case "text":
- params := url.Values{}
- for _, arg := range restArgs {
- if arg == "--raw" {
- params.Set("raw", "true")
- }
- }
- apiclient.DoGet(client, base, token, fmt.Sprintf("/tabs/%s/text", tabID), params)
-
- case "eval", "evaluate":
- if len(restArgs) < 1 {
- cli.Fatal("Usage: pinchtab tab eval <tabId> <expression>")
- }
- body := map[string]any{"expression": strings.Join(restArgs, " ")}
- apiclient.DoPost(client, base, token, fmt.Sprintf("/tabs/%s/evaluate", tabID), body)
-
- case "pdf":
- params := url.Values{}
- outFile := ""
- for i := 0; i < len(restArgs); i++ {
- switch restArgs[i] {
- case "-o", "--output":
- if i+1 < len(restArgs) {
- outFile = restArgs[i+1]
- i++
- }
- case "--landscape":
- params.Set("landscape", "true")
- case "--scale":
- if i+1 < len(restArgs) {
- params.Set("scale", restArgs[i+1])
- i++
- }
- }
- }
- params.Set("raw", "true")
- data := apiclient.DoGetRaw(client, base, token, fmt.Sprintf("/tabs/%s/pdf", tabID), params)
- if outFile == "" {
- outFile = fmt.Sprintf("page-%s.pdf", time.Now().Format("20060102-150405"))
- }
- if data != nil {
- if err := os.WriteFile(outFile, data, 0600); err != nil {
- cli.Fatal("Write failed: %v", err)
- }
- fmt.Println(cli.StyleStdout(cli.SuccessStyle, fmt.Sprintf("Saved %s (%d bytes)", outFile, len(data))))
- }
-
- case "cookies":
- apiclient.DoGet(client, base, token, fmt.Sprintf("/tabs/%s/cookies", tabID), nil)
-
- case "lock":
- body := map[string]any{}
- for i := 0; i < len(restArgs); i++ {
- switch restArgs[i] {
- case "--owner":
- if i+1 < len(restArgs) {
- body["owner"] = restArgs[i+1]
- i++
- }
- case "--ttl":
- if i+1 < len(restArgs) {
- if ttl, err := strconv.Atoi(restArgs[i+1]); err == nil {
- body["ttl"] = ttl
- }
- i++
- }
- }
- }
- apiclient.DoPost(client, base, token, fmt.Sprintf("/tabs/%s/lock", tabID), body)
-
- case "unlock":
- body := map[string]any{}
- for i := 0; i < len(restArgs); i++ {
- switch restArgs[i] {
- case "--owner":
- if i+1 < len(restArgs) {
- body["owner"] = restArgs[i+1]
- i++
- }
- }
- }
- apiclient.DoPost(client, base, token, fmt.Sprintf("/tabs/%s/unlock", tabID), body)
-
- case "locks":
- apiclient.DoGet(client, base, token, fmt.Sprintf("/tabs/%s/locks", tabID), nil)
-
- case "info":
- apiclient.DoGet(client, base, token, fmt.Sprintf("/tabs/%s", tabID), nil)
-
- default:
- cli.Fatal("Unknown tab operation: %s", op)
- }
+// TabList lists all open tabs.
+func TabList(client *http.Client, base, token string) {
+ apiclient.DoGet(client, base, token, "/tabs", nil)
}
// TabNew opens a new tab (exported for cobra subcommand).
diff --git a/internal/cli/actions/actions_tabs_test.go b/internal/cli/actions/actions_tabs_test.go
index 7982ef8f..2ca8ec2c 100644
--- a/internal/cli/actions/actions_tabs_test.go
+++ b/internal/cli/actions/actions_tabs_test.go
@@ -1,37 +1,17 @@
package actions
import (
- "encoding/json"
"testing"
)
-func TestTabsList(t *testing.T) {
+func TestTabList(t *testing.T) {
m := newMockServer()
m.response = `[{"id":"TAB1","url":"https://pinchtab.com"}]`
defer m.close()
client := m.server.Client()
- Tabs(client, m.base(), "", nil)
+ TabList(client, m.base(), "")
if m.lastPath != "/tabs" {
t.Errorf("expected /tabs, got %s", m.lastPath)
}
}
-
-func TestTabsNew(t *testing.T) {
- m := newMockServer()
- defer m.close()
- client := m.server.Client()
-
- Tabs(client, m.base(), "", []string{"new", "https://pinchtab.com"})
- if m.lastPath != "/tab" {
- t.Errorf("expected /tab, got %s", m.lastPath)
- }
- var body map[string]any
- _ = json.Unmarshal([]byte(m.lastBody), &body)
- if body["action"] != "new" {
- t.Errorf("expected action=new, got %v", body["action"])
- }
- if body["url"] != "https://pinchtab.com" {
- t.Errorf("expected url, got %v", body["url"])
- }
-}
diff --git a/internal/cli/actions/actions_text.go b/internal/cli/actions/actions_text.go
index 035f1885..8e434c40 100644
--- a/internal/cli/actions/actions_text.go
+++ b/internal/cli/actions/actions_text.go
@@ -7,23 +7,7 @@ import (
"net/url"
)
-func Text(client *http.Client, base, token string, args []string) {
- params := url.Values{}
- for i := 0; i < len(args); i++ {
- switch args[i] {
- case "--raw":
- params.Set("mode", "raw")
- case "--tab":
- if i+1 < len(args) {
- i++
- params.Set("tabId", args[i])
- }
- }
- }
- apiclient.DoGet(client, base, token, "/text", params)
-}
-
-func TextWithFlags(client *http.Client, base, token string, cmd *cobra.Command) {
+func Text(client *http.Client, base, token string, cmd *cobra.Command) {
params := url.Values{}
if v, _ := cmd.Flags().GetBool("raw"); v {
params.Set("mode", "raw")
diff --git a/internal/cli/actions/actions_text_test.go b/internal/cli/actions/actions_text_test.go
index 2bbf5da7..0daa467c 100644
--- a/internal/cli/actions/actions_text_test.go
+++ b/internal/cli/actions/actions_text_test.go
@@ -3,15 +3,25 @@ package actions
import (
"strings"
"testing"
+
+ "github.com/spf13/cobra"
)
+func newTextCmd() *cobra.Command {
+ cmd := &cobra.Command{}
+ cmd.Flags().Bool("raw", false, "")
+ cmd.Flags().String("tab", "", "")
+ return cmd
+}
+
func TestText(t *testing.T) {
m := newMockServer()
m.response = `{"url":"https://pinchtab.com","title":"Example","text":"Hello"}`
defer m.close()
client := m.server.Client()
- Text(client, m.base(), "", nil)
+ cmd := newTextCmd()
+ Text(client, m.base(), "", cmd)
if m.lastPath != "/text" {
t.Errorf("expected /text, got %s", m.lastPath)
}
@@ -22,7 +32,9 @@ func TestTextRaw(t *testing.T) {
defer m.close()
client := m.server.Client()
- Text(client, m.base(), "", []string{"--raw"})
+ cmd := newTextCmd()
+ cmd.Flags().Set("raw", "true")
+ Text(client, m.base(), "", cmd)
if !strings.Contains(m.lastQuery, "mode=raw") {
t.Errorf("expected mode=raw, got %s", m.lastQuery)
}
@@ -33,7 +45,9 @@ func TestTextTab(t *testing.T) {
defer m.close()
client := m.server.Client()
- Text(client, m.base(), "", []string{"--tab", "TAB1"})
+ cmd := newTextCmd()
+ cmd.Flags().Set("tab", "TAB1")
+ Text(client, m.base(), "", cmd)
if !strings.Contains(m.lastQuery, "tabId=TAB1") {
t.Errorf("expected tabId=TAB1, got %s", m.lastQuery)
}