// ReadAll reads a .dockerignore file and returns the list of file patterns
// to ignore. Note this will trim whitespace from each line as well
// as use GO's "clean" func to get the shortest/cleanest path for each.
ReadAll reads a .dockerignore file and returns the list of file patterns
to ignore. Note this will trim whitespace from each line as well
as use GO's "clean" func to get the shortest/cleanest path for each.
// ReadAll reads a . dockerignore file and returns the list of file patterns // to ignore . Note this will trim whitespace from each line as well // as use GO ' s " clean " func to get the shortest / cleanest path for each .
func readDockerignorePatterns(reader io.Reader, path string) ([]string, error) {
	if reader == nil {
		return nil, nil
	}
	scanner := bufio.NewScanner(reader)
	var excludes []string
	currentLine := 0
	utf8bom := []byte{0xEF, 0xBB, 0xBF}
	for scanner.Scan() {
		scannedBytes := scanner.Bytes()
		if currentLine == 0 {
			scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom)
		}
		pattern := string(scannedBytes)
		currentLine++
		if strings.HasPrefix(pattern, "#") {
			continue
		}
		pattern = strings.TrimSpace(pattern)
		if pattern == "" {
			continue
		}
		invert := pattern[0] == '!'
		if invert {
			pattern = strings.TrimSpace(pattern[1:])
		}
		if len(pattern) > 0 {
			pattern = filepath.Clean(pattern)
			pattern = filepath.ToSlash(pattern)
			if len(pattern) > 1 && pattern[0] == '/' {
				pattern = pattern[1:]
			}
		}
		if invert {
			pattern = "!" + pattern
		}
		excludes = append(excludes, pattern)
	}
	if err := scanner.Err(); err != nil {
		return nil, fmt.Errorf("Error reading dockerignore file at %v: %v", path, err)
	}
	return excludes, nil
}
func readDockerignorePatterns ( reader io . Reader , path string ) ( [ ] string , error ) { if reader == nil { return nil , nil } scanner := bufio . NewScanner ( reader ) var excludes [ ] string currentLine := 0 utf8bom := [ ] byte { 0xEF , 0xBB , 0xBF } for scanner . Scan ( ) { scannedBytes := scanner . Bytes ( ) if currentLine == 0 { scannedBytes = bytes . TrimPrefix ( scannedBytes , utf8bom ) } pattern := string ( scannedBytes ) currentLine ++ if strings . HasPrefix ( pattern , " " ) { continue } pattern = strings . TrimSpace ( pattern ) if pattern == " " { continue } invert := pattern [ 0 ] == '!' if invert { pattern = strings . TrimSpace ( pattern [ 1 : ] ) } if len ( pattern ) > 0 { pattern = filepath . Clean ( pattern ) pattern = filepath . ToSlash ( pattern ) if len ( pattern ) > 1 && pattern [ 0 ] == '/' { pattern = pattern [ 1 : ] } } if invert { pattern = " " + pattern } excludes = append ( excludes , pattern ) } if err := scanner . Err ( ) ; err != nil { return nil , fmt . Errorf ( " " , path , err ) } return excludes , nil }
ReadAll reads a .dockerignore file and returns the list of file patterns
to ignore.
ReadAll reads a . dockerignore file and returns the list of file patterns to ignore .
// We trim UTF8 BOM // Lines starting with # (comments) are ignored before processing // normalize absolute paths to paths relative to the context // (taking care of '!' prefix)