func testFindAllIndex(t *testing.T, tc *testCase, x *Index, rx *regexp.Regexp, n int) {
	res := x.FindAllIndex(rx, n)
	exp := rx.FindAllStringIndex(tc.source, n)

	// check that the lengths match
	if len(res) != len(exp) {
		t.Errorf("test %q, FindAllIndex %q (n = %d): expected %d results; got %d", tc.name, rx, n, len(exp), len(res))
	}

	// if n >= 0 the number of results is limited --- unless n >= all results,
	// we may obtain different positions from the Index and from regexp (because
	// Index may not find the results in the same order as regexp) => in general
	// we cannot simply check that the res and exp lists are equal

	// check that each result is in fact a correct match and the result is sorted
	for i, r := range res {
		if r[0] < 0 || r[0] > r[1] || len(tc.source) < r[1] {
			t.Errorf("test %q, FindAllIndex %q, result %d (n == %d): illegal match [%d, %d]", tc.name, rx, i, n, r[0], r[1])
		} else if !rx.MatchString(tc.source[r[0]:r[1]]) {
			t.Errorf("test %q, FindAllIndex %q, result %d (n = %d): [%d, %d] not a match", tc.name, rx, i, n, r[0], r[1])
		}
	}

	if n < 0 {
		// all results computed - sorted res and exp must be equal
		for i, r := range res {
			e := exp[i]
			if r[0] != e[0] || r[1] != e[1] {
				t.Errorf("test %q, FindAllIndex %q, result %d: expected match [%d, %d]; got [%d, %d]",
					tc.name, rx, i, e[0], e[1], r[0], r[1])
			}
		}
	}
}
Exemple #2
0
func removeTimestampAndRequestIdFromLogLine(line, requestId string, requestIdRegexp *regexp.Regexp) (string, bool) {
	sep := "\t"
	parts := strings.Split(line, sep)

	// assume timestamp is before request_id
	for i, p := range parts {
		if p == requestId {
			hasTimeStamp := i > 0 && timestampRegexp.MatchString(parts[i-1])
			if hasTimeStamp {
				parts = append(parts[:i-1], parts[i+1:]...)
			} else {
				parts = append(parts[:i], parts[i+1:]...)
			}
			return strings.Join(parts, sep), true
		}
	}

	//remove a log line from another request_id
	for _, p := range parts {
		if requestIdRegexp.MatchString(p) {
			return "", false
		}
	}
	return line, true
}
Exemple #3
0
// list is a subcommand to list up snippets.
// It just finds snippet files in the snippet directory and listed them.
func list(c *cli.Context) {
	var pattern *regexp.Regexp
	var err error
	query := c.Args().First()
	if len(query) > 0 {
		pattern, err = regexp.Compile(fmt.Sprintf(".*%s.*", query))
		if err != nil {
			log.Fatal(err)
		}
	}

	err = filepath.Walk(
		conf.SnippetDirectory,
		func(path string, info os.FileInfo, err error) error {
			if info.IsDir() {
				return nil
			}
			rel, err := filepath.Rel(conf.SnippetDirectory, path)

			if pattern != nil {
				if pattern.MatchString(rel) {
					fmt.Println(rel)
				}
				return nil
			}

			fmt.Println(rel)
			return nil
		},
	)

	if err != nil {
		log.Fatal(err)
	}
}
Exemple #4
0
func checkAuth(client <-chan string, result chan<- bool) {
	var (
		attempt int
		data    string

		pass *regexp.Regexp
	)

	pass = regexp.MustCompile(`^(?i)PASS(?-i) ` + opt.Password + `\r?\n?$`)

	attempt = 0
	for data = range client {
		if attempt > AuthAttempts {
			log.Print("Authentication bad, tearing down.")
			result <- false
			return
		}

		if pass.MatchString(data) {
			log.Print("Authentication good, open sesame.")
			result <- true
			return
		}

		attempt++
	}
}
func parseNetDevStats(r io.Reader, ignore *regexp.Regexp) (map[string]map[string]string, error) {
	scanner := bufio.NewScanner(r)
	scanner.Scan() // skip first header
	scanner.Scan()
	parts := strings.Split(string(scanner.Text()), "|")
	if len(parts) != 3 { // interface + receive + transmit
		return nil, fmt.Errorf("invalid header line in net/dev: %s",
			scanner.Text())
	}

	header := strings.Fields(parts[1])
	netDev := map[string]map[string]string{}
	for scanner.Scan() {
		line := strings.TrimLeft(string(scanner.Text()), " ")
		parts := procNetDevFieldSep.Split(line, -1)
		if len(parts) != 2*len(header)+1 {
			return nil, fmt.Errorf("invalid line in net/dev: %s", scanner.Text())
		}

		dev := parts[0][:len(parts[0])]
		if ignore.MatchString(dev) {
			log.Debugf("Ignoring device: %s", dev)
			continue
		}
		netDev[dev] = map[string]string{}
		for i, v := range header {
			netDev[dev]["receive_"+v] = parts[i+1]
			netDev[dev]["transmit_"+v] = parts[i+1+len(header)]
		}
	}
	return netDev, scanner.Err()
}
Exemple #6
0
// match regexp with string, and return a named group map
// Example:
//   regexp: "(?P<name>[A-Za-z]+)-(?P<age>\\d+)"
//   string: "CGC-30"
//   return: map[string][]string{ "name":["CGC"], "age":["30"] }
func NamedUrlValuesRegexpGroup(str string, reg *regexp.Regexp) (ng url.Values, matched bool) {
	rst := reg.FindStringSubmatch(str)
	if len(rst) < 1 {
		return
	}
	//for i,s :=range rst{
	//	fmt.Printf("%d => %s\n",i,s)
	//}
	ng = url.Values{}
	lenRst := len(rst)
	sn := reg.SubexpNames()
	for k, v := range sn {
		// SubexpNames contain the none named group,
		// so must filter v == ""
		//fmt.Printf("%s => %s\n",k,v)
		if k == 0 || v == "" {
			continue
		}
		if k+1 > lenRst {
			break
		}
		ng.Add(v, rst[k])
	}
	matched = true
	return
}
Exemple #7
0
func GetDeps(data []byte, dependRegex *regexp.Regexp) []string {
	var deps = []string{}
	for _, m := range dependRegex.FindAllStringSubmatch(string(data), -1) {
		deps = append(deps, m[regexFilepathGroupIndex])
	}
	return deps
}
Exemple #8
0
func TestMatchResourceAttr(name, key string, r *regexp.Regexp) TestCheckFunc {
	return func(s *terraform.State) error {
		ms := s.RootModule()
		rs, ok := ms.Resources[name]
		if !ok {
			return fmt.Errorf("Not found: %s", name)
		}

		is := rs.Primary
		if is == nil {
			return fmt.Errorf("No primary instance: %s", name)
		}

		if !r.MatchString(is.Attributes[key]) {
			return fmt.Errorf(
				"%s: Attribute '%s' didn't match %q, got %#v",
				name,
				key,
				r.String(),
				is.Attributes[key])
		}

		return nil
	}
}
func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]string, error) {
	netDev := map[string]map[string]string{}

	var ifap, ifa *C.struct_ifaddrs
	if C.getifaddrs(&ifap) == -1 {
		return nil, errors.New("getifaddrs() failed")
	}
	defer C.freeifaddrs(ifap)

	for ifa = ifap; ifa != nil; ifa = ifa.ifa_next {
		if ifa.ifa_addr.sa_family == C.AF_LINK {
			dev := C.GoString(ifa.ifa_name)
			if ignore.MatchString(dev) {
				log.Debugf("Ignoring device: %s", dev)
				continue
			}

			devStats := map[string]string{}
			data := (*C.struct_if_data)(ifa.ifa_data)

			devStats["receive_packets"] = strconv.Itoa(int(data.ifi_ipackets))
			devStats["transmit_packets"] = strconv.Itoa(int(data.ifi_opackets))
			devStats["receive_errs"] = strconv.Itoa(int(data.ifi_ierrors))
			devStats["transmit_errs"] = strconv.Itoa(int(data.ifi_oerrors))
			devStats["receive_bytes"] = strconv.Itoa(int(data.ifi_ibytes))
			devStats["transmit_bytes"] = strconv.Itoa(int(data.ifi_obytes))
			devStats["receive_multicast"] = strconv.Itoa(int(data.ifi_imcasts))
			devStats["transmit_multicast"] = strconv.Itoa(int(data.ifi_omcasts))
			devStats["receive_drop"] = strconv.Itoa(int(data.ifi_iqdrops))
			netDev[dev] = devStats
		}
	}

	return netDev, nil
}
Exemple #10
0
func ReaderWaitFor(r io.Reader, re *regexp.Regexp, duration time.Duration) ([]byte, bool, error) {
	res := make(chan *readWaitResult, 1)
	quit := make(chan bool, 1)
	go func() {
		out := &bytes.Buffer{}
		var err error
		found := false
		var n int
		buf := make([]byte, 1024)
		for err == nil && !found {
			select {
			case <-quit:
				break
			default:
				n, err = r.Read(buf)
				if n > 0 {
					out.Write(buf[0:n])
				}
				found = re.Match(out.Bytes())
			}
		}
		res <- &readWaitResult{out.Bytes(), found, err}
	}()
	select {
	case result := <-res:
		return result.byts, result.found, result.err
	case <-time.After(duration):
		quit <- true
		return nil, false, ErrTimeout
	}
}
Exemple #11
0
func checkNewChapter(re *regexp.Regexp, l []byte) (depth int, title string) {
	if m := re.FindSubmatch(l); m != nil && m[1][0] == m[3][0] {
		depth = int(m[1][0] - '0')
		title = string(m[2])
	}
	return
}
Exemple #12
0
func processColorTemplates(colorTemplateRegexp *regexp.Regexp, buf []byte) []byte {
	// We really want ReplaceAllSubmatchFunc, i.e.: https://github.com/golang/go/issues/5690
	// Instead we call FindSubmatch on each match, which means that backtracking may not be
	// used in custom Regexps (matches must also match on themselves without context).
	colorTemplateReplacer := func(token []byte) []byte {
		tmp2 := []byte{}
		groups := colorTemplateRegexp.FindSubmatch(token)
		var ansiActive ActiveAnsiCodes
		for _, codeBytes := range bytes.Split(groups[1], bytesComma) {
			colorCode, ok := ansiColorCodes[string(codeBytes)]
			if !ok {
				// Don't modify the text if we don't recognize any of the codes
				return groups[0]
			}
			for _, code := range colorCode.GetAnsiCodes() {
				ansiActive.add(code)
				tmp2 = append(tmp2, ansiEscapeBytes(code)...)
			}
		}
		if len(groups[2]) > 0 {
			tmp2 = append(tmp2, groups[3]...)
			tmp2 = append(tmp2, ansiActive.getResetBytes()...)
		}
		return tmp2
	}
	return colorTemplateRegexp.ReplaceAllFunc(buf, colorTemplateReplacer)
}
Exemple #13
0
func (r *SSHConfigReader) readFile(c SSHConfig, re *regexp.Regexp, f string) error {
	file, err := os.Open(f)
	if err != nil {
		return err
	}
	defer file.Close()

	hosts := []string{"*"}
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()
		match := re.FindStringSubmatch(line)
		if match == nil {
			continue
		}

		names := strings.Fields(match[2])
		if strings.EqualFold(match[1], "host") {
			hosts = names
		} else {
			for _, host := range hosts {
				for _, name := range names {
					c[host] = name
				}
			}
		}
	}

	return scanner.Err()
}
Exemple #14
0
// genericSplit provides a generic version of Split and SplitAfter.
// Set the includeSep bool to true to have it include the separtor.
func genericSplit(re *regexp.Regexp, s string, numFields int, includeSep bool) []string {
	if numFields == 0 {
		return make([]string, 0)
	}

	// Using regexp, including the separator is really easy. Instead of
	// including up to the start of the separator we include to the end.
	// The start of the separator is stored in index 0.
	// The end of the separator is stored in index 1.
	var includeTo int
	if includeSep {
		includeTo = 1
	} else {
		includeTo = 0
	}

	count := re.FindAllStringIndex(s, numFields-1)
	n := len(count) + 1
	stor := make([]string, n)

	if n == 1 {
		stor[0] = s
		return stor
	}

	stor[0] = s[:count[0][includeTo]]

	for i := 1; i < n-1; i++ {
		stor[i] = s[count[i-1][1]:count[i][includeTo]]
	}

	stor[n-1] = s[count[n-2][1]:]

	return stor
}
Exemple #15
0
// strip text between given markers
func stripLiteral(wikitext []byte, start *regexp.Regexp, end *regexp.Regexp) (out []byte) {
	var loc []int

	out = make([]byte, 0, len(wikitext))

top:
	loc = start.FindIndex(wikitext)
	if loc != nil { // match?
		goto strip
	}
	out = append(out, wikitext...) // add what's left
	return
strip:
	out = append(out, wikitext[:loc[0]]...)
	wikitext = wikitext[loc[1]:]
	loc = end.FindIndex(wikitext)
	if loc != nil { // match?
		goto endstrip
	}
	return // assume end at EOF if no match
endstrip:
	wikitext = wikitext[loc[1]:]
	goto top

	panic("unreachable") // please the compiler
}
Exemple #16
0
//broker.handleMessage() gets messages from broker.This() and handles them according
// to the user-provided plugins currently loaded.
func (b *Broker) handleMessage(thingy map[string]interface{}) {
	if b.cbIndex[M] == nil {
		return
	}
	message := new(Event)
	jthingy, _ := json.Marshal(thingy)
	json.Unmarshal(jthingy, message)
	message.Broker = b
	botNamePat := fmt.Sprintf(`^(?:@?%s[:,]?)\s+(?:${1})`, b.Config.Name)
	for _, cbInterface := range b.cbIndex[M] {
		callback := cbInterface.(*MessageCallback)

		Logger.Debug(`Broker:: checking callback: `, callback.ID)
		if callback.SlackChan != `` {
			if callback.SlackChan != message.Channel {
				Logger.Debug(`Broker:: dropping message because chan mismatch: `, callback.ID)
				continue //skip this message because it doesn't match the cb's channel filter
			} else {
				Logger.Debug(`Broker:: channel filter match for: `, callback.ID)
			}
		}
		var r *regexp.Regexp
		if callback.Respond {
			r = regexp.MustCompile(strings.Replace(botNamePat, "${1}", callback.Pattern, 1))
		} else {
			r = regexp.MustCompile(callback.Pattern)
		}
		if r.MatchString(message.Text) {
			match := r.FindAllStringSubmatch(message.Text, -1)[0]
			Logger.Debug(`Broker:: firing callback: `, callback.ID)
			callback.Chan <- PatternMatch{Event: message, Match: match}
		}
	}
}
Exemple #17
0
func getSuffixMatches(req *http.Request, pattern *regexp.Regexp) bool {
	if httputil.IsGet(req) {
		suffix := httputil.PathSuffix(req)
		return pattern.MatchString(suffix)
	}
	return false
}
Exemple #18
0
func read(name string, wg *sync.WaitGroup, reg *regexp.Regexp) {
	defer wg.Done()
	f, err := os.Open(name)
	if err != nil {
		errlog.Println(err)
		return
	}
	defer f.Close()
	var r io.Reader
	ext := filepath.Ext(name)
	switch ext {
	case ".gz":
		r, err = gzip.NewReader(f)
		if err != nil {
			log.Println(err)
			return
		}
	case ".bz":
		r = bzip2.NewReader(f)
	default:
		errlog.Println("Unknown extension:", ext)
		return
	}
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		b := scanner.Bytes()
		if reg.Match(b) {
			log.Printf("%s: %s", name, b)
		}
	}
	if err := scanner.Err(); err != nil {
		errlog.Printf("Error while reading %s: %s", name, err)
	}
}
Exemple #19
0
// PruneFrom removes all nodes beneath the lowest node matching dropRx, not including itself.
//
// Please see the example below to understand this method as well as
// the difference from Prune method.
//
// A sample contains Location of [A,B,C,B,D] where D is the top frame and there's no inline.
//
// PruneFrom(A) returns [A,B,C,B,D] because there's no node beneath A.
// Prune(A, nil) returns [B,C,B,D] by removing A itself.
//
// PruneFrom(B) returns [B,C,B,D] by removing all nodes beneath the first B when scanning from the bottom.
// Prune(B, nil) returns [D] because a matching node is found by scanning from the root.
func (p *Profile) PruneFrom(dropRx *regexp.Regexp) {
	pruneBeneath := make(map[uint64]bool)

	for _, loc := range p.Location {
		for i := 0; i < len(loc.Line); i++ {
			if fn := loc.Line[i].Function; fn != nil && fn.Name != "" {
				// Account for leading '.' on the PPC ELF v1 ABI.
				funcName := strings.TrimPrefix(fn.Name, ".")
				// Account for unsimplified names -- trim starting from the first '('.
				if index := strings.Index(funcName, "("); index > 0 {
					funcName = funcName[:index]
				}
				if dropRx.MatchString(funcName) {
					// Found matching entry to prune.
					pruneBeneath[loc.ID] = true
					loc.Line = loc.Line[i:]
					break
				}
			}
		}
	}

	// Prune locs from each Sample
	for _, sample := range p.Sample {
		// Scan from the bottom leaf to the root to find the prune location.
		for i, loc := range sample.Location {
			if pruneBeneath[loc.ID] {
				sample.Location = sample.Location[i:]
				break
			}
		}
	}
}
Exemple #20
0
// adapted from http://codereview.appspot.com/6846048/
//
// re_split slices s into substrings separated by the expression and returns a slice of
// the substrings between those expression matches.
//
// The slice returned by this method consists of all the substrings of s
// not contained in the slice returned by FindAllString(). When called on an exp ression
// that contains no metacharacters, it is equivalent to strings.SplitN().
// Example:
// s := regexp.MustCompile("a*").re_split("abaabaccadaaae", 5)
// // s: ["", "b", "b", "c", "cadaaae"]
//
// The count determines the number of substrings to return:
// n > 0: at most n substrings; the last substring will be the unsplit remaind er.
// n == 0: the result is nil (zero substrings)
// n < 0: all substrings
func re_split(re *regexp.Regexp, s string, n int) []string {
	if n == 0 {
		return nil
	}
	if len(s) == 0 {
		return []string{""}
	}
	matches := re.FindAllStringIndex(s, n)
	strings := make([]string, 0, len(matches))
	beg := 0
	end := 0
	for _, match := range matches {
		if n > 0 && len(strings) >= n-1 {
			break
		}
		end = match[0]
		if match[1] != 0 {
			strings = append(strings, s[beg:end])
		}
		beg = match[1]
	}
	if end != len(s) {
		strings = append(strings, s[beg:])
	}
	return strings
}
// RequireHeader requires a request header to match a value pattern. If the
// header is missing or does not match then the failureStatus is the response
// (e.g. http.StatusUnauthorized). If pathPattern is nil then any path is
// included. If requiredHeaderValue is nil then any value is accepted so long as
// the header is non-empty.
func RequireHeader(
	pathPattern *regexp.Regexp,
	requiredHeaderName string,
	requiredHeaderValue *regexp.Regexp,
	failureStatus int) goa.Middleware {

	return func(h goa.Handler) goa.Handler {
		return func(ctx *goa.Context) (err error) {
			if pathPattern == nil || pathPattern.MatchString(ctx.Request().URL.Path) {
				matched := false
				header := ctx.Request().Header
				headerValue := header.Get(requiredHeaderName)
				if len(headerValue) > 0 {
					if requiredHeaderValue == nil {
						matched = true
					} else {
						matched = requiredHeaderValue.MatchString(headerValue)
					}
				}
				if matched {
					err = h(ctx)
				} else {
					err = ctx.RespondBytes(failureStatus, []byte(http.StatusText(failureStatus)))
				}
			} else {
				err = h(ctx)
			}
			return
		}
	}
}
Exemple #22
0
func getSuitableAddrs(addrs []*address, v4, v6, linklocal, loopback bool, re *regexp.Regexp, mask *net.IPNet) ([]*address, error) {
	ret := []*address{}
	for _, a := range addrs {
		if a.IsLoopback() && !loopback {
			continue
		}
		if !v6 && a.IsV6() {
			continue
		}
		if !v4 && a.IsV4() {
			continue
		}
		if !linklocal && a.IsLinkLocalUnicast() {
			continue
		}
		if !loopback && a.IsLoopback() {
			continue
		}
		if re != nil {
			if !re.MatchString(a.String()) {
				continue
			}
		}
		if mask != nil {
			if !mask.Contains(a.IP) {
				continue
			}
		}
		ret = append(ret, a)
	}
	if len(ret) == 0 {
		return nil, errors.New("unable to find suitable address")
	}
	return ret, nil
}
Exemple #23
0
// 载入所有的Post
func LoadPosts(root string, exclude string) (posts map[string]Mapper, err error) {
	posts = make(map[string]Mapper)
	err = nil
	var _exclude *regexp.Regexp
	if exclude != "" {
		_exclude, err = regexp.Compile(exclude)
		if err != nil {
			err = errors.New("BAD pages exclude regexp : " + exclude + "\t" + err.Error())
			return
		}
	}
	err = filepath.Walk(root+"posts/", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() || strings.HasPrefix(filepath.Base(path), ".") {
			return nil
		}
		if _exclude != nil && _exclude.Match([]byte(path[len(root+"posts/"):])) {
			return nil
		}
		post, err := LoadPost(root, path)
		if err != nil {
			return err
		}
		posts[post.Id()] = post
		return nil
	})
	return
}
Exemple #24
0
func search(c chan *Trial, target *regexp.Regexp) {
	sequence := uint32(0)
	batch := make([]byte, 1024*4)
	for {
		_, err := rand.Read(batch)
		checkErr(err)
		for i := 0; i < len(batch)-16; i++ {
			trial := &Trial{
				Seed: make([]byte, 16),
			}
			copy(trial.Seed, batch[i:])
			if *ed25519key {
				trial.Key, err = crypto.NewEd25519Key(trial.Seed)
			} else {
				trial.Key, err = crypto.NewECDSAKey(trial.Seed)
			}
			checkErr(err)
			if *ed25519key {
				trial.Id, err = crypto.AccountId(trial.Key, nil)
			} else {
				trial.Id, err = crypto.AccountId(trial.Key, &sequence)
			}
			checkErr(err)
			atomic.AddUint64(&count, 1)
			if target.MatchString(trial.Id.String()) {
				c <- trial
			}
		}
	}
}
Exemple #25
0
func (g unitGenerator) units(name string, types map[string]typeDef) ([]unitDef, error) {
	var r *regexp.Regexp
	var err error

	t, ok := types[g.From]
	if !ok {
		return nil, fmt.Errorf(".from: '%s' not found in type lookup", g.From)
	}
	if g.Pattern != "" {
		r, err = regexp.Compile(g.Pattern)
		if err != nil {
			return nil, fmt.Errorf(".pattern: %s", err.Error())
		}
	}
	units := make([]unitDef, 0, len(t.Units))
	for _, from := range t.Units {
		if r != nil && !r.MatchString(from.Name) {
			continue
		}
		units = append(units, unitDef{
			Name:       from.Name + g.NameSuffix,
			NamePlural: from.NamePlural + g.NameSuffix,
			Value:      fmt.Sprintf("%s(%s)%s", name, from.Name, g.ValueSuffix),
		})
	}
	return units, nil
}
Exemple #26
0
// ServeHTTP parses the path parameters, calls SetPathParameters of
// the corresponding hander, then directs traffic to it.
func (r *RouteHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	glog.Infof("%s: '%s'", req.Method, req.URL.String())
	pathstr := req.URL.Path[len(r.path):]
	var match []string
	var pattern *regexp.Regexp
	var handler Handler
	for e := r.patternHandlers.Front(); e != nil; e = e.Next() {
		h := e.Value.(*patternHandler)
		pattern = h.Regexp
		handler = h.Handler
		match = pattern.FindStringSubmatch(pathstr)
		if match != nil {
			break
		}
	}
	if match == nil {
		glog.Warningf("Cannot find a matching pattern for Path `%s'",
			pathstr)
		http.NotFound(w, req)
		return
	}
	kvpairs := make(map[string]string)
	for i, name := range pattern.SubexpNames() {
		// ignore full match and unnamed submatch
		if i == 0 || name == "" {
			continue
		}
		kvpairs[name] = match[i]
	}
	glog.V(1).Infof("Parsed path parameters: %s", kvpairs)
	handler.ServeHTTP(w, req, kvpairs)
}
Exemple #27
0
func (d *xtremIODriver) getLocalDeviceByID() (map[string]string, error) {
	mapDiskByID := make(map[string]string)
	diskIDPath := "/dev/disk/by-id"
	files, err := ioutil.ReadDir(diskIDPath)
	if err != nil {
		return nil, err
	}

	var match1 *regexp.Regexp
	var match2 string

	if d.r.Config.XtremIODeviceMapper || d.r.Config.XtremIOMultipath {
		match1, _ = regexp.Compile(`^dm-name-\w*$`)
		match2 = `^dm-name-\d+`
	} else {
		match1, _ = regexp.Compile(`^wwn-0x\w*$`)
		match2 = `^wwn-0x`
	}

	for _, f := range files {
		if match1.MatchString(f.Name()) {
			naaName := strings.Replace(f.Name(), match2, "", 1)
			naaName = naaName[len(naaName)-16:]
			devPath, _ := filepath.EvalSymlinks(fmt.Sprintf("%s/%s", diskIDPath, f.Name()))
			mapDiskByID[naaName] = devPath
		}
	}
	return mapDiskByID, nil
}
Exemple #28
0
// FromPattern takes a pattern and if it matches 's' with two exactly two valid
// submatches, returns a BlobRef, else returns nil.
func FromPattern(r *regexp.Regexp, s string) *BlobRef {
	matches := r.FindStringSubmatch(s)
	if len(matches) != 3 {
		return nil
	}
	return blobIfValid(matches[1], matches[2])
}
Exemple #29
0
// RequireHeader requires a request header to match a value pattern. If the
// header is missing or does not match then the failureStatus is the response
// (e.g. http.StatusUnauthorized). If pathPattern is nil then any path is
// included. If requiredHeaderValue is nil then any value is accepted so long as
// the header is non-empty.
func RequireHeader(
	service *goa.Service,
	pathPattern *regexp.Regexp,
	requiredHeaderName string,
	requiredHeaderValue *regexp.Regexp,
	failureStatus int) goa.Middleware {

	return func(h goa.Handler) goa.Handler {
		return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) {
			if pathPattern == nil || pathPattern.MatchString(req.URL.Path) {
				matched := false
				headerValue := req.Header.Get(requiredHeaderName)
				if len(headerValue) > 0 {
					if requiredHeaderValue == nil {
						matched = true
					} else {
						matched = requiredHeaderValue.MatchString(headerValue)
					}
				}
				if matched {
					err = h(ctx, rw, req)
				} else {
					err = service.Send(ctx, failureStatus, http.StatusText(failureStatus))
				}
			} else {
				err = h(ctx, rw, req)
			}
			return
		}
	}
}
func TestIntegration(t *testing.T) {

	flag.Parse()

	bleve.Config.DefaultIndexType = *indexType
	t.Logf("using index type %s", *indexType)

	var err error
	var datasetRegexp *regexp.Regexp
	if *dataset != "" {
		datasetRegexp, err = regexp.Compile(*dataset)
		if err != nil {
			t.Fatal(err)
		}
	}

	fis, err := ioutil.ReadDir("tests")
	if err != nil {
		t.Fatal(err)
	}
	for _, fi := range fis {
		if datasetRegexp != nil {
			if !datasetRegexp.MatchString(fi.Name()) {
				continue
			}
		}
		if fi.IsDir() {
			t.Logf("Running test: %s", fi.Name())
			runTestDir(t, "tests"+string(filepath.Separator)+fi.Name(), fi.Name())
		}
	}
}