Skip to content

Commit 298a3bd

Browse files
authored
fix: pass context as the first argument (#2769)
The idiomatic way to pass a context.Context is as the first argument to functions. See https://go.dev/blog/context-and-structs. Updating relevant functions to follow this approach
1 parent 7e7cd2d commit 298a3bd

File tree

6 files changed

+19
-18
lines changed

6 files changed

+19
-18
lines changed

cmd/automation/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
package main
2020

2121
import (
22+
"context"
2223
"log"
2324
"os"
2425

2526
"github.com/googleapis/librarian/internal/automation"
2627
)
2728

2829
func main() {
29-
if err := automation.Run(os.Args[1:]); err != nil {
30+
if err := automation.Run(context.Background(), os.Args[1:]); err != nil {
3031
log.Fatal(err)
3132
}
3233
}

internal/automation/cli.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import (
2626
var runCommandFn = RunCommand
2727

2828
// Run parses the command line arguments and triggers the specified command.
29-
func Run(args []string) error {
30-
ctx := context.Background()
29+
func Run(ctx context.Context, args []string) error {
3130
options, err := parseFlags(args)
3231
if err != nil {
3332
slog.Error("error parsing command", slog.Any("err", err))

internal/automation/cli_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestRun(t *testing.T) {
5252
runCommandFn = func(ctx context.Context, command string, projectId string, push bool, build bool, forceRun bool) error {
5353
return tt.runCommandErr
5454
}
55-
if err := Run(tt.args); (err != nil) != tt.wantErr {
55+
if err := Run(context.Background(), tt.args); (err != nil) != tt.wantErr {
5656
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
5757
}
5858
})

internal/container/java/languagecontainer/languagecontainer.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type LanguageContainer struct {
4444
// Run accepts an implementation of the LanguageContainer.
4545
// The args parameter contains the command-line arguments passed to the container,
4646
// without including the program name. Usually it's os.Args[1:].
47-
func Run(args []string, container *LanguageContainer) int {
47+
func Run(ctx context.Context, args []string, container *LanguageContainer) int {
4848
// Logic to parse args and call the appropriate method on the container.
4949
// For example, if args[1] is "generate":
5050
// request := ... // unmarshal the request from the expected location
@@ -61,7 +61,7 @@ func Run(args []string, container *LanguageContainer) int {
6161
slog.Error("languagecontainer: generate command is not implemented")
6262
return 1
6363
}
64-
return handleGenerate(flags, container)
64+
return handleGenerate(ctx, flags, container)
6565
case "configure":
6666
slog.Warn("languagecontainer: configure command is missing")
6767
return 1
@@ -70,7 +70,7 @@ func Run(args []string, container *LanguageContainer) int {
7070
slog.Error("languagecontainer: generate command is missing")
7171
return 1
7272
}
73-
return handleReleaseStage(flags, container)
73+
return handleReleaseStage(ctx, flags, container)
7474
case "build":
7575
slog.Warn("languagecontainer: build command is not yet implemented")
7676
return 1
@@ -80,7 +80,7 @@ func Run(args []string, container *LanguageContainer) int {
8080
}
8181
}
8282

83-
func handleGenerate(flags []string, container *LanguageContainer) int {
83+
func handleGenerate(ctx context.Context, flags []string, container *LanguageContainer) int {
8484
genCtx := &generate.Context{}
8585
generateFlags := flag.NewFlagSet("generate", flag.ContinueOnError)
8686
generateFlags.StringVar(&genCtx.LibrarianDir, "librarian", "/librarian", "Path to the librarian-tool input directory. Contains generate-request.json.")
@@ -96,15 +96,15 @@ func handleGenerate(flags []string, container *LanguageContainer) int {
9696
slog.Error("failed to create generate config", "error", err)
9797
return 1
9898
}
99-
if err := container.Generate(context.Background(), cfg); err != nil {
99+
if err := container.Generate(ctx, cfg); err != nil {
100100
slog.Error("generate failed", "error", err)
101101
return 1
102102
}
103103
slog.Info("languagecontainer: generate command executed successfully")
104104
return 0
105105
}
106106

107-
func handleReleaseStage(flags []string, container *LanguageContainer) int {
107+
func handleReleaseStage(ctx context.Context, flags []string, container *LanguageContainer) int {
108108
cfg := &release.Context{}
109109
releaseInitFlags := flag.NewFlagSet("release-stage", flag.ContinueOnError)
110110
releaseInitFlags.StringVar(&cfg.LibrarianDir, "librarian", "/librarian", "Path to the librarian-tool input directory. Contains release-stage-request.json.")
@@ -129,7 +129,7 @@ func handleReleaseStage(flags []string, container *LanguageContainer) int {
129129
Context: cfg,
130130
Request: request,
131131
}
132-
response, err := container.ReleaseStage(context.Background(), config)
132+
response, err := container.ReleaseStage(ctx, config)
133133
if err != nil {
134134
slog.Error("release-stage failed", "error", err)
135135
return 1

internal/container/java/languagecontainer/languagecontainer_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestRun(t *testing.T) {
100100
return &message.ReleaseStageResponse{}, nil
101101
},
102102
}
103-
if gotCode := Run(tt.args, &container); gotCode != tt.wantCode {
103+
if gotCode := Run(context.Background(), tt.args, &container); gotCode != tt.wantCode {
104104
t.Errorf("Run() = %v, want %v", gotCode, tt.wantCode)
105105
}
106106
})
@@ -113,7 +113,7 @@ func TestRun_noArgs(t *testing.T) {
113113
t.Errorf("The code did not panic")
114114
}
115115
}()
116-
Run([]string{}, &LanguageContainer{})
116+
Run(context.Background(), []string{}, &LanguageContainer{})
117117
}
118118

119119
func TestRun_ReleaseStageWritesResponse(t *testing.T) {
@@ -129,7 +129,7 @@ func TestRun_ReleaseStageWritesResponse(t *testing.T) {
129129
},
130130
}
131131

132-
if code := Run(args, &container); code != 0 {
132+
if code := Run(context.Background(), args, &container); code != 0 {
133133
t.Errorf("Run() = %v, want 0", code)
134134
}
135135

@@ -172,7 +172,7 @@ func TestRun_ReleaseStageReadsContextArgs(t *testing.T) {
172172
return &message.ReleaseStageResponse{}, nil
173173
},
174174
}
175-
if code := Run(args, &container); code != 0 {
175+
if code := Run(context.Background(), args, &container); code != 0 {
176176
t.Errorf("Run() = %v, want 0", code)
177177
}
178178
if got, want := gotConfig.Context.LibrarianDir, librarianDir; got != want {
@@ -216,7 +216,7 @@ func TestRun_GenerateReadsContextArgs(t *testing.T) {
216216
return nil
217217
},
218218
}
219-
if code := Run(args, &container); code != 0 {
219+
if code := Run(context.Background(), args, &container); code != 0 {
220220
t.Errorf("Run() = %v, want 0", code)
221221
}
222222
if got, want := gotConfig.Context.LibrarianDir, librarianDir; got != want {
@@ -260,7 +260,7 @@ func TestRun_unimplementedCommands(t *testing.T) {
260260
}
261261
for _, tt := range tests {
262262
t.Run(tt.name, func(t *testing.T) {
263-
if gotCode := Run(tt.args, tt.container); gotCode != 1 {
263+
if gotCode := Run(context.Background(), tt.args, tt.container); gotCode != 1 {
264264
t.Errorf("Run() = %v, want 1", gotCode)
265265
}
266266
})

internal/container/java/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package main
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"log/slog"
2021
"os"
@@ -52,7 +53,7 @@ func runCLI(args []string) int {
5253
Generate: generate.Generate,
5354
ReleaseStage: release.Stage,
5455
}
55-
return languagecontainer.Run(args[1:], &container)
56+
return languagecontainer.Run(context.Background(), args[1:], &container)
5657
}
5758

5859
func parseLogLevel(logLevelEnv string) slog.Level {

0 commit comments

Comments
 (0)