@@ -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+
6467func 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
119142type 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 \n This 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 \n This 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