Skip to content

Commit 65fa7ed

Browse files
authored
feat(sidekick): update supports different roots (#2473)
1 parent 15d63c2 commit 65fa7ed

File tree

6 files changed

+22
-15
lines changed

6 files changed

+22
-15
lines changed

internal/sidekick/cmdline.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type CommandLine struct {
2525
Output string
2626
Language string
2727
Codec map[string]string
28+
UpdatedRoot string
2829
DryRun bool
2930
}
3031

@@ -38,4 +39,5 @@ var (
3839
flagLanguage string
3940
codecOpts = map[string]string{}
4041
dryrun bool
42+
updatedRoot string
4143
)

internal/sidekick/command.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func (c *command) parseCmdLine(args []string) (*CommandLine, error) {
147147
Output: output,
148148
Codec: codecOpts,
149149
DryRun: dryrun,
150+
UpdatedRoot: updatedRoot,
150151
}, nil
151152
}
152153

internal/sidekick/internal/config/update_root_config.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ type githubRepo struct {
4747
}
4848

4949
// UpdateRootConfig updates the root configuration file with the latest SHA from GitHub.
50-
func UpdateRootConfig(rootConfig *Config) error {
50+
func UpdateRootConfig(rootConfig *Config, rootName string) error {
51+
if rootName == "" {
52+
rootName = defaultRoot
53+
}
5154
endpoints := githubConfig(rootConfig)
52-
repo, err := githubRepoFromTarballLink(rootConfig, defaultRoot)
55+
repo, err := githubRepoFromTarballLink(rootConfig, rootName)
5356
if err != nil {
5457
return err
5558
}
@@ -73,7 +76,7 @@ func UpdateRootConfig(rootConfig *Config) error {
7376
if err != nil {
7477
return err
7578
}
76-
newContents, err := updateRootConfigContents(defaultRoot, contents, endpoints, repo, latestSha, newSha256)
79+
newContents, err := updateRootConfigContents(rootName, contents, endpoints, repo, latestSha, newSha256)
7780
if err != nil {
7881
return err
7982
}

internal/sidekick/internal/config/update_root_config_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,23 @@ func TestUpdateRootConfig(t *testing.T) {
7272
SpecificationFormat: "protobuf",
7373
},
7474
Source: map[string]string{
75-
"github-api": server.URL,
76-
"github": server.URL,
77-
"googleapis-root": fmt.Sprintf("%s/googleapis/googleapis/archive/old.tar.gz", server.URL),
78-
"googleapis-sha256": "old-sha-unused",
75+
"github-api": server.URL,
76+
"github": server.URL,
77+
"test-root": fmt.Sprintf("%s/googleapis/googleapis/archive/old.tar.gz", server.URL),
78+
"test-sha256": "old-sha-unused",
7979
},
8080
Codec: map[string]string{},
8181
}
8282
if err := WriteSidekickToml(".", rootConfig); err != nil {
8383
t.Fatal(err)
8484
}
8585

86-
if err := UpdateRootConfig(rootConfig); err != nil {
86+
if err := UpdateRootConfig(rootConfig, "test"); err != nil {
8787
t.Fatal(err)
8888
}
8989

9090
got := &Config{}
91-
contents, err := os.ReadFile(path.Join(tempDir, ".sidekick.toml"))
91+
contents, err := os.ReadFile(path.Join(tempDir, configName))
9292
if err != nil {
9393
t.Fatal(err)
9494
}
@@ -98,10 +98,10 @@ func TestUpdateRootConfig(t *testing.T) {
9898
want := &Config{
9999
General: rootConfig.General,
100100
Source: map[string]string{
101-
"github-api": server.URL,
102-
"github": server.URL,
103-
"googleapis-root": fmt.Sprintf("%s/googleapis/googleapis/archive/%s.tar.gz", server.URL, latestSha),
104-
"googleapis-sha256": latestShaContentsHash,
101+
"github-api": server.URL,
102+
"github": server.URL,
103+
"test-root": fmt.Sprintf("%s/googleapis/googleapis/archive/%s.tar.gz", server.URL, latestSha),
104+
"test-sha256": latestShaContentsHash,
105105
},
106106
}
107107

@@ -258,7 +258,7 @@ func TestUpdateRootConfigErrors(t *testing.T) {
258258
rootConfig.Source[k] = v
259259
}
260260
test.Setup(rootConfig)
261-
if err := UpdateRootConfig(rootConfig); err == nil {
261+
if err := UpdateRootConfig(rootConfig, ""); err == nil {
262262
t.Errorf("expected an error with configuration %v", rootConfig)
263263
t.Fatal(err)
264264
}

internal/sidekick/sidekick.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var cmdSidekick = newCommand(
3636
addFlagString(&output, "output", "the path within project-root to put generated files").
3737
addFlagString(&flagLanguage, "language", "the generated language").
3838
addFlagBool(&dryrun, "dry-run", false, "do a dry-run: load the configuration, but do not perform any changes.").
39+
addFlagString(&updatedRoot, "updated-root", "update a specific *-root commit SHA, defaults to `googleapis`.").
3940
addFlagFunc("source-option", "source options", func(opt string) error {
4041
components := strings.SplitN(opt, "=", 2)
4142
if len(components) != 2 {

internal/sidekick/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This command will update the googleapis-root and googleapis-sha256 fields in the
3131
}
3232

3333
func update(rootConfig *config.Config, cmdLine *CommandLine) error {
34-
if err := config.UpdateRootConfig(rootConfig); err != nil {
34+
if err := config.UpdateRootConfig(rootConfig, cmdLine.UpdatedRoot); err != nil {
3535
return err
3636
}
3737
// Reload the freshly minted configuration.

0 commit comments

Comments
 (0)