Exemplo n.º 1
0
// All returns whatever the path matches all of the patterns.
// pattern errors are ignored.
func All(path string, patterns ...string) bool {
	for _, pattern := range patterns {
		if match, _ := doublestar.Match(pattern, path); !match {
			return false
		}
	}
	return true
}
Exemplo n.º 2
0
func matchAny(path string, patterns []string) (bool, error) {
	for _, pattern := range patterns {
		match, err := doublestar.Match(pattern, filepath.ToSlash(path))
		if err != nil {
			return false, fmt.Errorf("Error matching pattern '%s': %s", pattern, err)
		} else if match {
			return true, nil
		}
	}
	return false, nil
}
Exemplo n.º 3
0
// Determine if a file should be included, based on the given exclude paths.
func shouldInclude(file string, excludePatterns []string, log termlog.Logger) bool {
	for _, pattern := range excludePatterns {
		match, err := doublestar.Match(pattern, file)
		if err != nil {
			log.Warn("Error matching pattern '%s': %s", pattern, err)
		} else if match {
			return false
		}
	}
	return true
}
Exemplo n.º 4
0
// Parse the files transforming them into a byte string and inserting the file
// into a map of files
func (c *Custom) Parse(files *map[string]*file.File, dirs **dir.Dir, config *SharedConfig) error {
	to := *files
	dirList := *dirs

	for _, customFile := range c.Files {
		customFile = utils.FixPath(customFile)
		walkErr := filepath.Walk(customFile, func(fpath string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}

			// only files will be processed
			if info.IsDir() {
				return nil
			}

			var fixedPath string
			if c.Base != "" {
				// remove base and inserts prefix
				fixedPath = strings.Replace(
					utils.FixPath(fpath),
					utils.FixPath(c.Base),
					utils.FixPath(c.Prefix),
					1) // once
			} else {
				fixedPath = utils.FixPath(fpath)
			}

			// check for excluded files
			for _, excludedFile := range c.Exclude {
				m, err := doublestar.Match(c.Prefix+excludedFile, fixedPath)
				if err != nil {
					return err
				}

				if m {
					return nil
				}
			}

			// FIXME
			// prevent including itself (destination file or dir)
			if info.Name() == config.Output {
				return nil
			}
			/*if info.Name() == cfg.Output { ||
			  info.Name() == path.Base(path.Dir(jsonFile)) {
			  return nil
			}*/

			// get file's content
			content, err := ioutil.ReadFile(fpath)
			if err != nil {
				return err
			}

			// loop through replace list
			for _, r := range c.Replace {
				// check if path matches the pattern from property: file
				matched, err := doublestar.Match(c.Prefix+r.File, fixedPath)
				if err != nil {
					return err
				}

				if matched {
					for pattern, word := range r.Replace {
						content = []byte(strings.Replace(string(content), pattern, word, -1))
					}
				}
			}

			// it's way faster to use a buffer as string than use string
			var buf bytes.Buffer
			buf.WriteString(`[]byte("`)
			f := file.NewFile()

			// compress the content
			if config.Compression.Options != nil {
				content, err = config.Compression.Compress(content)
				if err != nil {
					return err
				}
			}

			// it's way faster to loop and slice a string than a byte array
			h := hex.EncodeToString(content)

			// loop through hex string, at each 2 chars
			// it's added into a byte array -> []byte{0x61 ,...}
			for i := 0; i < len(h); i += 2 {
				buf.WriteString(`\x` + h[i:i+2])
			}

			f.Data = buf.String() + `")`
			f.Name = info.Name()
			f.Path = fixedPath

			// insert dir to dirlist so it can be created on b0x's init()
			dirList.Insert(path.Dir(fixedPath))

			// insert file into file list
			to[fixedPath] = f
			return nil
		})

		if walkErr != nil {
			return walkErr
		}
	}

	return nil
}
Exemplo n.º 5
-1
func Good(patterns ...string) error {
	for _, p := range patterns {
		if _, err := doublestar.Match(p, ""); err != nil {
			return err
		}
	}
	return nil
}