|
| 1 | +// Package agent talks to the SourceAnt agent running on this machine. |
| 2 | +// |
| 3 | +// The CLI never reaches past the agent to the Python core. The agent is the |
| 4 | +// process that is always up and the one that knows where the core landed; |
| 5 | +// going around it would mean learning both. |
| 6 | +package agent |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "io" |
| 13 | + "net/http" |
| 14 | + "net/url" |
| 15 | + "strconv" |
| 16 | + "strings" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +// Status is what the agent says about itself. |
| 21 | +type Status struct { |
| 22 | + Version string `json:"version"` |
| 23 | + CoreURL string `json:"core_url"` |
| 24 | + CoreUp bool `json:"core_up"` |
| 25 | + CoreStarts int `json:"core_starts"` |
| 26 | + LastExit string `json:"last_exit,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +// Repository is one repository indexed on this machine. |
| 30 | +type Repository struct { |
| 31 | + Name string `json:"name"` |
| 32 | + Path string `json:"path"` |
| 33 | +} |
| 34 | + |
| 35 | +// Node is one file, import or symbol. |
| 36 | +type Node struct { |
| 37 | + ID string `json:"id"` |
| 38 | + Name string `json:"name"` |
| 39 | + Kind string `json:"kind"` |
| 40 | + Labels []string `json:"labels"` |
| 41 | + Path string `json:"path"` |
| 42 | +} |
| 43 | + |
| 44 | +// Link is a typed edge between two nodes. |
| 45 | +type Link struct { |
| 46 | + Source string `json:"source"` |
| 47 | + Target string `json:"target"` |
| 48 | + Type string `json:"type"` |
| 49 | +} |
| 50 | + |
| 51 | +// Graph is one repository's whole scope. |
| 52 | +type Graph struct { |
| 53 | + Nodes []Node `json:"nodes"` |
| 54 | + Links []Link `json:"links"` |
| 55 | + Truncated bool `json:"truncated"` |
| 56 | +} |
| 57 | + |
| 58 | +// Error is a non-2xx answer from the agent. |
| 59 | +type Error struct { |
| 60 | + StatusCode int |
| 61 | + Detail string |
| 62 | +} |
| 63 | + |
| 64 | +func (e *Error) Error() string { |
| 65 | + if e.Detail == "" { |
| 66 | + return fmt.Sprintf("the agent returned %d", e.StatusCode) |
| 67 | + } |
| 68 | + return e.Detail |
| 69 | +} |
| 70 | + |
| 71 | +// Unreachable says the agent is not running, or not where we looked. |
| 72 | +type Unreachable struct { |
| 73 | + BaseURL string |
| 74 | + Cause error |
| 75 | +} |
| 76 | + |
| 77 | +func (e *Unreachable) Error() string { |
| 78 | + return fmt.Sprintf("no agent answering at %s: %v", e.BaseURL, e.Cause) |
| 79 | +} |
| 80 | + |
| 81 | +func (e *Unreachable) Unwrap() error { return e.Cause } |
| 82 | + |
| 83 | +// Client talks to one agent. |
| 84 | +type Client struct { |
| 85 | + baseURL string |
| 86 | + http *http.Client |
| 87 | +} |
| 88 | + |
| 89 | +// New builds a client for the agent at baseURL. |
| 90 | +func New(baseURL string, timeout time.Duration) *Client { |
| 91 | + return &Client{ |
| 92 | + baseURL: strings.TrimRight(baseURL, "/"), |
| 93 | + http: &http.Client{Timeout: timeout}, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// BaseURL is the agent this client talks to. |
| 98 | +func (c *Client) BaseURL() string { return c.baseURL } |
| 99 | + |
| 100 | +// Status asks the agent how it and the core are doing. |
| 101 | +func (c *Client) Status(ctx context.Context) (Status, error) { |
| 102 | + return get[Status](ctx, c, "/health", nil) |
| 103 | +} |
| 104 | + |
| 105 | +// Repositories lists what is indexed on this machine. |
| 106 | +func (c *Client) Repositories(ctx context.Context) ([]Repository, error) { |
| 107 | + return get[[]Repository](ctx, c, "/api/repositories", nil) |
| 108 | +} |
| 109 | + |
| 110 | +// GraphOptions narrows what a drawing covers. |
| 111 | +type GraphOptions struct { |
| 112 | + PathPrefix string |
| 113 | + IncludeTests bool |
| 114 | + NodeLimit int |
| 115 | +} |
| 116 | + |
| 117 | +// Graph reads one repository's whole scope. |
| 118 | +func (c *Client) Graph(ctx context.Context, repository string, opts GraphOptions) (Graph, error) { |
| 119 | + query := url.Values{"repository": {repository}} |
| 120 | + if opts.PathPrefix != "" { |
| 121 | + query.Set("path_prefix", opts.PathPrefix) |
| 122 | + } |
| 123 | + if opts.IncludeTests { |
| 124 | + query.Set("include_tests", "true") |
| 125 | + } |
| 126 | + if opts.NodeLimit > 0 { |
| 127 | + query.Set("node_limit", strconv.Itoa(opts.NodeLimit)) |
| 128 | + } |
| 129 | + return get[Graph](ctx, c, "/api/graph", query) |
| 130 | +} |
| 131 | + |
| 132 | +func get[T any](ctx context.Context, c *Client, path string, query url.Values) (T, error) { |
| 133 | + var zero T |
| 134 | + target := c.baseURL + path |
| 135 | + if len(query) > 0 { |
| 136 | + target += "?" + query.Encode() |
| 137 | + } |
| 138 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, target, nil) |
| 139 | + if err != nil { |
| 140 | + return zero, err |
| 141 | + } |
| 142 | + req.Header.Set("Accept", "application/json") |
| 143 | + |
| 144 | + resp, err := c.http.Do(req) |
| 145 | + if err != nil { |
| 146 | + return zero, &Unreachable{BaseURL: c.baseURL, Cause: err} |
| 147 | + } |
| 148 | + defer func() { _ = resp.Body.Close() }() |
| 149 | + |
| 150 | + body, err := io.ReadAll(resp.Body) |
| 151 | + if err != nil { |
| 152 | + return zero, err |
| 153 | + } |
| 154 | + if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 155 | + return zero, &Error{StatusCode: resp.StatusCode, Detail: detail(body)} |
| 156 | + } |
| 157 | + if err := json.Unmarshal(body, &zero); err != nil { |
| 158 | + return zero, fmt.Errorf("the agent answered %s with something other than JSON: %w", path, err) |
| 159 | + } |
| 160 | + return zero, nil |
| 161 | +} |
| 162 | + |
| 163 | +func detail(body []byte) string { |
| 164 | + var parsed struct { |
| 165 | + Error string `json:"error"` |
| 166 | + } |
| 167 | + if err := json.Unmarshal(body, &parsed); err == nil && parsed.Error != "" { |
| 168 | + return parsed.Error |
| 169 | + } |
| 170 | + return strings.TrimSpace(string(body)) |
| 171 | +} |
0 commit comments