Skip to content

Commit 529c509

Browse files
committed
chore: add support for contextcheck
1 parent 485a055 commit 529c509

8 files changed

Lines changed: 31 additions & 18 deletions

File tree

.golangci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ linters:
2828
- bodyclose # checks whether HTTP response body is closed successfully [fast: false, auto-fix: false]
2929
- canonicalheader # canonicalheader checks whether net/http.Header uses canonical header [fast: false, auto-fix: false]
3030
- containedctx # Containedctx is a linter that detects struct contained context.Context field.
31+
- contextcheck # Check whether the function uses a non-inherited context.
3132
- copyloopvar # copyloopvar is a linter detects places where loop variables are copied [fast: true, auto-fix: false]
3233
- decorder # check declaration order and count of types, constants, variables and functions [fast: true, auto-fix: false]
3334
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false]
@@ -148,6 +149,11 @@ linters:
148149
linters:
149150
- tagalign
150151

152+
# cobra API requires func(*cobra.Command) error signature; context extracted from command
153+
- linters:
154+
- contextcheck
155+
text: "should pass the context parameter"
156+
151157
issues:
152158
max-issues-per-linter: 0
153159
max-same-issues: 0

core/bootstrap.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,6 @@ func Bootstrap(ctx context.Context, config *BootstrapConfig) (exitCode int, resu
199199
meta.OverrideExec = defaultOverrideExec
200200
}
201201

202-
if ctx == nil {
203-
ctx = context.Background()
204-
}
205202
ctx = account.InjectHTTPClient(ctx, httpClient)
206203
ctx = InjectMeta(ctx, meta)
207204

core/build_info.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (b *BuildInfo) checkVersion(ctx context.Context) {
7272
}
7373

7474
// pull latest version
75-
latestVersion, err := getLatestVersion(ExtractHTTPClient(ctx))
75+
latestVersion, err := getLatestVersion(ctx)
7676
if err != nil {
7777
ExtractLogger(ctx).Debugf("failed to retrieve latest version: %s\n", err)
7878

@@ -89,15 +89,15 @@ func (b *BuildInfo) checkVersion(ctx context.Context) {
8989
}
9090

9191
// getLatestVersion attempt to read the latest version of the remote file at latestVersionFileURL.
92-
func getLatestVersion(client *http.Client) (*version.Version, error) {
93-
ctx, cancelTimeout := context.WithTimeout(context.Background(), latestVersionRequestTimeout)
92+
func getLatestVersion(ctx context.Context) (*version.Version, error) {
93+
ctx, cancelTimeout := context.WithTimeout(ctx, latestVersionRequestTimeout)
9494
defer cancelTimeout()
9595

9696
req, err := http.NewRequestWithContext(ctx, http.MethodGet, latestGithubReleaseURL, nil)
9797
if err != nil {
9898
return nil, err
9999
}
100-
resp, err := client.Do(req)
100+
resp, err := ExtractHTTPClient(ctx).Do(req)
101101
if resp != nil {
102102
defer resp.Body.Close()
103103
}

core/cobra_builder.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ func (b *cobraBuilder) hydrateCobra(
150150

151151
// Use a custom function to print usage
152152
// This function will build usage to avoid building it for each commands
153-
cobraCmd.SetUsageFunc(usageFuncBuilder(cobraCmd, func() {
154-
ctx := cobraCmd.Context()
153+
cobraCmd.SetUsageFunc(usageFuncBuilder(cobraCmd, func(ctx context.Context) {
155154
if cobraCmd.Annotations == nil {
156155
cobraCmd.Annotations = make(map[string]string)
157156
}
@@ -180,7 +179,7 @@ func (b *cobraBuilder) hydrateCobra(
180179
}))
181180

182181
if cmd.Run != nil {
183-
cobraCmd.RunE = cobraRun(context.TODO(), cmd)
182+
cobraCmd.RunE = cobraRun(cmd)
184183
} else {
185184
// If command is not runnable we create a default run function that
186185
// will print usage of the parent command and exit with code 1

core/cobra_usage_builder.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,27 @@ func buildExamples(binaryName string, cmd *Command) string {
153153
// usageFuncBuilder returns the usage function that will be used by cobra to print usage,
154154
// the builder also takes a function that will fill annotations used by the usage template,
155155
// this is done like this to avoid build annotations for each command if not required
156-
func usageFuncBuilder(cmd *cobra.Command, annotationBuilder func()) func(*cobra.Command) error {
156+
func usageFuncBuilder(
157+
cmd *cobra.Command,
158+
annotationBuilder func(context.Context),
159+
) func(*cobra.Command) error {
157160
return func(command *cobra.Command) error {
158-
annotationBuilder()
159-
// after building annotation we remove this function as we prefer to use default UsageFunc
160-
cmd.SetUsageFunc(nil)
161+
executeWithCtx(command.Context(), cmd, annotationBuilder)
161162

162163
return cmd.UsageFunc()(command)
163164
}
164165
}
165166

167+
func executeWithCtx(
168+
ctx context.Context,
169+
cmd *cobra.Command,
170+
annotationBuilder func(context.Context),
171+
) {
172+
annotationBuilder(ctx)
173+
// after building annotation we remove this function as we prefer to use default UsageFunc
174+
cmd.SetUsageFunc(nil)
175+
}
176+
166177
func orderCobraCommands(cobraCommands []*cobra.Command) []*cobra.Command {
167178
commands := make([]*cobra.Command, len(cobraCommands))
168179
copy(commands, cobraCommands)

core/cobra_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
)
1414

1515
// cobraRun returns a cobraRun command that wrap a CommandRunner function.
16-
func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string) error {
16+
func cobraRun(cmd *Command) func(*cobra.Command, []string) error {
1717
return func(cobraCmd *cobra.Command, rawArgsStr []string) error {
1818
// Get context from cobra command, which should have been set during bootstrap
19-
ctx = cobraCmd.Context()
19+
ctx := cobraCmd.Context()
2020

2121
rawArgs := args.RawArgs(rawArgsStr)
2222

internal/namespaces/mcp/server/streamable_http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func RunStreamableHTTPServer(
4545
<-ctx.Done()
4646

4747
// Graceful shutdown with timeout
48-
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
48+
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 10*time.Second)
4949
defer shutdownCancel()
5050

5151
return server.Shutdown(shutdownCtx)

internal/tasks/tasks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (ts *Tasks) Execute(ctx context.Context, data any) (any, error) {
134134
cancelableCtx, cleanCtx := setupContext(ctx)
135135
defer cleanCtx()
136136

137-
logger, err := NewTasksLogger(context.Background(), ts.LoggerMode)
137+
logger, err := NewTasksLogger(cancelableCtx, ts.LoggerMode)
138138
if err != nil {
139139
return nil, err
140140
}

0 commit comments

Comments
 (0)