github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/builder/dockerignore/dockerignore.go (about)

     1  package dockerignore
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"io"
     7  	"path/filepath"
     8  	"strings"
     9  )
    10  
    11  // ReadAll reads a .dockerignore file and returns the list of file patterns
    12  // to ignore. Note this will trim whitespace from each line as well
    13  // as use GO's "clean" func to get the shortest/cleanest path for each.
    14  func ReadAll(reader io.ReadCloser) ([]string, error) {
    15  	if reader == nil {
    16  		return nil, nil
    17  	}
    18  	defer reader.Close()
    19  	scanner := bufio.NewScanner(reader)
    20  	var excludes []string
    21  
    22  	for scanner.Scan() {
    23  		pattern := strings.TrimSpace(scanner.Text())
    24  		if pattern == "" {
    25  			continue
    26  		}
    27  		pattern = filepath.Clean(pattern)
    28  		excludes = append(excludes, pattern)
    29  	}
    30  	if err := scanner.Err(); err != nil {
    31  		return nil, fmt.Errorf("Error reading .dockerignore: %v", err)
    32  	}
    33  	return excludes, nil
    34  }