@@ -22,7 +22,6 @@ import (
2222 "context"
2323 "encoding/json"
2424 "fmt"
25- "io"
2625 "log/slog"
2726 "os"
2827 "os/exec"
@@ -135,10 +134,10 @@ type ReleaseInitRequest struct {
135134 // Output specifies the empty output directory into which the command should
136135 // generate code.
137136 Output string
138- // partialRepoDir is the local root directory of language repository contains
137+ // PartialRepoDir is the local root directory of language repository contains
139138 // files that make up libraries and global files.
140139 // This is the directory that container can access.
141- partialRepoDir string
140+ PartialRepoDir string
142141}
143142
144143// New constructs a Docker instance which will invoke the specified
@@ -253,7 +252,7 @@ func (c *Docker) Configure(ctx context.Context, request *ConfigureRequest) (stri
253252
254253// ReleaseInit initiates a release for a given language repository.
255254func (c * Docker ) ReleaseInit (ctx context.Context , request * ReleaseInitRequest ) error {
256- requestFilePath := filepath .Join (request .Cfg . Repo , config .LibrarianDir , config .ReleaseInitRequest )
255+ requestFilePath := filepath .Join (request .PartialRepoDir , config .LibrarianDir , config .ReleaseInitRequest )
257256 if err := writeLibrarianState (request .State , requestFilePath ); err != nil {
258257 return err
259258 }
@@ -275,14 +274,10 @@ func (c *Docker) ReleaseInit(ctx context.Context, request *ReleaseInitRequest) e
275274 commandArgs = append (commandArgs , fmt .Sprintf ("--library-version=%s" , request .LibraryVersion ))
276275 }
277276
278- if err := setupPartialRepo (request ); err != nil {
279- return err
280- }
281-
282- librarianDir := filepath .Join (request .partialRepoDir , config .LibrarianDir )
277+ librarianDir := filepath .Join (request .PartialRepoDir , config .LibrarianDir )
283278 mounts := []string {
284279 fmt .Sprintf ("%s:/librarian" , librarianDir ),
285- fmt .Sprintf ("%s:/repo:ro" , request .partialRepoDir ), // readonly volume
280+ fmt .Sprintf ("%s:/repo:ro" , request .PartialRepoDir ), // readonly volume
286281 fmt .Sprintf ("%s:/output" , request .Output ),
287282 }
288283
@@ -347,96 +342,6 @@ func (c *Docker) runCommand(cmdName string, args ...string) error {
347342 return err
348343}
349344
350- // setupPartialRepo copies the following files from the [config.Config.Repo] to
351- // partialRepoDir in the given ReleaseInitRequest:
352- //
353- // 1. all directories that make up all libraries, or one library, if the library
354- // ID is specified.
355- //
356- // 2. the .librarian directory.
357- //
358- // 3. global files declared in config.yaml.
359- func setupPartialRepo (request * ReleaseInitRequest ) error {
360- if request .partialRepoDir == "" {
361- request .partialRepoDir = filepath .Join (request .Cfg .WorkRoot , "release-init" )
362- }
363- dst := request .partialRepoDir
364- src := request .Cfg .Repo
365- if err := os .MkdirAll (dst , 0755 ); err != nil {
366- return fmt .Errorf ("failed to make directory: %w" , err )
367- }
368-
369- for _ , library := range request .State .Libraries {
370- // Only copy files that make up one library.
371- if request .LibraryID != "" {
372- if library .ID == request .LibraryID {
373- if err := copyOneLibrary (dst , src , library ); err != nil {
374- return err
375- }
376- break
377- }
378- continue
379- }
380-
381- // Copy all files make up all libraries.
382- if err := copyOneLibrary (dst , src , library ); err != nil {
383- return err
384- }
385- }
386-
387- // Copy the .librarian directory.
388- if err := os .CopyFS (
389- filepath .Join (dst , config .LibrarianDir ),
390- os .DirFS (filepath .Join (src , config .LibrarianDir ))); err != nil {
391- return fmt .Errorf ("failed to copy librarian dir to %s: %w" , dst , err )
392- }
393-
394- // Copy global files declared in librarian config.
395- for _ , globalFile := range request .LibrarianConfig .GlobalFilesAllowlist {
396- dstPath := filepath .Join (dst , globalFile .Path )
397- srcPath := filepath .Join (src , globalFile .Path )
398- if err := copyFile (dstPath , srcPath ); err != nil {
399- return err
400- }
401- }
402-
403- return nil
404- }
405-
406- func copyOneLibrary (dst , src string , library * config.LibraryState ) error {
407- for _ , srcRoot := range library .SourceRoots {
408- dstPath := filepath .Join (dst , srcRoot )
409- srcPath := filepath .Join (src , srcRoot )
410- if err := os .CopyFS (dstPath , os .DirFS (srcPath )); err != nil {
411- return fmt .Errorf ("failed to copy %s to %s: %w" , library .ID , dstPath , err )
412- }
413- }
414-
415- return nil
416- }
417-
418- func copyFile (dst , src string ) (err error ) {
419- sourceFile , err := os .Open (src )
420- if err != nil {
421- return fmt .Errorf ("failed to open file: %q: %w" , src , err )
422- }
423- defer sourceFile .Close ()
424-
425- if err := os .MkdirAll (filepath .Dir (dst ), 0755 ); err != nil {
426- return fmt .Errorf ("failed to make directory: %s" , src )
427- }
428-
429- destinationFile , err := os .Create (dst )
430- if err != nil {
431- return fmt .Errorf ("failed to create file: %s" , dst )
432- }
433- defer destinationFile .Close ()
434-
435- _ , err = io .Copy (destinationFile , sourceFile )
436-
437- return err
438- }
439-
440345func writeLibraryState (state * config.LibrarianState , libraryID , jsonFilePath string ) error {
441346 if err := os .MkdirAll (filepath .Dir (jsonFilePath ), 0755 ); err != nil {
442347 return fmt .Errorf ("failed to make directory: %w" , err )
0 commit comments