Skip to content

Commit 2972d19

Browse files
authored
feat(internal/carver): support carving batches (#4623)
Refactoring to make it so we can carver multiple modules at once. Did a test generation to ensure everything still works.
1 parent 18ff070 commit 2972d19

File tree

2 files changed

+55
-35
lines changed

2 files changed

+55
-35
lines changed

internal/carver/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ This is a tool used to carve out new modules in cloud.google.com/go.
77
```bash
88
go run cmd/main.go \
99
-parent=/path/to/google-cloud-go \
10-
-child=asset \
11-
-repo-metadata=/path/to/google-cloud-go/internal/.repo-metadata-full.json
10+
-repo-metadata=/path/to/google-cloud-go/internal/.repo-metadata-full.json \
11+
-child=asset
1212
```

internal/carver/cmd/main.go

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"sort"
3030
"strconv"
3131
"strings"
32+
"sync"
3233
"text/template"
3334
"time"
3435
)
@@ -61,6 +62,8 @@ type carver struct {
6162
w io.WriteCloser
6263
}
6364

65+
var once sync.Once
66+
6467
func main() {
6568
parent := flag.String("parent", "", "The path to the parent module. Required.")
6669
child := flag.String("child", "", "The relative path to the child module from the parent module. Required.")
@@ -80,40 +83,60 @@ func main() {
8083
if err != nil {
8184
log.Fatalf("unable to calculate root mod info: %v", err)
8285
}
83-
childMod := childModInfo(rootMod, *child, *childTagVersion)
86+
children := strings.Split(strings.TrimSpace(*child), ",")
87+
var tags []string
88+
for _, child := range children {
89+
child := strings.TrimSpace(child)
90+
childMod := childModInfo(rootMod, child, *childTagVersion)
91+
c := &carver{
92+
rootMod: rootMod,
93+
childMod: childMod,
94+
repoMetadataPath: *repoMetadataPath,
95+
name: *name,
96+
dryRun: *dryRun,
97+
}
98+
ts, err := c.Run()
99+
if err != nil {
100+
log.Println(err)
101+
flag.Usage()
102+
os.Exit(1)
103+
}
104+
tags = append(tags, ts...)
105+
}
106+
if err := gitCommit(rootMod.path, tags, *dryRun); err != nil {
107+
log.Fatal(err)
108+
}
84109

85-
c := &carver{
86-
rootMod: rootMod,
87-
childMod: childMod,
88-
repoMetadataPath: *repoMetadataPath,
89-
name: *name,
90-
dryRun: *dryRun,
110+
log.Println("Successfully carved out modules. Please run the following commands after your changes are merged:")
111+
for _, tag := range tags {
112+
fmt.Fprintf(os.Stdout, "git tag %s $COMMIT_SHA\n", tag)
91113
}
92-
if err := c.Run(); err != nil {
93-
log.Println(err)
94-
flag.Usage()
95-
os.Exit(1)
114+
for _, tag := range tags {
115+
fmt.Fprintf(os.Stdout, "git push origin refs/tags/%s\n", tag)
96116
}
117+
fmt.Fprintf(os.Stdout, "Once tags have propagated open a new PR tidying the new child mods go.sum entries.\n")
97118
}
98119

99-
func (c *carver) Run() error {
120+
func (c *carver) Run() ([]string, error) {
100121
if c.rootMod.path == "" || c.childMod.path == "" || c.repoMetadataPath == "" {
101-
return fmt.Errorf("all required flags were not provided")
122+
return nil, fmt.Errorf("all required flags were not provided")
102123
}
103124
if err := c.CreateChildCommonFiles(); err != nil {
104-
return fmt.Errorf("failed to create readme: %v", err)
125+
return nil, fmt.Errorf("failed to create readme: %v", err)
105126
}
106127
if err := c.CreateChildModule(); err != nil {
107-
return fmt.Errorf("failed to create child module: %v", err)
128+
return nil, fmt.Errorf("failed to create child module: %v", err)
108129
}
109130
if err := c.FixupSnippets(); err != nil {
110-
return fmt.Errorf("failed to update snippet module: %v", err)
111-
}
112-
if err := c.GitCommit(); err != nil {
113-
return fmt.Errorf("failed to create child module: %v", err)
131+
return nil, fmt.Errorf("failed to update snippet module: %v", err)
114132
}
115133

116-
return nil
134+
var tags []string
135+
once.Do(func() {
136+
tags = append(tags, c.rootMod.Tag())
137+
})
138+
tags = append(tags, c.childMod.Tag())
139+
return tags, nil
117140
}
118141

119142
type modInfo struct {
@@ -349,28 +372,25 @@ func (c *carver) FixupSnippets() error {
349372
return nil
350373
}
351374

352-
func (c *carver) GitCommit() error {
375+
func gitCommit(dir string, tags []string, dryRun bool) error {
353376
log.Println("Commiting changes")
354-
if !c.dryRun {
377+
if !dryRun {
355378
cmd := exec.Command("git", "add", "-A")
356-
cmd.Dir = c.rootMod.path
379+
cmd.Dir = dir
357380
if b, err := cmd.Output(); err != nil {
358381
return fmt.Errorf("unable to add changes: %s", b)
359382
}
360-
cmd = exec.Command("git", "commit", "-m",
361-
fmt.Sprintf("chore(%s): carve out sub-module\n\nThis commit will be tagged %s and %s.", c.childMod.tagPrefix, c.rootMod.Tag(), c.childMod.Tag()))
362-
cmd.Dir = c.rootMod.path
383+
var sb strings.Builder
384+
sb.WriteString("chore: carve out sub-modules\n\nThis commit will be tagged:\n")
385+
for _, tag := range tags {
386+
sb.WriteString(fmt.Sprintf("\t- %s\n", tag))
387+
}
388+
cmd = exec.Command("git", "commit", "-m", sb.String())
389+
cmd.Dir = dir
363390
if b, err := cmd.Output(); err != nil {
364391
return fmt.Errorf("unable to commit changes: %s", b)
365392
}
366393
}
367-
log.Println("Successfully carved out module. Please run the following commands after your change is merged:")
368-
fmt.Fprintf(os.Stdout, "git tag %s <COMMIT-SHA>\n", c.rootMod.Tag())
369-
fmt.Fprintf(os.Stdout, "git tag %s <COMMIT-SHA>\n", c.childMod.Tag())
370-
fmt.Fprintf(os.Stdout, "git push origin refs/tags/%s\n", c.rootMod.Tag())
371-
fmt.Fprintf(os.Stdout, "git push origin refs/tags/%s\n", c.childMod.Tag())
372-
fmt.Fprintf(os.Stdout, "Once tags have propagated open a new PR tidying the new child mods go.sum entries.\n")
373-
374394
return nil
375395
}
376396

0 commit comments

Comments
 (0)