Пример #1
0
Файл: file.go Проект: dvln/util
// CleanPatterns takes a slice of patterns returns a new
// slice of patterns cleaned with filepath.Clean, stripped
// of any empty patterns and lets the caller know whether the
// slice contains any exception patterns (prefixed with !).
func CleanPatterns(patterns []string) ([]string, [][]string, bool, error) {
	// Loop over exclusion patterns and:
	// 1. Clean them up.
	// 2. Indicate whether we are dealing with any exception rules.
	// 3. Error if we see a single exclusion marker on it's own (!).
	cleanedPatterns := []string{}
	patternDirs := [][]string{}
	exceptions := false
	for _, pattern := range patterns {
		// Eliminate leading and trailing whitespace.
		pattern = strings.TrimSpace(pattern)
		if empty(pattern) {
			continue
		}
		if exclusion(pattern) {
			if len(pattern) == 1 {
				return nil, nil, false, out.NewErr("Illegal exclusion pattern: !", 4009)
			}
			exceptions = true
		}
		pattern = filepath.Clean(pattern)
		cleanedPatterns = append(cleanedPatterns, pattern)
		if exclusion(pattern) {
			pattern = pattern[1:]
		}
		patternDirs = append(patternDirs, strings.Split(pattern, "/"))
	}

	return cleanedPatterns, patternDirs, exceptions, nil
}
Пример #2
0
Файл: dir.go Проект: dvln/util
// Exists checks if given dir exists, if you want to check for a *file*
// use the file.Exists() routine or if you want to check for both file and
// directory use the path.Exists() routine.
func Exists(dir string) (bool, error) {
	exists, err := path.Exists(dir)
	if err != nil {
		// error already wrapped by path.Exists()
		return exists, err
	}
	if exists {
		fileinfo, err := os.Stat(dir)
		if err != nil {
			return false, out.WrapErr(err, "Failed to stat directory, unable to verify existence", 4011)
		}
		if !fileinfo.IsDir() {
			exists = false
			err = out.NewErr("Item is not a directory hence directory existence check failed", 4012)
		}
	}
	return exists, err
}
Пример #3
0
Файл: file.go Проект: dvln/util
// Exists checks if given file exists, if you want to check for a directory
// use the dir.Exists() routine or if you want to check for both file and
// directory use the path.Exists() routine.
func Exists(file string) (bool, error) {
	exists, err := path.Exists(file)
	if err != nil {
		// error already wrapped by path.Exists()
		return exists, err
	}
	if exists {
		var fileinfo os.FileInfo
		fileinfo, err = os.Stat(file)
		if err != nil {
			return false, out.WrapErr(err, "Failed to stat file, unable to verify existence", 4013)
		}
		if fileinfo.IsDir() {
			exists = false
			err = out.NewErr("Item is a directory hence the file existence check failed", 4014)
		}
	}
	return exists, err
}