File size: 1,697 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package actions

import (
	"fmt"
	"github.com/pinchtab/pinchtab/internal/cli"
	"github.com/pinchtab/pinchtab/internal/cli/apiclient"
	"github.com/spf13/cobra"
	"net/http"
)

func InstanceStart(client *http.Client, base, token string, cmd *cobra.Command) {
	body := map[string]any{}
	if v, _ := cmd.Flags().GetString("profile"); v != "" {
		body["profileId"] = v
	}
	if v, _ := cmd.Flags().GetString("mode"); v != "" {
		body["mode"] = v
	}
	if v, _ := cmd.Flags().GetString("port"); v != "" {
		body["port"] = v
	}
	if exts, _ := cmd.Flags().GetStringArray("extension"); len(exts) > 0 {
		body["extensionPaths"] = exts
	}
	apiclient.DoPost(client, base, token, "/instances/start", body)
}

func InstanceNavigate(client *http.Client, base, token string, args []string) {
	if len(args) < 2 {
		cli.Fatal("Usage: pinchtab instance navigate <instance-id> <url>")
	}

	instID := args[0]
	targetURL := args[1]

	openResp := apiclient.DoPost(client, base, token, fmt.Sprintf("/instances/%s/tabs/open", instID), map[string]any{
		"url": "about:blank",
	})
	tabID, _ := openResp["tabId"].(string)
	if tabID == "" {
		cli.Fatal("failed to open tab for instance %s", instID)
	}

	apiclient.DoPost(client, base, token, fmt.Sprintf("/tabs/%s/navigate", tabID), map[string]any{
		"url": targetURL,
	})
}

func InstanceLogs(client *http.Client, base, token string, args []string) {
	instID := args[0]
	logs := apiclient.DoGetRaw(client, base, token, fmt.Sprintf("/instances/%s/logs", instID), nil)
	fmt.Println(string(logs))
}

func InstanceStop(client *http.Client, base, token string, args []string) {
	instID := args[0]
	apiclient.DoPost(client, base, token, fmt.Sprintf("/instances/%s/stop", instID), nil)
}