github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/extractor/dir/walk.go (about)

     1  /*
     2   * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   *
     7   */
     8  
     9  package dir
    10  
    11  import (
    12  	"os"
    13  	"path/filepath"
    14  	"strings"
    15  
    16  	"github.com/vchain-us/vcn/pkg/bundle"
    17  )
    18  
    19  func Walk(root string) (files []bundle.Descriptor, err error) {
    20  	files = make([]bundle.Descriptor, 0)
    21  	ignore, err := newIgnoreFileMatcher(root)
    22  	if err != nil {
    23  		return
    24  	}
    25  	err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
    26  		// skip irregular files (e.g. dir, symlink, pipe, socket, device...)
    27  		if !info.Mode().IsRegular() {
    28  			return nil
    29  		}
    30  
    31  		relPath, err := filepath.Rel(root, path)
    32  		if err != nil {
    33  			return err
    34  		}
    35  		// descriptor's path must be OS agnostic
    36  		relPath = filepath.ToSlash(relPath)
    37  
    38  		// skip manifest and files matching the ignore patterns
    39  		if relPath == bundle.ManifestFilename || ignore.Match(strings.Split(relPath, "/"), false) {
    40  			return nil
    41  		}
    42  
    43  		file, err := os.Open(path)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		d, err := bundle.NewDescriptor(relPath, file)
    48  		file.Close()
    49  		if err != nil {
    50  			return err
    51  		}
    52  		files = append(files, *d)
    53  
    54  		return nil
    55  	})
    56  	return
    57  }