Example #1
0
// Match reports whether ref matches the specified pattern.
// See https://godoc.org/path#Match for supported patterns.
func Match(pattern string, ref Reference) (bool, error) {
	matched, err := path.Match(pattern, ref.String())
	if namedRef, isNamed := ref.(Named); isNamed && !matched {
		matched, _ = path.Match(pattern, namedRef.Name())
	}
	return matched, err
}
Example #2
0
// AzureClient -> Glob function
// only supports '*', '?'
// Syntax:
// cntName?/part.*
func (c *AzureClient) Glob(pattern string) (matches []string, err error) {
	afterSplit := strings.Split(pattern, "/")
	cntPattern, blobPattern := afterSplit[0], afterSplit[1]
	if len(afterSplit) != 2 {
		return nil, fmt.Errorf("Glob pattern should follow the Syntax")
	}
	resp, err := c.blobClient.ListContainers(storage.ListContainersParameters{Prefix: ""})
	if err != nil {
		return nil, err
	}
	for _, cnt := range resp.Containers {
		matched, err := path.Match(cntPattern, cnt.Name)
		if err != nil {
			return nil, err
		}
		if !matched {
			continue
		}
		resp, err := c.blobClient.ListBlobs(cnt.Name, storage.ListBlobsParameters{Marker: ""})
		if err != nil {
			return nil, err
		}
		for _, v := range resp.Blobs {
			matched, err := path.Match(blobPattern, v.Name)
			if err != nil {
				return nil, err
			}
			if matched {
				matches = append(matches, cnt.Name+"/"+v.Name)
			}
		}
	}
	return matches, nil
}
Example #3
0
// ValidateOptions ensures that a set of UnitOptions is valid; if not, an error
// is returned detailing the issue encountered.  If there are several problems
// with a set of options, only the first is returned.
func ValidateOptions(opts []*schema.UnitOption) error {
	uf := schema.MapSchemaUnitOptionsToUnitFile(opts)
	// Sanity check using go-systemd's deserializer, which will do things
	// like check for excessive line lengths
	_, err := gsunit.Deserialize(gsunit.Serialize(uf.Options))
	if err != nil {
		return err
	}

	j := &job.Job{
		Unit: *uf,
	}
	conflicts := pkg.NewUnsafeSet(j.Conflicts()...)
	replaces := pkg.NewUnsafeSet(j.Replaces()...)
	peers := pkg.NewUnsafeSet(j.Peers()...)
	for _, peer := range peers.Values() {
		for _, conflict := range conflicts.Values() {
			matched, _ := path.Match(conflict, peer)
			if matched {
				return fmt.Errorf("unresolvable requirements: peer %q matches conflict %q", peer, conflict)
			}
		}
		for _, replace := range replaces.Values() {
			matched, _ := path.Match(replace, peer)
			if matched {
				return fmt.Errorf("unresolvable requirements: peer %q matches replace %q", peer, replace)
			}
		}
	}
	hasPeers := peers.Length() != 0
	hasConflicts := conflicts.Length() != 0
	hasReplaces := replaces.Length() != 0
	_, hasReqTarget := j.RequiredTarget()
	u := &job.Unit{
		Unit: *uf,
	}
	isGlobal := u.IsGlobal()

	switch {
	case hasReqTarget && hasPeers:
		return errors.New("MachineID cannot be used with Peers")
	case hasReqTarget && hasConflicts:
		return errors.New("MachineID cannot be used with Conflicts")
	case hasReqTarget && isGlobal:
		return errors.New("MachineID cannot be used with Global")
	case hasReqTarget && hasReplaces:
		return errors.New("MachineID cannot be used with Replaces")
	case isGlobal && hasPeers:
		return errors.New("Global cannot be used with Peers")
	case isGlobal && hasReplaces:
		return errors.New("Global cannot be used with Replaces")
	case hasConflicts && hasReplaces:
		return errors.New("Conflicts cannot be used with Replaces")
	}

	return nil
}
Example #4
0
func (e *Emitter) getMiddlewares(topic string) []func(*Event) {
	var acc []func(*Event)
	for pattern, v := range e.middlewares {
		if match, _ := path.Match(pattern, topic); match {
			acc = append(acc, v...)
		} else if match, _ := path.Match(topic, pattern); match {
			acc = append(acc, v...)
		}
	}
	return acc
}
Example #5
0
// MatchTarget returns true if t's name or outputs match pattern.
func MatchTarget(pattern string, t *Target) (matched bool, err error) {
	if matched, err = path.Match(pattern, t.Name); matched || err != nil {
		return
	}

	for _, output := range t.Outputs {
		if matched, err = path.Match(pattern, output); matched || err != nil {
			return
		}
	}

	return
}
Example #6
0
func SendLog(url, dir string, upload, keep time.Duration) error {
	err := filepath.Walk(dir, func(filename string, f os.FileInfo, err error) error {
		if f == nil {
			info_err := errors.New("Filepath.Walk() returned no fileinfo")
			glog.Errorf("%v", info_err)
			return nil
		}
		if f.IsDir() {
			return nil
		}
		if b, _ := path.Match("LOG.*.*????-??-??T??:??:??Z", f.Name()); b {
			fields := strings.Split(f.Name(), ".")
			t, _ := time.Parse("MST2006-01-02T15:04:05Z", fields[2])
			time_int := t.Unix()
			// glog.Infoln("Log created at: ", t, "\tTime now: ", time.Now())
			// fmt.Println("Log created at: ", t, "\tTime now: ", time.Now())
			if time.Now().Unix()-int64(upload.Seconds()) > time_int {
				post_err := PostFile(filename, url)
				if post_err != nil {
					// glog.Errorf("Upload file failed, %v", post_err)
				} else {
					os.Rename(filename, path.Dir(filename)+"/OLD."+f.Name())
					glog.Infof("%s upload succeed \n", filename)
					fmt.Printf("%s upload succeed \n", filename)
				}
			}
			return nil
		}
		if b, _ := path.Match("OLD.LOG.*.*????-??-??T??:??:??Z", f.Name()); b {
			fields := strings.Split(f.Name(), ".")
			t, _ := time.Parse("MST2006-01-02T15:04:05Z", fields[3])
			time_int := t.Unix()
			if time.Now().Unix()-int64(keep.Seconds()) > time_int {
				err := os.Remove(filename)
				if err == nil {
					glog.Infof("%s removed \n", filename)
					fmt.Printf("%s removed \n", filename)
				}
			}
			return nil
		}
		return nil
	})
	if err != nil {
		return err
	}
	return nil
}
Example #7
0
func watcherLoop(watcher *fsnotify.Watcher) {
	var cmd *exec.Cmd
	for {
		select {
		case event := <-watcher.Events:
			if event.Op&fsnotify.Write == fsnotify.Write {
				for i := range tasks {
					p := "./" + tasks[i].Pattern
					if m, err := path.Match(p, event.Name); m && (err == nil) {
						if cmd != nil {
							cmd.Process.Kill()
						}
						name := tasks[i].Command[0]
						args := tasks[i].Command[1:]
						log.Print("> ", tasks[i].Name)
						cmd = exec.Command(name, args...)
						cmd.Stdout = os.Stdout
						cmd.Stderr = os.Stderr
						cmd.Start()
					}
				}
			}
		case err := <-watcher.Errors:
			log.Println("error:", err)
		}
	}
}
Example #8
0
File: zip.go Project: jameinel/core
// Find returns the cleaned path of every file in the supplied zip reader whose
// base name matches the supplied pattern, which is interpreted as in path.Match.
func Find(reader *zip.Reader, pattern string) ([]string, error) {
	// path.Match will only return an error if the pattern is not
	// valid (*and* the supplied name is not empty, hence "check").
	if _, err := path.Match(pattern, "check"); err != nil {
		return nil, err
	}
	var matches []string
	for _, zipFile := range reader.File {
		cleanPath := path.Clean(zipFile.Name)
		baseName := path.Base(cleanPath)
		if match, _ := path.Match(pattern, baseName); match {
			matches = append(matches, cleanPath)
		}
	}
	return matches, nil
}
Example #9
0
func (r *URIPathResolver) subtreeMatch(c cookoo.Context, pathName, pattern string) bool {

	if pattern == "**" {
		return true
	}

	// Find out how many slashes we have.
	countSlash := strings.Count(pattern, "/")

	// '**' matches anything.
	if countSlash == 0 {
		c.Logf("warn", "Illegal pattern: %s", pattern)
		return false
	}

	// Add 2 for verb plus trailer.
	parts := strings.SplitN(pathName, "/", countSlash+1)
	prefix := strings.Join(parts[0:countSlash], "/")

	subpattern := strings.Replace(pattern, "/**", "", -1)
	if ok, err := path.Match(subpattern, prefix); ok && err == nil {
		return true
	} else if err != nil {
		c.Logf("warn", "Parsing path `%s` gave error: %s", err)
	}
	return false
}
Example #10
0
func main() {
	var cDir, _ = os.Getwd()
	cDir += "/"

	var _, filePattern = path.Split(cDir)

	fileInfos, err := ioutil.ReadDir(cDir)
	if err != nil {
		log.Fatal(err)
	}

	sort.Sort(byName(fileInfos))
	for _, fileInfo := range fileInfos {
		var findName = (fileInfo).Name()
		var findTime = (fileInfo).ModTime().Unix()

		var matched = true
		if filePattern != "" {
			matched, _ = path.Match(filePattern, findName)
		}
		if matched == true {
			fmt.Printf("%s\n", findName)
			fmt.Println(findTime)
		}
	}
}
Example #11
0
// getFiles returns a sorted list of files in the directory
// in which fname exists starting with fname (i.e. fname*).
func getFiles(fname string) ([]string, error) {
	dn := path.Dir(fname)
	bn := path.Base(fname)
	files, err := ioutil.ReadDir(dn)
	if err != nil {
		return nil, err
	}
	match := make([]os.FileInfo, len(files))
	count := 0
	for _, f := range files {
		if m, _ := path.Match(bn+".*", f.Name()); m {
			match[count] = f
			count++
		}
	}
	if count > 0 {
		res := make([]string, count)
		for i, f := range match[:count] {
			res[i] = fmt.Sprintf("%s/%s", dn, f.Name())
		}
		sort.Sort(handysort.Strings(res))
		return res, nil
	}
	return nil, nil
}
Example #12
0
func Drop(item interface{}, items []interface{}) ([]interface{}, error) {
	var out []interface{}
	pattern, isstr := item.(string)
	if isstr {
		for i := range items {
			str, ok := items[i].(string)
			if !ok {
				return nil, fmt.Errorf("all elements must be a string to drop a string")
			}
			match, err := path.Match(pattern, str)
			if err != nil {
				return nil, fmt.Errorf("bad pattern: %s", pattern)
			}
			if !match {
				out = append(out, items[i])
			}
		}
		return out, nil
	}
	for i := range items {
		if item != items[i] {
			out = append(out, items[i])
		}
	}
	return out, nil
}
Example #13
0
func parseFilter(envname string, pad func(string) string) map[string]bool {
	filter := make(map[string]bool)

	env := os.Getenv(envname)
	if env == "" {
		return filter
	}

	for _, fn := range strings.Split(env, ",") {
		t := pad(strings.TrimSpace(fn))
		val := true
		if t[0] == '-' {
			val = false
			t = t[1:]
		} else if t[0] == '+' {
			val = true
			t = t[1:]
		}

		// test pattern
		_, err := path.Match(t, "")
		if err != nil {
			fmt.Fprintf(os.Stderr, "error: invalid pattern %q: %v\n", t, err)
			os.Exit(5)
		}

		filter[t] = val
	}

	return filter
}
Example #14
0
File: sysinit.go Project: rowhit/os
func findImages(cfg *config.CloudConfig) ([]string, error) {
	log.Debugf("Looking for images at %s", config.IMAGES_PATH)

	result := []string{}

	dir, err := os.Open(config.IMAGES_PATH)
	if os.IsNotExist(err) {
		log.Debugf("Not loading images, %s does not exist")
		return result, nil
	}
	if err != nil {
		return nil, err
	}

	defer dir.Close()

	files, err := dir.Readdirnames(0)
	if err != nil {
		return nil, err
	}

	for _, fileName := range files {
		if ok, _ := path.Match(config.IMAGES_PATTERN, fileName); ok {
			log.Debugf("Found %s", fileName)
			result = append(result, fileName)
		}
	}

	return result, nil
}
Example #15
0
func globMatches(pattern, target string) bool {
	matched, err := path.Match(pattern, target)
	if err != nil {
		log.V(2).Infof("Received error while matching pattern '%s': %v", pattern, err)
	}
	return matched
}
Example #16
0
func (self *attachedFilter) Match(event string) (ok bool) {
	var err error
	if ok, err = path.Match(self.Pattern, event); err != nil {
		panic(err.Error())
	}
	return
}
Example #17
0
func SendLogNow(url, dir string) error {
	err := filepath.Walk(dir, func(filename string, f os.FileInfo, err error) error {
		if f == nil {
			info_err := errors.New("Filepath.Walk() returned no fileinfo")
			glog.Errorf("%v", info_err)
			return nil
		}
		if f.IsDir() {
			return nil
		}
		if b, _ := path.Match("LOG.*.*????-??-??T??:??:??Z", f.Name()); b {
			post_err := PostFile(filename, url)
			if post_err != nil {
				glog.Errorf("Upload file failed, %v", post_err)
			} else {
				glog.Infof("%s upload succeed \n", filename)
				fmt.Printf("%s upload succeed \n", filename)
			}
			return nil
		}
		return nil
	})
	if err != nil {
		return err
	}
	return nil
}
Example #18
0
func MatchPath(path string, pattern string) bool {
	match, err := p.Match(pattern, path)
	if err != nil || match != true {
		return false
	}
	return true
}
Example #19
0
func (_ FileSystemAvatar) GetAvatarURL(u ChatUser) (string, error) {
	// if userid, ok := c.userData["userid"]; ok {
	// 	if useridStr, ok := userid.(string); ok {
	// 		if files, err := ioutil.ReadDir("avatars"); err == nil {
	// 			for _, file := range files {
	// 				if file.IsDir() {
	// 					continue
	// 				}

	// 				if match, _ := path.Match(useridStr+"*", file.Name()); match {
	// 					return "/avatars/" + file.Name(), nil
	// 				}
	// 			}
	// 		}
	// 	}
	// }
	// return "", ErrNoAvatarUrl

	if files, err := ioutil.ReadDir("avatars"); err == nil {
		for _, file := range files {
			if file.IsDir() {
				continue
			}
			if match, _ := path.Match(u.UniqueId()+"*", file.Name()); match {
				return "/avatars/" + file.Name(), nil
			}
		}
	}
	return "", ErrNoAvatarUrl
}
func (this *LogClientController) uploadlocal(interval time.Duration) ([]byte, error) {
	var logs []byte
	err := filepath.Walk(this.dir, func(filename string, f os.FileInfo, err error) error {
		if f == nil {
			glog.Errorf("No log file found")
			return nil
		}
		if f.IsDir() {
			return nil
		}
		if b, _ := path.Match("LOG.*.*????-??-??T??:??:??Z", f.Name()); b {
			fields := strings.Split(f.Name(), ".")
			t, _ := time.Parse("MST2006-01-02T15:04:05Z", fields[2])
			time_int := t.Unix()
			if time.Now().Unix()-int64(interval.Seconds()) < time_int {
				// fmt.Println(filename)
				log, err := ioutil.ReadFile(filename)
				if err != nil {
					glog.Errorf("%v", err)
					return nil
				}
				logs = append(logs, log...)
				// fmt.Println(string(logs))
				// logs = tmp
			}
		}
		return nil
	})
	return logs, err
}
Example #21
0
func (d *templateKV) List(pattern string, offset, limit int) ([]Template, error) {
	// Templates are indexed via their ID only.
	// While templates are sorted in the data section by their ID anyway
	// this allows us to do offset/limits and filtering without having to read in all template data.

	// List all template ids sorted by ID
	ids, err := d.store.List(templateIndexesPrefix + idIndex)
	if err != nil {
		return nil, err
	}

	var match func([]byte) bool
	if pattern != "" {
		match = func(value []byte) bool {
			id := string(value)
			matched, _ := path.Match(pattern, id)
			return matched
		}
	} else {
		match = func([]byte) bool { return true }
	}
	matches := storage.DoListFunc(ids, match, offset, limit)

	templates := make([]Template, len(matches))
	for i, id := range matches {
		data, err := d.store.Get(d.templateDataKey(string(id)))
		if err != nil {
			return nil, err
		}
		t, err := d.decodeTemplate(data.Value)
		templates[i] = t
	}
	return templates, nil
}
Example #22
0
func Test() (failed, passed []string) {
	// run tests!
	dir, err := os.Open(filepath.Join(*root, "hack", "e2e-suite"))
	if err != nil {
		log.Fatal("Couldn't open e2e-suite dir")
	}
	defer dir.Close()
	names, err := dir.Readdirnames(0)
	if err != nil {
		log.Fatal("Couldn't read names in e2e-suite dir")
	}

	for i := range names {
		name := names[i]
		if name == "." || name == ".." {
			continue
		}
		if match, err := path.Match(*tests, name); !match && err == nil {
			continue
		}
		absName := filepath.Join(*root, "hack", "e2e-suite", name)
		log.Printf("%v matches %v. Starting test.", name, *tests)
		if runBash(name, absName) {
			log.Printf("%v passed", name)
			passed = append(passed, name)
		} else {
			log.Printf("%v failed", name)
			failed = append(failed, name)
		}
	}

	return
}
Example #23
0
func (r *relayHandler) lookup(dst string, conn *gosocks.SocksConn) chain.SocksChain {
	chain := &chain.SocksSocksChain{
		SocksDialer: &gosocks.SocksDialer{
			Timeout: conn.Timeout,
			Auth:    &gosocks.AnonymousClientAuthenticator{},
		},
		SocksAddr: r.nextHop,
	}

	if r.tunnellingAll {
		return chain
	}

	labels := strings.Split(dst, ".")
	for i := 0; i < len(labels); i++ {
		_, ok := r.embeddedTunnellingDomains[strings.Join(labels[i:], ".")]
		if ok {
			return chain
		}
	}

	for _, v := range r.customTunnellingDomains {
		matched, _ := path.Match(v, dst)
		if matched {
			return chain
		}
	}

	return nil
}
Example #24
0
func (c *Config) getHostByName(name string, safe bool) (*Host, error) {
	if host, ok := c.Hosts[name]; ok {
		var computedHost Host = host
		computedHost.ApplyDefaults(&c.Defaults)
		computedHost.Name = name
		return &computedHost, nil
	}

	for pattern, host := range c.Hosts {
		matched, err := path.Match(pattern, name)
		if err != nil {
			return nil, err
		}
		if matched {
			var computedHost Host = host
			computedHost.ApplyDefaults(&c.Defaults)
			computedHost.Name = name
			return &computedHost, nil
		}
	}

	if safe {
		host := &Host{
			Host: name,
			Name: name,
		}
		host.ApplyDefaults(&c.Defaults)
		return host, nil
	}

	return nil, fmt.Errorf("no such host: %s", name)
}
Example #25
0
//Matches states whether the addition matches the given pattern.
//If the pattern ends in a path separator, then all files inside a directory with that name are matched. However, files with that name itself will not be matched.
//If a pattern contains the path separator in any other location, the match works according to the pattern logic of the default golang glob mechanism
//If there is no path separator anywhere in the pattern, the pattern is matched against the base name of the file. Thus, the pattern will match files with that name anywhere in the repository.
func (a Addition) Matches(pattern string) bool {
	var result bool
	if pattern[len(pattern)-1] == os.PathSeparator {
		result = strings.HasPrefix(string(a.Path), pattern)
	} else if strings.ContainsRune(pattern, os.PathSeparator) {
		result, _ = path.Match(pattern, string(a.Path))
	} else {
		result, _ = path.Match(pattern, string(a.Name))
	}
	log.WithFields(log.Fields{
		"pattern":  pattern,
		"filePath": a.Path,
		"match":    result,
	}).Debug("Checking addition for match.")
	return result
}
Example #26
0
func findPart(part []byte, currentLevel []*PatternNode) ([]*PatternNode, int) {
	nextLevel := make([]*PatternNode, 0, 64)
	hash := xxhash.Checksum32(part)
	for _, node := range currentLevel {
		for _, child := range node.Children {
			match := false

			if child.Hash == asteriskHash || child.Hash == hash {
				match = true
			} else if len(child.InnerParts) > 0 {
				for _, innerPart := range child.InnerParts {
					innerMatch, _ := path.Match(innerPart, string(part))
					if innerMatch {
						match = true
						break
					}
				}
			}

			if match {
				nextLevel = append(nextLevel, child)
			}
		}
	}
	return nextLevel, len(nextLevel)
}
Example #27
0
// glob searches for files matching pattern in the directory dir
// and appends them to matches. If the directory cannot be
// opened, it returns the existing matches. New matches are
// added in lexicographical order.
func glob(fs vfs.FileSystem, dir, pattern string, matches []string) (m []string, e error) {
	m = matches
	fi, err := fs.Stat(dir)
	if err != nil {
		return
	}
	if !fi.IsDir() {
		return
	}
	fis, err := fs.ReadDir(dir)
	if err != nil {
		return
	}

	sort.Sort(byName(fis))

	for _, fi := range fis {
		n := fi.Name()
		matched, err := path.Match(path.Clean(pattern), n)
		if err != nil {
			return m, err
		}
		if matched {
			m = append(m, path.Join(dir, n))
		}
	}
	return
}
Example #28
0
func (self Server) GetFiles(targetPath string, files []Memo) []Memo {
	dir := filepath.Join(CurDir(), targetPath)
	isDir, _ := IsDirectory(dir)
	if isDir == true {
		fileInfos, _ := ioutil.ReadDir(dir)
		for _, fileInfo := range fileInfos {
			fileName := (fileInfo).Name()
			newPath := filepath.Join(targetPath, fileName)
			if fileInfo.IsDir() == true {
				files = self.GetFiles(newPath, files)
			} else {
				matched, _ := path.Match("*.md", fileName)
				if matched == true {
					title := self.GetTitle(newPath)
					memo := Memo{Path: newPath, Title: title}
					files = append(files, memo)
				}
			}
		}
		return files
	} else {
		emptyMemo := Memo{Title: "Directory is empty", Path: targetPath}
		files := []Memo{emptyMemo}
		return files
	}
}
Example #29
0
File: toys.go Project: nvcnvn/glog
// match is a wrapper function for path.Math
func match(pattern, name string) bool {
	ok, err := path.Match(pattern, name)
	if err != nil {
		return false
	}
	return ok
}
Example #30
0
func (self Server) ServedFile(urlPath string, w http.ResponseWriter, r *http.Request) bool {
	fullpath := filepath.Join(CurDir(), urlPath)
	isDir, _ := IsDirectory(fullpath)
	if isDir == true {
		http.Error(w, "File not found", 404)
		return true
	} else {
		fileInfo, _ := os.Stat(fullpath)
		if fileInfo == nil {
			targetPath := strings.Replace(urlPath, "/", "", 1)
			bin, err := Asset(targetPath)
			if err == nil {
				fmt.Printf(" [INFO] %s\n", urlPath)
				http.ServeContent(w, r, urlPath, time.Now(), bytes.NewReader(bin))
				return true
			} else {
				http.Error(w, "File not found", 404)
				return true
			}
		}

		fileName := (fileInfo).Name()
		matched, _ := path.Match("*.md", fileName)
		if matched != true {
			fmt.Printf(" [INFO] %s\n", urlPath)
			http.ServeFile(w, r, fullpath)
			return true
		}
	}
	return false
}