Skip to content

Commit 11de988

Browse files
authored
refactor(internal/librarian/golang): reorganize module initialization functions (#4351)
This change improves the organization of the `golang` package by grouping module-related functions together. - Moved `modulePath` and `initModule` from `generate.go` to `module.go`. - Moved `TestInitModule` from `generate_test.go` to `module_test.go`. - Reordered the `Format` function within `generate.go` to appear earlier in the file. For #3617
1 parent dc2af10 commit 11de988

4 files changed

Lines changed: 57 additions & 55 deletions

File tree

internal/librarian/golang/generate.go

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ func GenerateLibraries(ctx context.Context, libraries []*config.Library, googlea
5555
return nil
5656
}
5757

58+
// Format formats a generated Go library.
59+
func Format(ctx context.Context, library *config.Library) error {
60+
outDir, err := filepath.Abs(library.Output)
61+
if err != nil {
62+
return err
63+
}
64+
args := []string{"-w", filepath.Join(outDir, library.Name)}
65+
// TODO(https://github.com/googleapis/librarian/issues/4297), refactor this function
66+
// to use import path.
67+
snippetDir := snippetDirectory(outDir, library.Name)
68+
if _, err := os.Stat(snippetDir); err == nil {
69+
args = append(args, snippetDir)
70+
}
71+
return command.Run(ctx, "goimports", args...)
72+
}
73+
5874
// generate generates a Go client library.
5975
func generate(ctx context.Context, library *config.Library, googleapisDir string) error {
6076
if len(library.APIs) == 0 {
@@ -139,22 +155,6 @@ func generate(ctx context.Context, library *config.Library, googleapisDir string
139155
return nil
140156
}
141157

142-
// Format formats a generated Go library.
143-
func Format(ctx context.Context, library *config.Library) error {
144-
outDir, err := filepath.Abs(library.Output)
145-
if err != nil {
146-
return err
147-
}
148-
args := []string{"-w", filepath.Join(outDir, library.Name)}
149-
// TODO(https://github.com/googleapis/librarian/issues/4297), refactor this function
150-
// to use import path.
151-
snippetDir := snippetDirectory(outDir, library.Name)
152-
if _, err := os.Stat(snippetDir); err == nil {
153-
args = append(args, snippetDir)
154-
}
155-
return command.Run(ctx, "goimports", args...)
156-
}
157-
158158
func generateAPI(ctx context.Context, api *config.API, library *config.Library, googleapisDir, outdir string) error {
159159
goAPI := findGoAPI(library, api.Path)
160160
if goAPI == nil {
@@ -282,16 +282,6 @@ func fixVersioning(outputDir, library, modPath string) error {
282282
return nil
283283
}
284284

285-
// modulePath returns the Go module path for the library. ModulePathVersion is
286-
// set for modules at v2+, e.g. "cloud.google.com/go/pubsub/v2".
287-
func modulePath(library *config.Library) string {
288-
path := "cloud.google.com/go/" + library.Name
289-
if library.Go != nil && library.Go.ModulePathVersion != "" {
290-
path += "/" + library.Go.ModulePathVersion
291-
}
292-
return path
293-
}
294-
295285
func collectProtoFiles(googleapisDir, apiPath string, nestedProtos []string) ([]string, error) {
296286
apiDir := filepath.Join(googleapisDir, apiPath)
297287
entries, err := os.ReadDir(apiDir)
@@ -392,15 +382,6 @@ func updateSnippetDirectory(baseDir, version string) error {
392382
})
393383
}
394384

395-
func initModule(ctx context.Context, dir, modPath string) error {
396-
initArgs := []string{"go", "mod", "init", modPath}
397-
if err := command.RunInDir(ctx, dir, initArgs[0], initArgs[1:]...); err != nil {
398-
return err
399-
}
400-
tidyArgs := []string{"go", "mod", "tidy"}
401-
return command.RunInDir(ctx, dir, tidyArgs[0], tidyArgs[1:]...)
402-
}
403-
404385
// releaseLevel determines the release level for an API based on the API path and the library's current version.
405386
func releaseLevel(apiPath, version string) (string, error) {
406387
apiVersion := filepath.Base(apiPath)

internal/librarian/golang/generate_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package golang
1616

1717
import (
1818
"errors"
19-
"io/fs"
2019
"os"
2120
"path/filepath"
2221
"strings"
@@ -1080,22 +1079,3 @@ func TestBuildGAPICOpts(t *testing.T) {
10801079
})
10811080
}
10821081
}
1083-
1084-
func TestInitModule(t *testing.T) {
1085-
testhelper.RequireCommand(t, "go")
1086-
outDir := t.TempDir()
1087-
// Write an import so go mod tidy can generate a go.sum file.
1088-
content := []byte("package main\nimport _ \"golang.org/x/text\"\n")
1089-
if err := os.WriteFile(filepath.Join(outDir, "main.go"), content, 0644); err != nil {
1090-
t.Fatal(err)
1091-
}
1092-
if err := initModule(t.Context(), outDir, "example.com/testmod"); err != nil {
1093-
t.Fatal(err)
1094-
}
1095-
if _, err := os.Stat(filepath.Join(outDir, "go.mod")); errors.Is(err, fs.ErrNotExist) {
1096-
t.Errorf("go.mod does not exist")
1097-
}
1098-
if _, err := os.Stat(filepath.Join(outDir, "go.sum")); errors.Is(err, fs.ErrNotExist) {
1099-
t.Errorf("go.sum does not exist")
1100-
}
1101-
}

internal/librarian/golang/module.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
package golang
1616

1717
import (
18+
"context"
1819
"errors"
1920
"fmt"
2021
"path/filepath"
2122
"strings"
2223

24+
"github.com/googleapis/librarian/internal/command"
2325
"github.com/googleapis/librarian/internal/config"
2426
"github.com/googleapis/librarian/internal/serviceconfig"
2527
)
@@ -68,6 +70,24 @@ func findGoAPI(library *config.Library, apiPath string) *config.GoAPI {
6870
return nil
6971
}
7072

73+
// modulePath returns the Go module path for the library. ModulePathVersion is
74+
// set for modules at v2+, e.g. "cloud.google.com/go/pubsub/v2".
75+
func modulePath(library *config.Library) string {
76+
path := "cloud.google.com/go/" + library.Name
77+
if library.Go != nil && library.Go.ModulePathVersion != "" {
78+
path += "/" + library.Go.ModulePathVersion
79+
}
80+
return path
81+
}
82+
83+
// initModule initializes and tidies a Go module in the given directory.
84+
func initModule(ctx context.Context, dir, modPath string) error {
85+
if err := command.RunInDir(ctx, dir, "go", "mod", "init", modPath); err != nil {
86+
return err
87+
}
88+
return command.RunInDir(ctx, dir, "go", "mod", "tidy")
89+
}
90+
7191
// defaultImportPathAndClientPkg returns the default Go import path and client package name
7292
// based on the provided API path.
7393
//

internal/librarian/golang/module_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
package golang
1616

1717
import (
18+
"os"
1819
"path/filepath"
1920
"testing"
2021

2122
"github.com/google/go-cmp/cmp"
2223
"github.com/googleapis/librarian/internal/config"
24+
"github.com/googleapis/librarian/internal/testhelper"
2325
)
2426

2527
func TestFill(t *testing.T) {
@@ -261,3 +263,22 @@ func TestSnippetDirectory(t *testing.T) {
261263
t.Errorf("mismatch (-want +got):\n%s", diff)
262264
}
263265
}
266+
267+
func TestInitModule(t *testing.T) {
268+
testhelper.RequireCommand(t, "go")
269+
outDir := t.TempDir()
270+
// Write an import so go mod tidy can generate a go.sum file.
271+
content := []byte("package main\nimport _ \"golang.org/x/text\"\n")
272+
if err := os.WriteFile(filepath.Join(outDir, "main.go"), content, 0644); err != nil {
273+
t.Fatal(err)
274+
}
275+
if err := initModule(t.Context(), outDir, "example.com/testmod"); err != nil {
276+
t.Fatal(err)
277+
}
278+
if _, err := os.Stat(filepath.Join(outDir, "go.mod")); err != nil {
279+
t.Errorf("expected go.mod to exist, but Stat failed: %v", err)
280+
}
281+
if _, err := os.Stat(filepath.Join(outDir, "go.sum")); err != nil {
282+
t.Errorf("expected go.sum to exist, but Stat failed: %v", err)
283+
}
284+
}

0 commit comments

Comments
 (0)