-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.go
More file actions
320 lines (286 loc) · 10.9 KB
/
Copy pathmonitor.go
File metadata and controls
320 lines (286 loc) · 10.9 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"text/tabwriter"
"github.com/InstaNode-dev/cli/internal/tokens"
"github.com/spf13/cobra"
)
// ── Provisioning subcommand groups ───────────────────────────────────────────
// instant db new --name <name>
// instant cache new --name <name>
// instant nosql new --name <name>
// instant queue new --name <name>
//
// The resource `name` is REQUIRED on every provisioning endpoint. The server
// enforces 1–64 chars matching nameRegexp and rejects an omitted name with
// HTTP 400; the CLI marks --name required so the error surfaces locally
// before any API round trip.
// nameMaxLen and nameRegexp mirror the server-side resource-name contract
// (1–64 chars, must start with an alphanumeric character).
const nameMaxLen = 64
var nameRegexp = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9 _-]*$`)
// resourceName is bound to the required --name flag on every `new` command.
var resourceName string
// validateResourceName applies the server-side name contract locally so the
// CLI fails fast with a clear message instead of a bare HTTP 400.
func validateResourceName(name string) error {
if name == "" {
return fmt.Errorf("--name is required")
}
if len(name) > nameMaxLen {
return fmt.Errorf("--name must be 1–%d characters (got %d)", nameMaxLen, len(name))
}
if !nameRegexp.MatchString(name) {
return fmt.Errorf("--name %q is invalid: must match %s", name, nameRegexp.String())
}
return nil
}
// B15-P0 (3) — every resource-group command (db / cache / nosql / queue /
// storage / webhook / vector) MUST reject unknown sub-sub-commands with a
// non-zero exit. The previous behaviour was:
//
// instant db delete <id> → prints help, exits 0
//
// which silently hid typo bugs in agent scripts and let `... | xargs instant`
// pipelines look successful. The pattern below combines:
//
// 1. Args: cobra.NoArgs — refuses any positional arg
// 2. RunE: showGroupHelp — when called with zero args, shows
// help and exits 0 (the legacy path)
// 3. cobra's built-in "did you mean?" suggestions surface for typos that
// are within 2 edits of a valid subcommand (cobra default).
//
// Together, `instant db delete <id>` now errors with:
// Error: unknown command "delete" for "instant db"
// Run 'instant db --help' for usage.
// and exits 1.
func showGroupHelp(cmd *cobra.Command, args []string) error {
return cmd.Help()
}
func newGroupCmd(use, short string) *cobra.Command {
return &cobra.Command{
Use: use,
Short: short,
// Args: NoArgs — any positional arg that isn't a registered
// sub-sub-command name surfaces cobra's "unknown command" error.
Args: cobra.NoArgs,
// RunE fires only when zero args reach the parent — i.e.
// `instant db` with no subcommand. Print help, exit 0.
RunE: showGroupHelp,
}
}
var (
dbCmd = newGroupCmd("db", "Manage Postgres database resources")
cacheCmd = newGroupCmd("cache", "Manage Redis cache resources")
nosqlCmd = newGroupCmd("nosql", "Manage MongoDB document-store resources")
queueCmd = newGroupCmd("queue", "Manage NATS JetStream queue resources")
)
var dbNewCmd = &cobra.Command{
Use: "new --name <name>",
Short: "Provision a Postgres database (+ pgvector)",
Example: " instant db new --name app-db",
Args: cobra.NoArgs,
RunE: makeProvisionCmd("/db/new", "db"),
}
var cacheNewCmd = &cobra.Command{
Use: "new --name <name>",
Short: "Provision a Redis cache",
Example: " instant cache new --name app-cache",
Args: cobra.NoArgs,
RunE: makeProvisionCmd("/cache/new", "cache"),
}
var nosqlNewCmd = &cobra.Command{
Use: "new --name <name>",
Short: "Provision a MongoDB document store",
Example: " instant nosql new --name app-docs",
Args: cobra.NoArgs,
RunE: makeProvisionCmd("/nosql/new", "nosql"),
}
var queueNewCmd = &cobra.Command{
Use: "new --name <name>",
Short: "Provision a NATS JetStream queue",
Example: " instant queue new --name app-jobs",
Args: cobra.NoArgs,
RunE: makeProvisionCmd("/queue/new", "queue"),
}
// makeProvisionCmd returns a RunE function that POSTs to the given endpoint
// and prints the provisioned connection URL. The resource name comes from the
// required --name flag.
func makeProvisionCmd(endpoint, resourceType string) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
name := resourceName
if err := validateResourceName(name); err != nil {
return err
}
creds, err := provisionResource(endpoint, name)
if err != nil {
return fmt.Errorf("provisioning failed: %w", err)
}
// Save token locally for `instant status` + B15-P1 (7) anon-up
// idempotency. Type+Env are required so anonymous `up` can match
// (type, name, env) on subsequent runs without an API list call.
// Env is populated from the resolved provision env when the server
// echoed it back (api ≥ 2026-05-13 / migration 026); falls back to
// "development" — the platform default — when omitted.
if store, loadErr := tokens.Load(); loadErr == nil {
urlOrReceive := creds.ConnectionURL
if urlOrReceive == "" {
urlOrReceive = creds.ReceiveURL
}
env := creds.Env
if env == "" {
env = "development"
}
_ = store.Add(tokens.Entry{
Token: creds.Token,
Name: creds.Name,
Type: resourceType,
Env: env,
URL: urlOrReceive,
Source: "provision",
})
}
fmt.Printf("ok %-8s %s\n", resourceType, creds.Token)
fmt.Printf("url %s\n", creds.ConnectionURL)
if creds.Tier != "" {
fmt.Printf("tier %s\n", creds.Tier)
}
if creds.Note != "" {
fmt.Printf("\n%s\n", creds.Note)
}
return nil
}
}
// provisionResponse is the shape returned by POST /{service}/new endpoints.
// /webhook/new returns receive_url instead of connection_url.
type provisionResponse struct {
OK bool `json:"ok"`
Token string `json:"token"`
Name string `json:"name"`
ConnectionURL string `json:"connection_url"`
ReceiveURL string `json:"receive_url"`
Tier string `json:"tier"`
// Env is the resolved provisioning environment the server echoed back
// (api ≥ 2026-05-13 / migration 026). May be empty against older builds;
// callers default to "development" — the platform's lowest-stakes
// default (CLAUDE.md rule 11) — when empty. Used to key the local
// tokens cache so B15-P1 (7) anonymous-up idempotency can match on
// (type, name, env) without an API list call.
Env string `json:"env"`
Note string `json:"note"`
Upgrade string `json:"upgrade"`
}
// provisionResource calls POST {APIBaseURL}{endpoint} and returns parsed credentials.
//
// T16 P1-2: a 401 against an authenticated request returns the uniform
// errSessionExpired() error so the exit-code contract is consistent across
// `resources`, `up`, and direct provisioning.
func provisionResource(endpoint, name string) (*provisionResponse, error) {
url := APIBaseURL + endpoint
body, _ := json.Marshal(map[string]string{"name": name})
resp, err := HTTPClient.Post(url, "application/json", bytes.NewReader(body))
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
raw, _ := io.ReadAll(resp.Body)
if resp.StatusCode == http.StatusUnauthorized && haveAuth() {
return nil, errSessionExpired()
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
// T16 P2-1 — surface the structured error envelope
// ({message, agent_action, upgrade_url, ...}) rather than
// dumping the raw JSON blob at the user.
return nil, parseAPIError(resp.StatusCode, raw)
}
var result provisionResponse
if err := json.Unmarshal(raw, &result); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
if !result.OK || result.Token == "" {
return nil, fmt.Errorf("unexpected response: ok=%v token=%q", result.OK, result.Token)
}
return &result, nil
}
// ── status command ────────────────────────────────────────────────────────────
// statusJSON is the --json flag for `instant status`. T16 P3: machine-readable
// output for agents.
var statusJSON bool
var statusCmd = &cobra.Command{
Use: "status",
Short: "Show locally tracked resources",
Long: `Display all resources saved in ~/.instant-tokens.
Resources are saved automatically when you run:
instant db new --name <name>
instant cache new --name <name>
instant nosql new --name <name>
instant queue new --name <name>
With --json, output is a machine-readable JSON array of token entries
({token, name, url, source, created_at}).
`,
RunE: func(cmd *cobra.Command, args []string) error {
store, err := tokens.Load()
if err != nil {
return wrapJSONErr(cmd, fmt.Errorf("loading token store: %w", err))
}
// T16 P3 — machine-readable output. Empty list emits `[]`.
//
// B15-P1 (9) — store.Entries is a nil slice when ~/.instant-tokens
// has never been written; json.Encoder serializes a nil []T as
// `null`, which crashes `instant status --json | jq '.[] | …'`.
// Force the empty-slice literal so agents can pipe the output
// unconditionally and `resources --json` / `status --json` share
// the same `[]` shape on empty stores.
if statusJSON {
entries := store.Entries
if entries == nil {
entries = []tokens.Entry{}
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(entries)
}
if len(store.Entries) == 0 {
fmt.Println("No resources found. Run `instant db new` or similar to get started.")
return nil
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintln(w, "TOKEN\tNAME\tSOURCE\tCREATED")
for _, e := range store.Entries {
shortToken := e.Token
if len(shortToken) > 12 {
shortToken = shortToken[:12] + "…"
}
created := e.CreatedAt.Format("2006-01-02")
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
shortToken, e.Name, e.Source, created)
}
_ = w.Flush()
return nil
},
}
func init() {
// --name is REQUIRED on every provisioning command. Cobra surfaces a
// clear `required flag(s) "name" not set` error before RunE runs.
for _, c := range []*cobra.Command{dbNewCmd, cacheNewCmd, nosqlNewCmd, queueNewCmd} {
c.Flags().StringVar(&resourceName, "name", "", "Resource name (required, 1–64 chars, matches ^[A-Za-z0-9][A-Za-z0-9 _-]*$)")
_ = c.MarkFlagRequired("name")
}
dbCmd.AddCommand(dbNewCmd)
cacheCmd.AddCommand(cacheNewCmd)
nosqlCmd.AddCommand(nosqlNewCmd)
queueCmd.AddCommand(queueNewCmd)
rootCmd.AddCommand(dbCmd)
rootCmd.AddCommand(cacheCmd)
rootCmd.AddCommand(nosqlCmd)
rootCmd.AddCommand(queueCmd)
statusCmd.Flags().BoolVar(&statusJSON, "json", false,
"Emit a JSON array of local token entries instead of a human-readable table")
rootCmd.AddCommand(statusCmd)
}