github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/utils/collector/local_context.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package collector
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"path/filepath"
    21  
    22  	"github.com/alibaba/sealer/logger"
    23  	fsutil "github.com/tonistiigi/fsutil/copy"
    24  )
    25  
    26  type localCollector struct {
    27  }
    28  
    29  func (l localCollector) Collect(buildContext, src, savePath string) error {
    30  	xattrErrorHandler := func(dst, src, key string, err error) error {
    31  		logger.Warn(err)
    32  		return nil
    33  	}
    34  	opt := []fsutil.Opt{
    35  		fsutil.WithXAttrErrorHandler(xattrErrorHandler),
    36  	}
    37  
    38  	m, err := fsutil.ResolveWildcards(buildContext, src, true)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	if len(m) == 0 {
    44  		return fmt.Errorf("%s not found", src)
    45  	}
    46  
    47  	dir, file := filepath.Split(savePath)
    48  	for _, s := range m {
    49  		if filepath.Base(s) == file {
    50  			savePath = dir
    51  		}
    52  		if err := fsutil.Copy(context.TODO(), buildContext, s, savePath, filepath.Base(s), opt...); err != nil {
    53  			return err
    54  		}
    55  	}
    56  	return nil
    57  }
    58  
    59  func NewLocalCollector() Collector {
    60  	return localCollector{}
    61  }