summaryrefslogtreecommitdiff
path: root/internal/cli/start.go
blob: 99af9a51a3a4fb9e35018fb3a8d99edace751b32 (plain)
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
package cli

import (
	"encoding/json"
	"fmt"
	"io"
	"net/url"

	"github.com/spf13/cobra"
)
func newStartCmd() *cobra.Command {
	var serverURL string
	var agent string

	cmd := &cobra.Command{
		Use:   "start <task-id>",
		Short: "Queue a task for execution via the running server",
		Args:  cobra.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			return startTask(serverURL, args[0], agent)
		},
	}

	cmd.Flags().StringVar(&serverURL, "server", "http://localhost:8484", "claudomator server URL")
	cmd.Flags().StringVar(&agent, "agent", "", "agent to use (claude, gemini, or auto)")
	return cmd
}

func startTask(serverURL, id, agent string) error {
	u := fmt.Sprintf("%s/api/tasks/%s/run", serverURL, url.PathEscape(id))
	if agent != "" {
		u += "?agent=" + url.QueryEscape(agent)
	}
	resp, err := httpClient.Post(u, "application/json", nil) //nolint:noctx
	if err != nil {
		return fmt.Errorf("POST %s: %w", u, err)
	}
	defer resp.Body.Close()

	raw, _ := io.ReadAll(resp.Body)
	var body map[string]string
	if err := json.Unmarshal(raw, &body); err != nil {
		return fmt.Errorf("server returned invalid JSON (status %d): %s", resp.StatusCode, string(raw))
	}

	if resp.StatusCode >= 300 {
		return fmt.Errorf("server returned %d: %s", resp.StatusCode, body["error"])
	}

	fmt.Printf("Queued task %s\n", id)
	return nil
}