@@ -262,7 +262,8 @@ func (ts *TargetState) Get(target string) (Entry, error) {
262262 return ts .findEntry (targetName )
263263}
264264
265- func (ts * TargetState ) AddArchive (r * tar.Reader , destinationDir string , stripComponents int , mutator Mutator ) error {
265+ // Import imports an archive.
266+ func (ts * TargetState ) Import (r * tar.Reader , destinationDir string , stripComponents int , mutator Mutator ) error {
266267 for {
267268 header , err := r .Next ()
268269 if err == io .EOF {
@@ -272,7 +273,7 @@ func (ts *TargetState) AddArchive(r *tar.Reader, destinationDir string, stripCom
272273 }
273274 switch header .Typeflag {
274275 case tar .TypeDir , tar .TypeReg , tar .TypeSymlink :
275- if err := ts .addArchiveHeader (r , header , destinationDir , stripComponents , mutator ); err != nil {
276+ if err := ts .importHeader (r , header , destinationDir , stripComponents , mutator ); err != nil {
276277 return err
277278 }
278279 case tar .TypeXGlobalHeader :
@@ -367,7 +368,59 @@ func (ts *TargetState) Populate(fs vfs.FS) error {
367368 })
368369}
369370
370- func (ts * TargetState ) addArchiveHeader (r * tar.Reader , header * tar.Header , destinationDir string , stripComponents int , mutator Mutator ) error {
371+ func (ts * TargetState ) executeTemplate (fs vfs.FS , path string ) ([]byte , error ) {
372+ data , err := fs .ReadFile (path )
373+ if err != nil {
374+ return nil , err
375+ }
376+ return ts .executeTemplateData (path , data )
377+ }
378+
379+ func (ts * TargetState ) executeTemplateData (name string , data []byte ) (_ []byte , err error ) {
380+ tmpl , err := template .New (name ).Option ("missingkey=error" ).Funcs (ts .Funcs ).Parse (string (data ))
381+ if err != nil {
382+ return nil , fmt .Errorf ("%s: %v" , name , err )
383+ }
384+ defer func () {
385+ if r := recover (); r != nil {
386+ if tfe , ok := r .(templateFuncError ); ok {
387+ err = tfe .err
388+ } else {
389+ panic (r )
390+ }
391+ }
392+ }()
393+ output := & bytes.Buffer {}
394+ if err = tmpl .Execute (output , ts .Data ); err != nil {
395+ return nil , fmt .Errorf ("%s: %v" , name , err )
396+ }
397+ return output .Bytes (), nil
398+ }
399+
400+ func (ts * TargetState ) findEntries (dirNames []string ) (map [string ]Entry , error ) {
401+ entries := ts .Entries
402+ for i , dirName := range dirNames {
403+ if entry , ok := entries [dirName ]; ! ok {
404+ return nil , os .ErrNotExist
405+ } else if dir , ok := entry .(* Dir ); ok {
406+ entries = dir .Entries
407+ } else {
408+ return nil , fmt .Errorf ("%s: not a directory" , filepath .Join (dirNames [:i + 1 ]... ))
409+ }
410+ }
411+ return entries , nil
412+ }
413+
414+ func (ts * TargetState ) findEntry (name string ) (Entry , error ) {
415+ names := splitPathList (name )
416+ entries , err := ts .findEntries (names [:len (names )- 1 ])
417+ if err != nil {
418+ return nil , err
419+ }
420+ return entries [names [len (names )- 1 ]], nil
421+ }
422+
423+ func (ts * TargetState ) importHeader (r io.Reader , header * tar.Header , destinationDir string , stripComponents int , mutator Mutator ) error {
371424 targetPath := header .Name
372425 if stripComponents > 0 {
373426 targetPath = filepath .Join (strings .Split (targetPath , string (os .PathSeparator ))[stripComponents :]... )
@@ -513,55 +566,3 @@ func (ts *TargetState) addArchiveHeader(r *tar.Reader, header *tar.Header, desti
513566 return fmt .Errorf ("%s: unspported typeflag '%c'" , header .Name , header .Typeflag )
514567 }
515568}
516-
517- func (ts * TargetState ) executeTemplate (fs vfs.FS , path string ) ([]byte , error ) {
518- data , err := fs .ReadFile (path )
519- if err != nil {
520- return nil , err
521- }
522- return ts .executeTemplateData (path , data )
523- }
524-
525- func (ts * TargetState ) executeTemplateData (name string , data []byte ) (_ []byte , err error ) {
526- tmpl , err := template .New (name ).Option ("missingkey=error" ).Funcs (ts .Funcs ).Parse (string (data ))
527- if err != nil {
528- return nil , fmt .Errorf ("%s: %v" , name , err )
529- }
530- defer func () {
531- if r := recover (); r != nil {
532- if tfe , ok := r .(templateFuncError ); ok {
533- err = tfe .err
534- } else {
535- panic (r )
536- }
537- }
538- }()
539- output := & bytes.Buffer {}
540- if err = tmpl .Execute (output , ts .Data ); err != nil {
541- return nil , fmt .Errorf ("%s: %v" , name , err )
542- }
543- return output .Bytes (), nil
544- }
545-
546- func (ts * TargetState ) findEntries (dirNames []string ) (map [string ]Entry , error ) {
547- entries := ts .Entries
548- for i , dirName := range dirNames {
549- if entry , ok := entries [dirName ]; ! ok {
550- return nil , os .ErrNotExist
551- } else if dir , ok := entry .(* Dir ); ok {
552- entries = dir .Entries
553- } else {
554- return nil , fmt .Errorf ("%s: not a directory" , filepath .Join (dirNames [:i + 1 ]... ))
555- }
556- }
557- return entries , nil
558- }
559-
560- func (ts * TargetState ) findEntry (name string ) (Entry , error ) {
561- names := splitPathList (name )
562- entries , err := ts .findEntries (names [:len (names )- 1 ])
563- if err != nil {
564- return nil , err
565- }
566- return entries [names [len (names )- 1 ]], nil
567- }
0 commit comments