Skip to content

Commit 71f7d3b

Browse files
authored
refactor(internal/config): add LibrarianYAML const for config filename (#4849)
Add a config.LibrarianYAML constant and uses it everywhere, so the filename is defined in one place.
1 parent df6e0a5 commit 71f7d3b

19 files changed

Lines changed: 46 additions & 47 deletions

internal/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package config
1818

1919
//go:generate go run -tags configdocgen ../../cmd/config_doc_generate.go -input . -output ../../doc/config-schema.md
2020

21+
// LibrarianYAML is the filename for the librarian configuration file.
22+
const LibrarianYAML = "librarian.yaml"
23+
2124
// Config represents a librarian.yaml configuration file.
2225
type Config struct {
2326
// Language is the language for this workspace (go, python, rust).

internal/librarian/add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func addCommand() *cli.Command {
4848
if len(apis) == 0 {
4949
return errMissingAPI
5050
}
51-
cfg, err := yaml.Read[config.Config](librarianConfigPath)
51+
cfg, err := yaml.Read[config.Config](config.LibrarianYAML)
5252
if err != nil {
5353
return err
5454
}

internal/librarian/add_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func TestAddLibraryCommand(t *testing.T) {
9999
cfg.Default.Output = "output"
100100
cfg.Libraries = test.initialLibraries
101101
cfg.Sources.Googleapis.Dir = googleapisDir
102-
if err := yaml.Write(librarianConfigPath, cfg); err != nil {
102+
if err := yaml.Write(config.LibrarianYAML, cfg); err != nil {
103103
t.Fatal(err)
104104
}
105105
err = runAdd(t.Context(), cfg, test.apis...)
@@ -113,7 +113,7 @@ func TestAddLibraryCommand(t *testing.T) {
113113
t.Fatal(err)
114114
}
115115

116-
gotCfg, err := yaml.Read[config.Config](librarianConfigPath)
116+
gotCfg, err := yaml.Read[config.Config](config.LibrarianYAML)
117117
if err != nil {
118118
t.Fatal(err)
119119
}
@@ -174,7 +174,7 @@ func TestAddCommand(t *testing.T) {
174174
cfg.Default.Output = "output"
175175
cfg.Libraries = nil
176176
cfg.Sources.Googleapis.Dir = googleapisDir
177-
if err := yaml.Write(librarianConfigPath, cfg); err != nil {
177+
if err := yaml.Write(config.LibrarianYAML, cfg); err != nil {
178178
t.Fatal(err)
179179
}
180180
args := append([]string{"librarian", "add"}, test.apis...)
@@ -189,7 +189,7 @@ func TestAddCommand(t *testing.T) {
189189
t.Fatal(err)
190190
}
191191

192-
gotCfg, err := yaml.Read[config.Config](librarianConfigPath)
192+
gotCfg, err := yaml.Read[config.Config](config.LibrarianYAML)
193193
if err != nil {
194194
t.Fatal(err)
195195
}
@@ -253,7 +253,7 @@ func TestAddLibrary(t *testing.T) {
253253
Output: "output/existinglib",
254254
},
255255
}
256-
if err := yaml.Write(librarianConfigPath, cfg); err != nil {
256+
if err := yaml.Write(config.LibrarianYAML, cfg); err != nil {
257257
t.Fatal(err)
258258
}
259259
gotName, cfg, err := addLibrary(cfg, test.apis...)

internal/librarian/bump.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Examples:
9494
if all && versionOverride != "" {
9595
return errBothVersionAndAllFlag
9696
}
97-
cfg, err := yaml.Read[config.Config](librarianConfigPath)
97+
cfg, err := yaml.Read[config.Config](config.LibrarianYAML)
9898
if err != nil {
9999
return err
100100
}
@@ -277,7 +277,7 @@ func deriveNextVersion(ctx context.Context, gitExe string, cfg *config.Config, l
277277
}
278278

279279
func loadBranchLibraryVersion(ctx context.Context, gitExe, remote, branch, libName string) (string, error) {
280-
branchLibrarianCfgFile, err := git.ShowFileAtRemoteBranch(ctx, gitExe, remote, branch, librarianConfigPath)
280+
branchLibrarianCfgFile, err := git.ShowFileAtRemoteBranch(ctx, gitExe, remote, branch, config.LibrarianYAML)
281281
if err != nil {
282282
return "", err
283283
}
@@ -338,7 +338,7 @@ func findReleasedLibraries(cfgBefore, cfgAfter *config.Config) ([]string, error)
338338
// release process has not yet been completed (e.g. to find which commit
339339
// *should* be tagged).
340340
func findLatestReleaseCommitHash(ctx context.Context, gitExe string) (string, error) {
341-
commits, err := git.FindCommitsForPath(ctx, gitExe, librarianConfigPath)
341+
commits, err := git.FindCommitsForPath(ctx, gitExe, config.LibrarianYAML)
342342
if err != nil {
343343
return "", err
344344
}
@@ -348,7 +348,7 @@ func findLatestReleaseCommitHash(ctx context.Context, gitExe string) (string, er
348348
var candidateConfig *config.Config
349349
candidateCommit := ""
350350
for _, commit := range commits {
351-
commitCfgContent, err := git.ShowFileAtRevision(ctx, gitExe, commit, librarianConfigPath)
351+
commitCfgContent, err := git.ShowFileAtRevision(ctx, gitExe, commit, config.LibrarianYAML)
352352
if err != nil {
353353
return "", err
354354
}

internal/librarian/bump_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestBumpCommand(t *testing.T) {
102102
t.Fatal(err)
103103
}
104104

105-
got, err := yaml.Read[config.Config](librarianConfigPath)
105+
got, err := yaml.Read[config.Config](config.LibrarianYAML)
106106
if err != nil {
107107
t.Fatal(err)
108108
}
@@ -135,7 +135,7 @@ func TestBumpCommandDeriveOutput(t *testing.T) {
135135
t.Fatal(err)
136136
}
137137

138-
got, err := yaml.Read[config.Config](librarianConfigPath)
138+
got, err := yaml.Read[config.Config](config.LibrarianYAML)
139139
if err != nil {
140140
t.Fatal(err)
141141
}
@@ -1005,13 +1005,13 @@ func TestFindLatestReleaseCommitHash_Error(t *testing.T) {
10051005
{
10061006
name: "invalid config file",
10071007
setup: func(cfg *config.Config) {
1008-
writeFileAndCommit(t, librarianConfigPath, []byte("not a config file"), "broke config file")
1008+
writeFileAndCommit(t, config.LibrarianYAML, []byte("not a config file"), "broke config file")
10091009
},
10101010
},
10111011
{
10121012
name: "deleted config file",
10131013
setup: func(cfg *config.Config) {
1014-
if err := os.Remove(librarianConfigPath); err != nil {
1014+
if err := os.Remove(config.LibrarianYAML); err != nil {
10151015
t.Fatal(err)
10161016
}
10171017
testhelper.RunGit(t, "commit", "-m", "deleted config file", ".")
@@ -1180,7 +1180,7 @@ func TestLegacyRustBump(t *testing.T) {
11801180
t.Fatal(err)
11811181
}
11821182

1183-
got, err := yaml.Read[config.Config](librarianConfigPath)
1183+
got, err := yaml.Read[config.Config](config.LibrarianYAML)
11841184
if err != nil {
11851185
t.Fatal(err)
11861186
}
@@ -1364,7 +1364,7 @@ func writeConfigAndCommitWithMessage(t *testing.T, cfg *config.Config, message s
13641364
if err != nil {
13651365
t.Fatal(err)
13661366
}
1367-
writeFileAndCommit(t, librarianConfigPath, data, message)
1367+
writeFileAndCommit(t, config.LibrarianYAML, data, message)
13681368
}
13691369

13701370
func writeFileAndCommit(t *testing.T, path string, content []byte, message string) {

internal/librarian/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func generateCommand() *cli.Command {
5959
if all && libraryName != "" {
6060
return errBothLibraryAndAllFlag
6161
}
62-
cfg, err := yaml.Read[config.Config](librarianConfigPath)
62+
cfg, err := yaml.Read[config.Config](config.LibrarianYAML)
6363
if err != nil {
6464
return err
6565
}

internal/librarian/generate_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestGenerateCommand(t *testing.T) {
114114
},
115115
},
116116
}
117-
if err := yaml.Write(filepath.Join(tempDir, librarianConfigPath), cfg); err != nil {
117+
if err := yaml.Write(filepath.Join(tempDir, config.LibrarianYAML), cfg); err != nil {
118118
t.Fatal(err)
119119
}
120120

@@ -239,7 +239,7 @@ libraries:
239239
apis:
240240
- path: google/cloud/texttospeech/v1
241241
`, googleapisDir, lib1, lib1Output, lib2, lib2Output)
242-
if err := os.WriteFile(filepath.Join(tempDir, librarianConfigPath), []byte(configContent), 0644); err != nil {
242+
if err := os.WriteFile(filepath.Join(tempDir, config.LibrarianYAML), []byte(configContent), 0644); err != nil {
243243
t.Fatal(err)
244244
}
245245
err := Run(t.Context(), test.args...)
@@ -305,7 +305,7 @@ libraries:
305305
- path: google/cloud/secretmanager/v1
306306
`, googleapisDir)
307307

308-
if err := os.WriteFile(filepath.Join(tempDir, librarianConfigPath), []byte(configContent), 0644); err != nil {
308+
if err := os.WriteFile(filepath.Join(tempDir, config.LibrarianYAML), []byte(configContent), 0644); err != nil {
309309
t.Fatal(err)
310310
}
311311

internal/librarian/librarian.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ import (
2828
// ErrLibraryNotFound is returned when the specified library is not found in config.
2929
var ErrLibraryNotFound = errors.New("library not found")
3030

31-
const (
32-
librarianConfigPath = "librarian.yaml"
33-
)
34-
3531
// Run executes the librarian command with the given arguments.
3632
func Run(ctx context.Context, args ...string) error {
3733
cmd := &cli.Command{

internal/librarian/publish.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func publishCommand() *cli.Command {
5454
},
5555
},
5656
Action: func(ctx context.Context, cmd *cli.Command) error {
57-
cfg, err := yaml.Read[config.Config](librarianConfigPath)
57+
cfg, err := yaml.Read[config.Config](config.LibrarianYAML)
5858
if err != nil {
5959
return err
6060
}
@@ -103,7 +103,7 @@ func publish(ctx context.Context, cfg *config.Config, releaseCommit string, exec
103103
return err
104104
}
105105
// Reload the config after checking out the release commit.
106-
cfg, err = yaml.Read[config.Config](librarianConfigPath)
106+
cfg, err = yaml.Read[config.Config](config.LibrarianYAML)
107107
if err != nil {
108108
return err
109109
}
@@ -112,7 +112,7 @@ func publish(ctx context.Context, cfg *config.Config, releaseCommit string, exec
112112
// findLatestReleaseCommitHash, but keeps the interface simple - and means
113113
// that if we want to be able to specify the release commit directly, we
114114
// can skip findLatestReleaseCommitHash entirely.)
115-
cfgContentBeforeCommit, err := git.ShowFileAtRevision(ctx, gitExe, "HEAD~", librarianConfigPath)
115+
cfgContentBeforeCommit, err := git.ShowFileAtRevision(ctx, gitExe, "HEAD~", config.LibrarianYAML)
116116
if err != nil {
117117
return err
118118
}

internal/librarian/tag.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func tagCommand() *cli.Command {
5252
},
5353
},
5454
Action: func(ctx context.Context, cmd *cli.Command) error {
55-
cfg, err := yaml.Read[config.Config](librarianConfigPath)
55+
cfg, err := yaml.Read[config.Config](config.LibrarianYAML)
5656
if err != nil {
5757
return err
5858
}
@@ -80,7 +80,7 @@ func tag(ctx context.Context, cfg *config.Config, releaseCommit string, createRe
8080
}
8181
releaseCommit = latestReleaseCommit
8282
}
83-
releaseCommitCfgContent, err := git.ShowFileAtRevision(ctx, gitExe, releaseCommit, librarianConfigPath)
83+
releaseCommitCfgContent, err := git.ShowFileAtRevision(ctx, gitExe, releaseCommit, config.LibrarianYAML)
8484
if err != nil {
8585
return err
8686
}
@@ -93,7 +93,7 @@ func tag(ctx context.Context, cfg *config.Config, releaseCommit string, createRe
9393
// findLatestReleaseCommitHash, but keeps the interface simple - and means
9494
// that if we specify the release commit directly, we can skip
9595
// findLatestReleaseCommitHash entirely.)
96-
beforeReleaseCommitCfgContent, err := git.ShowFileAtRevision(ctx, gitExe, releaseCommit+"~", librarianConfigPath)
96+
beforeReleaseCommitCfgContent, err := git.ShowFileAtRevision(ctx, gitExe, releaseCommit+"~", config.LibrarianYAML)
9797
if err != nil {
9898
return err
9999
}

0 commit comments

Comments
 (0)