Skip to content

Commit 708127c

Browse files
authored
Merge pull request #27 from andreagrandi/context-aware-requests
Add context-aware requests and configurable timeout
2 parents 20dc583 + f4ce336 commit 708127c

32 files changed

Lines changed: 475 additions & 193 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [Unreleased]
44

5+
- Add `--timeout` flag and propagate request contexts so commands abort cleanly on Ctrl+C, with clear timeout and cancellation errors (#11)
56
- Add `MB_SESSION_TOKEN` as an alternative authentication method for users without admin access to mint an API key; mutually exclusive with `MB_API_KEY` (#9)
67
- Document changelog update workflow in `AGENTS.md` and add a pull request template prompting contributors to update `CHANGELOG.md` for user-visible changes (#20)
78
- Move main package to `cmd/mb-cli` so `go install github.com/andreagrandi/mb-cli/cmd/mb-cli@latest` produces an `mb-cli` binary that matches the documented command name (#10)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export MB_REDACT_PII=false # Disable PII redaction (enabled by default)
6262
| `--format`, `-f` | Output format: `json`, `table` | `json` |
6363
| `--verbose`, `-v` | Show request details on stderr | `false` |
6464
| `--redact-pii` | Redact PII values in query results | `true` |
65+
| `--timeout` | Timeout for a command's API requests (`0` disables) | `30s` |
6566

6667
### Database commands
6768

internal/cli/card.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ func runCardList(cmd *cobra.Command, args []string) error {
6666
return err
6767
}
6868

69-
cards, err := c.ListCards()
69+
ctx, cancel := requestContext(cmd)
70+
defer cancel()
71+
72+
cards, err := c.ListCards(ctx)
7073
if err != nil {
7174
return err
7275
}
@@ -85,7 +88,10 @@ func runCardGet(cmd *cobra.Command, args []string) error {
8588
return err
8689
}
8790

88-
card, err := c.GetCard(id)
91+
ctx, cancel := requestContext(cmd)
92+
defer cancel()
93+
94+
card, err := c.GetCard(ctx, id)
8995
if err != nil {
9096
return err
9197
}
@@ -114,7 +120,10 @@ func runCardRun(cmd *cobra.Command, args []string) error {
114120
return err
115121
}
116122

117-
result, err := c.RunCardWithParams(id, params)
123+
ctx, cancel := requestContext(cmd)
124+
defer cancel()
125+
126+
result, err := c.RunCardWithParams(ctx, id, params)
118127
if err != nil {
119128
return wrapParameterizedRunError(err)
120129
}

internal/cli/context_embed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Set both environment variables (required):
5050
| `--verbose`, `-v` | bool | false | Show request details on stderr |
5151
| `--error-format` | string | `text` | Error output format: `text` or `json` |
5252
| `--redact-pii` | bool | `true` | Redact PII values in query results |
53+
| `--timeout` | duration | `30s` | Timeout for a command's API requests (`0` disables) |
5354

5455
## Command-Specific Flags
5556

internal/cli/dashboard.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ func runDashboardList(cmd *cobra.Command, args []string) error {
120120
return err
121121
}
122122

123-
dashboards, err := c.ListDashboards()
123+
ctx, cancel := requestContext(cmd)
124+
defer cancel()
125+
126+
dashboards, err := c.ListDashboards(ctx)
124127
if err != nil {
125128
return err
126129
}
@@ -149,7 +152,10 @@ func runDashboardGet(cmd *cobra.Command, args []string) error {
149152
return err
150153
}
151154

152-
dashboard, err := c.GetDashboard(id)
155+
ctx, cancel := requestContext(cmd)
156+
defer cancel()
157+
158+
dashboard, err := c.GetDashboard(ctx, id)
153159
if err != nil {
154160
return err
155161
}
@@ -173,7 +179,10 @@ func runDashboardCards(cmd *cobra.Command, args []string) error {
173179
return err
174180
}
175181

176-
dashboard, err := c.GetDashboard(id)
182+
ctx, cancel := requestContext(cmd)
183+
defer cancel()
184+
185+
dashboard, err := c.GetDashboard(ctx, id)
177186
if err != nil {
178187
return err
179188
}
@@ -248,7 +257,10 @@ func runDashboardRunCard(cmd *cobra.Command, args []string) error {
248257
return err
249258
}
250259

251-
result, err := c.RunDashboardCard(dashboardID, dashcardID, cardID, params)
260+
ctx, cancel := requestContext(cmd)
261+
defer cancel()
262+
263+
result, err := c.RunDashboardCard(ctx, dashboardID, dashcardID, cardID, params)
252264
if err != nil {
253265
return wrapParameterizedRunError(err)
254266
}
@@ -267,7 +279,10 @@ func runDashboardParamLookup(cmd *cobra.Command, dashboardArg string, requestedK
267279
return err
268280
}
269281

270-
dashboard, err := c.GetDashboard(dashboardID)
282+
ctx, cancel := requestContext(cmd)
283+
defer cancel()
284+
285+
dashboard, err := c.GetDashboard(ctx, dashboardID)
271286
if err != nil {
272287
return err
273288
}
@@ -276,9 +291,9 @@ func runDashboardParamLookup(cmd *cobra.Command, dashboardArg string, requestedK
276291

277292
var values *client.ParameterValues
278293
if search {
279-
values, err = c.SearchDashboardParamValues(dashboardID, resolvedKey, query)
294+
values, err = c.SearchDashboardParamValues(ctx, dashboardID, resolvedKey, query)
280295
} else {
281-
values, err = c.GetDashboardParamValues(dashboardID, resolvedKey)
296+
values, err = c.GetDashboardParamValues(ctx, dashboardID, resolvedKey)
282297
}
283298
if err != nil {
284299
return err

internal/cli/dashboard_analyze.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"os"
@@ -98,7 +99,10 @@ func runDashboardAnalyze(cmd *cobra.Command, args []string) error {
9899
return err
99100
}
100101

101-
analysis, err := analyzeDashboard(c, id)
102+
ctx, cancel := requestContext(cmd)
103+
defer cancel()
104+
105+
analysis, err := analyzeDashboard(ctx, c, id)
102106
if err != nil {
103107
return err
104108
}
@@ -111,8 +115,8 @@ func runDashboardAnalyze(cmd *cobra.Command, args []string) error {
111115
return formatDashboardAnalysisTable(os.Stdout, analysis)
112116
}
113117

114-
func analyzeDashboard(c *client.Client, id int) (*dashboardAnalysis, error) {
115-
dashboard, err := c.GetDashboard(id)
118+
func analyzeDashboard(ctx context.Context, c *client.Client, id int) (*dashboardAnalysis, error) {
119+
dashboard, err := c.GetDashboard(ctx, id)
116120
if err != nil {
117121
return nil, err
118122
}
@@ -166,7 +170,7 @@ func analyzeDashboard(c *client.Client, id int) (*dashboardAnalysis, error) {
166170
entry.ParameterIDs = collectDashcardParameterIDs(dashCard)
167171

168172
if dashCard.CardID != nil {
169-
fullCard, chain, baseCard, err := traceCardLineage(c, cardCache, *dashCard.CardID)
173+
fullCard, chain, baseCard, err := traceCardLineage(ctx, c, cardCache, *dashCard.CardID)
170174
if err != nil {
171175
return nil, err
172176
}
@@ -275,8 +279,8 @@ func collectDashcardParameterIDs(dashCard client.DashCard) []string {
275279
return ids
276280
}
277281

278-
func traceCardLineage(c *client.Client, cache map[int]*client.Card, cardID int) (*client.Card, []int, *client.Card, error) {
279-
current, err := getAnalyzedCard(c, cache, cardID)
282+
func traceCardLineage(ctx context.Context, c *client.Client, cache map[int]*client.Card, cardID int) (*client.Card, []int, *client.Card, error) {
283+
current, err := getAnalyzedCard(ctx, c, cache, cardID)
280284
if err != nil {
281285
return nil, nil, nil, err
282286
}
@@ -291,7 +295,7 @@ func traceCardLineage(c *client.Client, cache map[int]*client.Card, cardID int)
291295
chain = append(chain, *sourceCardID)
292296
seen[*sourceCardID] = true
293297

294-
current, err = getAnalyzedCard(c, cache, *sourceCardID)
298+
current, err = getAnalyzedCard(ctx, c, cache, *sourceCardID)
295299
if err != nil {
296300
return nil, nil, nil, err
297301
}
@@ -300,12 +304,12 @@ func traceCardLineage(c *client.Client, cache map[int]*client.Card, cardID int)
300304
return cache[cardID], chain, nil, nil
301305
}
302306

303-
func getAnalyzedCard(c *client.Client, cache map[int]*client.Card, cardID int) (*client.Card, error) {
307+
func getAnalyzedCard(ctx context.Context, c *client.Client, cache map[int]*client.Card, cardID int) (*client.Card, error) {
304308
if card, ok := cache[cardID]; ok {
305309
return card, nil
306310
}
307311

308-
card, err := c.GetCard(cardID)
312+
card, err := c.GetCard(ctx, cardID)
309313
if err != nil {
310314
return nil, err
311315
}

internal/cli/database.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ func runDatabaseList(cmd *cobra.Command, args []string) error {
101101
return err
102102
}
103103

104-
databases, err := c.ListDatabases(false)
104+
ctx, cancel := requestContext(cmd)
105+
defer cancel()
106+
107+
databases, err := c.ListDatabases(ctx, false)
105108
if err != nil {
106109
return err
107110
}
@@ -120,7 +123,10 @@ func runDatabaseGet(cmd *cobra.Command, args []string) error {
120123
return err
121124
}
122125

123-
db, err := c.GetDatabase(id)
126+
ctx, cancel := requestContext(cmd)
127+
defer cancel()
128+
129+
db, err := c.GetDatabase(ctx, id)
124130
if err != nil {
125131
return err
126132
}
@@ -139,7 +145,10 @@ func runDatabaseMetadata(cmd *cobra.Command, args []string) error {
139145
return err
140146
}
141147

142-
meta, err := c.GetDatabaseMetadata(id)
148+
ctx, cancel := requestContext(cmd)
149+
defer cancel()
150+
151+
meta, err := c.GetDatabaseMetadata(ctx, id)
143152
if err != nil {
144153
return err
145154
}
@@ -158,7 +167,10 @@ func runDatabaseFields(cmd *cobra.Command, args []string) error {
158167
return err
159168
}
160169

161-
fields, err := c.GetDatabaseFields(id)
170+
ctx, cancel := requestContext(cmd)
171+
defer cancel()
172+
173+
fields, err := c.GetDatabaseFields(ctx, id)
162174
if err != nil {
163175
return err
164176
}
@@ -177,7 +189,10 @@ func runDatabaseSchemas(cmd *cobra.Command, args []string) error {
177189
return err
178190
}
179191

180-
schemas, err := c.ListDatabaseSchemas(id)
192+
ctx, cancel := requestContext(cmd)
193+
defer cancel()
194+
195+
schemas, err := c.ListDatabaseSchemas(ctx, id)
181196
if err != nil {
182197
return err
183198
}
@@ -197,7 +212,10 @@ func runDatabaseSchema(cmd *cobra.Command, args []string) error {
197212
return err
198213
}
199214

200-
tables, err := c.GetDatabaseSchema(id, schema)
215+
ctx, cancel := requestContext(cmd)
216+
defer cancel()
217+
218+
tables, err := c.GetDatabaseSchema(ctx, id, schema)
201219
if err != nil {
202220
return err
203221
}

internal/cli/field.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ func runFieldGet(cmd *cobra.Command, args []string) error {
5252
return err
5353
}
5454

55-
field, err := c.GetField(id)
55+
ctx, cancel := requestContext(cmd)
56+
defer cancel()
57+
58+
field, err := c.GetField(ctx, id)
5659
if err != nil {
5760
return err
5861
}
@@ -71,7 +74,10 @@ func runFieldSummary(cmd *cobra.Command, args []string) error {
7174
return err
7275
}
7376

74-
summary, err := c.GetFieldSummary(id)
77+
ctx, cancel := requestContext(cmd)
78+
defer cancel()
79+
80+
summary, err := c.GetFieldSummary(ctx, id)
7581
if err != nil {
7682
return err
7783
}
@@ -90,7 +96,10 @@ func runFieldValues(cmd *cobra.Command, args []string) error {
9096
return err
9197
}
9298

93-
values, err := c.GetFieldValues(id)
99+
ctx, cancel := requestContext(cmd)
100+
defer cancel()
101+
102+
values, err := c.GetFieldValues(ctx, id)
94103
if err != nil {
95104
return err
96105
}

0 commit comments

Comments
 (0)