コード例 #1
0
ファイル: route.go プロジェクト: rosylilly/ape
func (r *Route) compile() {
	segments := strings.Split(r.Path, "/")
	compiledSegments := make([]string, len(segments))

	for i, segment := range segments {
		switch {
		case namedSegmentRegexp.MatchString(segment):
			segmentName := regexp.QuoteMeta(segment[1:len(segment)])
			constraint := defaultConstraint

			if _, hit := r.Constraints[segmentName]; hit {
				constraint = r.Constraints[segmentName]
			}

			segment = "(?P<" + segmentName + ">" + constraint + ")"
		default:
			segment = regexp.QuoteMeta(segment)
		}
		compiledSegments[i] = segment
	}

	regexpSource := "^" + strings.Join(compiledSegments, "/") + formantConstraint

	if !r.mounted {
		regexpSource += "$"
	}

	r.regexp = regexp.MustCompile(regexpSource)
}
コード例 #2
0
ファイル: connection_test.go プロジェクト: brunoqc/go-sqlmock
func TestExecArgsMismatch(t *testing.T) {
	c := &conn{
		expectations: []expectation{
			&expectedExec{
				queryBasedExpectation: queryBasedExpectation{
					commonExpectation: commonExpectation{
						err: errors.New("WillReturnError"),
					},
					sqlRegex: regexp.MustCompile(regexp.QuoteMeta("query")),
					args:     []driver.Value{456},
				},
			},
		},
	}
	res, err := c.Exec("query", []driver.Value{123})
	if res != nil {
		t.Error("Result should be nil")
	}
	if err == nil {
		t.Error("error should not be nil")
	}
	pattern := regexp.MustCompile(regexp.QuoteMeta("does not match expected"))
	if !pattern.MatchString(err.Error()) {
		t.Errorf("error should match expected error message (actual: %s)", err.Error())
	}
}
コード例 #3
0
ファイル: gb_test.go プロジェクト: hyper-carrot/gb
// check that missing -race support generates error message.
func TestRaceMissing(t *testing.T) {
	if canRace {
		t.Skip("skipping because race detector is available")
	}

	gb := T{T: t}
	defer gb.cleanup()

	gb.tempDir("src/race")
	gb.tempFile("src/race/map_test.go", `package race
import "testing"

func TestRaceMapRW(t *testing.T) {
        m := make(map[int]int)
        ch := make(chan bool, 1)
        go func() {
                _ = m[1]
                ch <- true
        }()
        m[1] = 1
        <-ch
}
`)
	gb.cd(gb.tempdir)
	tmpdir := gb.tempDir("tmp")
	gb.setenv("TMP", tmpdir)
	gb.runFail("test", "-race")
	raceError1 := fmt.Sprintf("FATAL: go installation at %s is missing race support", runtime.GOROOT())
	raceError2 := fmt.Sprintf("FATAL: race detector not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
	gb.grepStderr(regexp.QuoteMeta(raceError1)+"|"+regexp.QuoteMeta(raceError2), "expected missing race support message")
	gb.mustBeEmpty(tmpdir)
}
コード例 #4
0
ファイル: router.go プロジェクト: chillicoder/twister
// compilePattern compiles the pattern to a regexp and array of parameter names.
func compilePattern(pattern string, addSlash bool, sep string) (*regexp.Regexp, []string) {
	var buf bytes.Buffer
	names := make([]string, 8)
	i := 0
	buf.WriteString("^")
	for {
		a := parameterRegexp.FindStringSubmatchIndex(pattern)
		if len(a) == 0 {
			buf.WriteString(regexp.QuoteMeta(pattern))
			break
		} else {
			buf.WriteString(regexp.QuoteMeta(pattern[0:a[0]]))
			name := pattern[a[2]:a[3]]
			if name != "" {
				names[i] = pattern[a[2]:a[3]]
				i += 1
				buf.WriteString("(")
			}
			if a[4] >= 0 {
				buf.WriteString(pattern[a[4]+1 : a[5]])
			} else {
				buf.WriteString("[^" + sep + "]+")
			}
			if name != "" {
				buf.WriteString(")")
			}
			pattern = pattern[a[1]:]
		}
	}
	if addSlash {
		buf.WriteString("?")
	}
	buf.WriteString("$")
	return regexp.MustCompile(buf.String()), names[0:i]
}
コード例 #5
0
ファイル: regexp.go プロジェクト: naoina/kocha-urlrouter
func build(path string, data interface{}) (*route, error) {
	var buf bytes.Buffer
	dups := make(map[string]bool)
	for _, paths := range pathRegexp.FindAllStringSubmatch(path, -1) {
		name := paths[1] + paths[2]
		if name == "" {
			// don't have path parameters.
			buf.WriteString(regexp.QuoteMeta(paths[0]))
			continue
		}
		var pathReStr string
		if pathReStr = paramRegexpStr[name[0]]; pathReStr == "" {
			pathReStr = defaultParamRegexpStr
		} else {
			if dups[name] {
				return nil, fmt.Errorf("path parameter `%v` is duplicated in the key '%v'", name, path)
			}
			dups[name] = true
			name = name[1:] // truncate a meta character.
		}
		buf.WriteString(fmt.Sprintf(`/(?P<%s>%s)`, regexp.QuoteMeta(name), pathReStr))
	}
	reg, err := regexp.Compile(fmt.Sprintf(`^%s$`, buf.String()))
	if err != nil {
		return nil, err
	}
	return &route{regexp: reg, data: data}, nil
}
コード例 #6
0
ファイル: route.go プロジェクト: fragmenta/router
// compileRegexp compiles our route format to a true regexp
// Both name and regexp are required - routes should be well structured and restrictive by default
// Convert the pattern from the form  /pages/{id:[0-9]*}/edit?param=test
// to one suitable for regexp -  /pages/([0-9]*)/edit\?param=test
// We want to match things like this:
// /pages/{id:[0-9]*}/edit
// /pages/{id:[0-9]*}/edit?param=test
func (r *Route) compileRegexp() (err error) {
	// Check if it is well-formed.
	idxs, errBraces := r.findBraces(r.Pattern)
	if errBraces != nil {
		return errBraces
	}

	pattern := bytes.NewBufferString("^")
	end := 0

	// Walk through indexes two at a time
	for i := 0; i < len(idxs); i += 2 {
		// Set all values we are interested in.
		raw := r.Pattern[end:idxs[i]]
		end = idxs[i+1]
		parts := strings.SplitN(r.Pattern[idxs[i]+1:end-1], ":", 2)
		if len(parts) != 2 {
			return fmt.Errorf("Missing name or pattern in %s", raw)
		}

		// Add the Argument name
		r.ParamNames = append(r.ParamNames, parts[0])

		// Add the real regexp
		fmt.Fprintf(pattern, "%s(%s)", regexp.QuoteMeta(raw), parts[1])

	}
	// Add the remaining pattern
	pattern.WriteString(regexp.QuoteMeta(r.Pattern[end:]))
	r.Regexp, err = regexp.Compile(pattern.String())

	return err
}
コード例 #7
0
ファイル: mux.go プロジェクト: mrlauer/pdfmaker
func CreateMux(text string) *Mux {
	// parse the text into a regexp
	re := regexp.MustCompile(`:[A-Z]\w*`)
	matches := re.FindAllStringIndex(text, -1)

	restring := "^"
	pos := 0
	variables := []string{}
	for _, match := range matches {
		start := match[0]
		end := match[1]
		// Everything since the last match
		// Should check for evil characters
		restring += regexp.QuoteMeta(text[pos:start])
		pos = end
		// Turn this into an RE
		// Should the RE be more restrictive?
		restring += `([^/]*)`
		variables = append(variables, text[start+1:end])
	}
	restring += regexp.QuoteMeta(text[pos:])
	restring += "$"
	muxre := regexp.MustCompile(restring)

	return &Mux{text: text, muxRegexp: muxre, variables: variables}
}
コード例 #8
0
ファイル: gitignore.go プロジェクト: rliebling/gitignorer
// Braket expression wildcard. Except for the beginning
// exclamation mark, the whole braket expression can be used
// directly as regex but we have to find where the expression
// ends.
// - "[][!]" matchs ']', '[' and '!'.
// - "[]-]" matchs ']' and '-'.
// - "[!]a-]" matchs any character except ']', 'a' and '-'.
func translateBraketExpression(i *int, glob string) string {
	regex := string(glob[*i])
	*i++
	j := *i

	// Pass brack expression negation.
	if j < len(glob) && glob[j] == '!' {
		j++
	}
	// Pass first closing braket if it is at the beginning of the
	// expression.
	if j < len(glob) && glob[j] == ']' {
		j++
	}
	// Find closing braket. Stop once we reach the end or find it.
	for j < len(glob) && glob[j] != ']' {
		j++
	}

	if j < len(glob) {
		if glob[*i] == '!' {
			regex = regex + "^"
			*i++
		}
		regex = regexp.QuoteMeta(glob[*i:j])
		*i = j
	} else {
		// Failed to find closing braket, treat opening braket as a
		// braket literal instead of as an expression.
		regex = regexp.QuoteMeta(string(glob[*i]))
	}
	return "[" + regex + "]"
}
コード例 #9
0
ファイル: package_test.go プロジェクト: AlexisBruemmer/juju
// TestHelp runs the command with --help as argument and verifies the
// output.
func (s *BaseSpaceSuite) TestHelp(c *gc.C) {
	stderr, stdout, err := s.RunSuperCommand(c, "--help")
	c.Assert(err, jc.ErrorIsNil)
	c.Check(stdout, gc.Equals, "")
	c.Check(stderr, gc.Not(gc.Equals), "")

	// If s.command is set, use it instead of s.superCmd.
	cmdInfo := s.superCmd.Info()
	var expected string
	if s.command != nil {
		// Subcommands embed ModelCommandBase
		cmdInfo = s.command.Info()
		expected = "(?sm).*^Usage: juju space " +
			regexp.QuoteMeta(cmdInfo.Name) +
			`( \[options\])? ` + regexp.QuoteMeta(cmdInfo.Args) + ".+"
	} else {
		expected = "(?sm).*^Usage: juju space" +
			`( \[options\])? ` + regexp.QuoteMeta(cmdInfo.Args) + ".+"
	}
	c.Check(cmdInfo, gc.NotNil)
	c.Check(stderr, gc.Matches, expected)

	expected = "(?sm).*^Summary:\n" + regexp.QuoteMeta(cmdInfo.Purpose) + "$.*"
	c.Check(stderr, gc.Matches, expected)

	expected = "(?sm).*^Details:\n" + regexp.QuoteMeta(cmdInfo.Doc) + "$.*"
	c.Check(stderr, gc.Matches, expected)
}
コード例 #10
0
ファイル: codec_test.go プロジェクト: rogpeppe/juju
func (*suite) TestReadHeaderLogsRequests(c *gc.C) {
	codecLogger := loggo.GetLogger("juju.rpc.jsoncodec")
	defer codecLogger.SetLogLevel(codecLogger.LogLevel())
	codecLogger.SetLogLevel(loggo.TRACE)
	msg := `{"RequestId":1,"Type": "foo","Id": "id","Request":"frob","Params":{"X":"param"}}`
	codec := jsoncodec.New(&testConn{
		readMsgs: []string{msg, msg, msg},
	})
	// Check that logging is off by default
	var h rpc.Header
	err := codec.ReadHeader(&h)
	c.Assert(err, gc.IsNil)
	c.Assert(c.GetTestLog(), gc.Matches, "")

	// Check that we see a log message when we switch logging on.
	codec.SetLogging(true)
	err = codec.ReadHeader(&h)
	c.Assert(err, gc.IsNil)
	c.Assert(c.GetTestLog(), gc.Matches, ".*TRACE juju.rpc.jsoncodec <- "+regexp.QuoteMeta(msg)+`\n`)

	// Check that we can switch it off again
	codec.SetLogging(false)
	err = codec.ReadHeader(&h)
	c.Assert(err, gc.IsNil)
	c.Assert(c.GetTestLog(), gc.Matches, ".*TRACE juju.rpc.jsoncodec <- "+regexp.QuoteMeta(msg)+`\n`)
}
コード例 #11
0
func (*suite) TestWriteMessageLogsRequests(c *C) {
	codec := jsoncodec.New(&testConn{})
	h := rpc.Header{
		RequestId: 1,
		Type:      "foo",
		Id:        "id",
		Request:   "frob",
	}

	// Check that logging is off by default
	err := codec.WriteMessage(&h, value{X: "param"})
	c.Assert(err, IsNil)
	c.Assert(c.GetTestLog(), Matches, "")

	// Check that we see a log message when we switch logging on.
	codec.SetLogging(true)
	err = codec.WriteMessage(&h, value{X: "param"})
	c.Assert(err, IsNil)
	msg := `{"RequestId":1,"Type":"foo","Id":"id","Request":"frob","Params":{"X":"param"}}`
	c.Assert(c.GetTestLog(), Matches, `.*DEBUG juju rpc/jsoncodec: -> `+regexp.QuoteMeta(msg)+`\n`)

	// Check that we can switch it off again
	codec.SetLogging(false)
	err = codec.WriteMessage(&h, value{X: "param"})
	c.Assert(err, IsNil)
	c.Assert(c.GetTestLog(), Matches, `.*DEBUG juju rpc/jsoncodec: -> `+regexp.QuoteMeta(msg)+`\n`)
}
コード例 #12
0
ファイル: state.go プロジェクト: klyachin/juju
func (st *State) checkCanUpgrade(currentVersion, newVersion string) error {
	matchCurrent := "^" + regexp.QuoteMeta(currentVersion) + "-"
	matchNew := "^" + regexp.QuoteMeta(newVersion) + "-"
	// Get all machines and units with a different or empty version.
	sel := bson.D{{"$or", []bson.D{
		{{"tools", bson.D{{"$exists", false}}}},
		{{"$and", []bson.D{
			{{"tools.version", bson.D{{"$not", bson.RegEx{matchCurrent, ""}}}}},
			{{"tools.version", bson.D{{"$not", bson.RegEx{matchNew, ""}}}}},
		}}},
	}}}
	var agentTags []string
	for _, collection := range []*mgo.Collection{st.machines, st.units} {
		var doc struct {
			Id string `bson:"_id"`
		}
		iter := collection.Find(sel).Select(bson.D{{"_id", 1}}).Iter()
		for iter.Next(&doc) {
			switch collection.Name {
			case "machines":
				agentTags = append(agentTags, names.NewMachineTag(doc.Id).String())
			case "units":
				agentTags = append(agentTags, names.NewUnitTag(doc.Id).String())
			}
		}
		if err := iter.Close(); err != nil {
			return err
		}
	}
	if len(agentTags) > 0 {
		return newVersionInconsistentError(version.MustParse(currentVersion), agentTags)
	}
	return nil
}
コード例 #13
0
ファイル: prolix.go プロジェクト: gaal/prolix-go
func importSnippet(subsitutions []string) (ok bool) {
	for _, sub := range subsitutions {
		if len(sub) < 4 {
			fmt.Fprintf(os.Stderr, "invalid substitution: %q\n", sub)
			return
		}
		delim := sub[1:2]
		// TODO(gaal): paired delimiters, e.g., s{foo}{bar}
		parse := regexp.MustCompile("^s" + delim +
			`((?:\\.|[^` + regexp.QuoteMeta(delim) + `])*)` + delim +
			`((?:\\.|[^` + regexp.QuoteMeta(delim) + `])*)` + delim + "([ig])*$")
		parsedSub := parse.FindStringSubmatch(sub)
		if len(parsedSub) != 4 {
			fmt.Fprint(os.Stderr, "invalid substitution: ", sub)
			return
		}
		global := strings.Contains(parsedSub[3], "g")
		ignoreCase := strings.Contains(parsedSub[3], "i")
		pat := parsedSub[1]
		if ignoreCase {
			pat = "(?i)" + pat
		}
		if search, err := regexp.Compile(pat); err == nil {
			substitutionVals = append(substitutionVals, Substitution{search, parsedSub[2], global})
		} else {
			fmt.Fprint(os.Stderr, "invalid substitution: ", sub)
			return
		}
	}
	return true
}
コード例 #14
0
ファイル: package_test.go プロジェクト: exekias/juju
// TestHelp runs the command with --help as argument and verifies the
// output.
func (s *BaseSubnetSuite) TestHelp(c *gc.C) {
	stderr, stdout, err := s.RunSuperCommand(c, "--help")
	c.Assert(err, jc.ErrorIsNil)
	c.Check(stdout, gc.Equals, "")
	c.Check(stderr, gc.Not(gc.Equals), "")

	// If s.command is set, use it instead of s.superCmd.
	cmdInfo := s.superCmd.Info()
	var expected string
	if s.command != nil {
		// Subcommands embed ModelCommandBase and have an extra
		// "[options]" prepended before the args.
		cmdInfo = s.command.Info()
		expected = "(?sm).*^usage: juju subnet " +
			regexp.QuoteMeta(cmdInfo.Name) +
			`( \[options\])? ` + regexp.QuoteMeta(cmdInfo.Args) + ".+"
	} else {
		expected = "(?sm).*^usage: juju subnet" +
			`( \[options\])? ` + regexp.QuoteMeta(cmdInfo.Args) + ".+"
	}
	c.Check(cmdInfo, gc.NotNil)
	c.Check(stderr, gc.Matches, expected)

	expected = "(?sm).*^purpose: " + regexp.QuoteMeta(cmdInfo.Purpose) + "$.*"
	c.Check(stderr, gc.Matches, expected)

	expected = "(?sm).*^" + regexp.QuoteMeta(cmdInfo.Doc) + "$.*"
	c.Check(stderr, gc.Matches, expected)
}
コード例 #15
0
ファイル: connection_test.go プロジェクト: brunoqc/go-sqlmock
func TestExecNoExpectations(t *testing.T) {
	c := &conn{
		expectations: []expectation{
			&expectedExec{
				queryBasedExpectation: queryBasedExpectation{
					commonExpectation: commonExpectation{
						triggered: true,
						err:       errors.New("WillReturnError"),
					},
					sqlRegex: regexp.MustCompile(regexp.QuoteMeta("otherquery")),
					args:     []driver.Value{456},
				},
			},
		},
	}
	res, err := c.Exec("query", []driver.Value{123})
	if res != nil {
		t.Error("Result should be nil")
	}
	if err == nil {
		t.Error("error should not be nil")
	}
	pattern := regexp.MustCompile(regexp.QuoteMeta("all expectations were already fulfilled, call to exec"))
	if !pattern.MatchString(err.Error()) {
		t.Errorf("error should match expected error message (actual: %s)", err.Error())
	}
}
コード例 #16
0
ファイル: codec_test.go プロジェクト: rogpeppe/juju
func (*suite) TestWriteMessageLogsRequests(c *gc.C) {
	codecLogger := loggo.GetLogger("juju.rpc.jsoncodec")
	defer codecLogger.SetLogLevel(codecLogger.LogLevel())
	codecLogger.SetLogLevel(loggo.TRACE)
	codec := jsoncodec.New(&testConn{})
	h := rpc.Header{
		RequestId: 1,
		Request: rpc.Request{
			Type:   "foo",
			Id:     "id",
			Action: "frob",
		},
	}

	// Check that logging is off by default
	err := codec.WriteMessage(&h, value{X: "param"})
	c.Assert(err, gc.IsNil)
	c.Assert(c.GetTestLog(), gc.Matches, "")

	// Check that we see a log message when we switch logging on.
	codec.SetLogging(true)
	err = codec.WriteMessage(&h, value{X: "param"})
	c.Assert(err, gc.IsNil)
	msg := `{"RequestId":1,"Type":"foo","Id":"id","Request":"frob","Params":{"X":"param"}}`
	c.Assert(c.GetTestLog(), gc.Matches, `.*TRACE juju.rpc.jsoncodec -> `+regexp.QuoteMeta(msg)+`\n`)

	// Check that we can switch it off again
	codec.SetLogging(false)
	err = codec.WriteMessage(&h, value{X: "param"})
	c.Assert(err, gc.IsNil)
	c.Assert(c.GetTestLog(), gc.Matches, `.*TRACE juju.rpc.jsoncodec -> `+regexp.QuoteMeta(msg)+`\n`)
}
コード例 #17
0
ファイル: gitignore.go プロジェクト: rliebling/gitignorer
// NOTE: This is derived from `fnmatch.translate()` and is similar to
// the POSIX function `fnmatch()` with the `FNM_PATHNAME` flag set.
func translateGlob(glob string) string {
	var regex bytes.Buffer
	escape := false

	for i := 0; i < len(glob); i++ {
		char := glob[i]
		// Escape the character.
		switch {
		case escape:
			escape = false
			regex.WriteString(regexp.QuoteMeta(string(char)))
		case char == '\\':
			// Escape character, escape next character.
			escape = true
		case char == '*':
			// Multi-character wildcard. Match any string (except slashes),
			// including an empty string.
			regex.WriteString("[^/]*")
		case char == '?':
			// Single-character wildcard. Match any single character (except
			// a slash).
			regex.WriteString("[^/]")
		case char == '[':
			regex.WriteString(translateBraketExpression(&i, glob))
		default:
			// Regular character, escape it for regex.
			regex.WriteString(regexp.QuoteMeta(string(char)))
		}
	}
	return regex.String()
}
コード例 #18
0
ファイル: client_test.go プロジェクト: jkary/core
func (*DebugHooksClientSuite) TestClientScript(c *gc.C) {
	ctx := debug.NewHooksContext("foo/8")

	// Test the variable substitutions.
	result := debug.ClientScript(ctx, nil)
	// No variables left behind.
	c.Assert(result, gc.Matches, "[^{}]*")
	// tmux new-session -d -s {unit_name}
	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*tmux new-session -s %s(.|\n)*", regexp.QuoteMeta(ctx.Unit)))
	//) 9>{exit_flock}
	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*\\) 9>%s(.|\n)*", regexp.QuoteMeta(ctx.ClientExitFileLock())))
	//) 8>{entry_flock}
	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*\\) 8>%s(.|\n)*", regexp.QuoteMeta(ctx.ClientFileLock())))

	// nil is the same as empty slice is the same as "*".
	// Also, if "*" is present as well as a named hook,
	// it is equivalent to "*".
	c.Assert(debug.ClientScript(ctx, nil), gc.Equals, debug.ClientScript(ctx, []string{}))
	c.Assert(debug.ClientScript(ctx, []string{"*"}), gc.Equals, debug.ClientScript(ctx, nil))
	c.Assert(debug.ClientScript(ctx, []string{"*", "something"}), gc.Equals, debug.ClientScript(ctx, []string{"*"}))

	// debug.ClientScript does not validate hook names, as it doesn't have
	// a full state API connection to determine valid relation hooks.
	expected := fmt.Sprintf(
		`(.|\n)*echo "aG9va3M6Ci0gc29tZXRoaW5nIHNvbWV0aGluZ2Vsc2UK" | base64 -d > %s(.|\n)*`,
		regexp.QuoteMeta(ctx.ClientFileLock()),
	)
	c.Assert(debug.ClientScript(ctx, []string{"something somethingelse"}), gc.Matches, expected)
}
コード例 #19
0
func GenerateFiletypeMatcher(showGlobals bool) (matchingString string) {
	if showGlobals {
		matchingString = fmt.Sprintf(".+(%s|%s)\\z", regexp.QuoteMeta(backupFileSuffix), regexp.QuoteMeta(globalsFileSuffix))
	} else {
		matchingString = fmt.Sprintf(".+%s\\z", regexp.QuoteMeta(backupFileSuffix))
	}
	return
}
コード例 #20
0
ファイル: ex.go プロジェクト: webx-top/webx
func (self *templateEx) InitRegexp() {
	left := regexp.QuoteMeta(self.DelimLeft)
	right := regexp.QuoteMeta(self.DelimRight)
	rfirst := regexp.QuoteMeta(self.DelimRight[0:1])
	self.incTagRegex = regexp.MustCompile(left + self.IncludeTag + `[\s]+"([^"]+)"(?:[\s]+([^` + rfirst + `]+))?[\s]*` + right)
	self.extTagRegex = regexp.MustCompile(left + self.ExtendTag + `[\s]+"([^"]+)"(?:[\s]+([^` + rfirst + `]+))?[\s]*` + right)
	self.blkTagRegex = regexp.MustCompile(`(?s)` + left + self.BlockTag + `[\s]+"([^"]+)"[\s]*` + right + `(.*?)` + left + `\/` + self.BlockTag + right)
}
コード例 #21
0
ファイル: parser.go プロジェクト: dholbach/snappy
func checkReboot(reason string) bool {
	duringReboot := matchString(
		fmt.Sprintf(regexp.QuoteMeta(common.FormatSkipDuringReboot), ".*", ".*"),
		reason)
	afterReboot := matchString(
		fmt.Sprintf(regexp.QuoteMeta(common.FormatSkipAfterReboot), ".*", ".*"),
		reason)
	return duringReboot || afterReboot
}
コード例 #22
0
ファイル: glog_cleanup.go プロジェクト: Go-community/glog
// cleanup deletes old log files created by this binary (not restricted to the
// current pid), when their total size exceeds -log_cleanup_bytes
func cleanup() {
	if *logTotalBytes == -1 {
		return
	}

	// Must match how logName() works.
	logRe, err := regexp.Compile(
		fmt.Sprintf("^%s\\.[^.]+\\.%s\\.log\\.",
			regexp.QuoteMeta(program),
			regexp.QuoteMeta(userName)))
	if err != nil {
		// Print directly to stderr, as we cannot use log (might create a cycle).
		fmt.Fprintf(os.Stderr, "Cannot clean up old logfiles: %v\n", err)
		return
	}

	for _, dir := range logDirs {
		logdir, err := os.Open(dir)
		if err != nil {
			continue
		}
		defer logdir.Close()
		infos, err := logdir.Readdir(-1)
		if err != nil {
			continue
		}

		var total int64
		sorted := make([]string, 0, len(infos))
		sizes := make(map[string]int64)
		for _, fi := range infos {
			name := fi.Name()
			if !logRe.MatchString(name) {
				continue
			}
			sorted = append(sorted, name)
			sizes[name] = fi.Size()
			total += fi.Size()
		}
		// Save the sort if there is nothing to clean up.
		if total < *logTotalBytes {
			continue
		}
		sort.Strings(sorted)

		for total > *logTotalBytes && len(sorted) > 0 {
			oldest := sorted[0]
			name := filepath.Join(dir, sorted[0])
			if err := os.Remove(name); err != nil {
				// Print directly to stderr, as we cannot use log (might create a cycle).
				fmt.Fprintf(os.Stderr, "Could not clean up old logfile %q: %v\n", name, err)
			}
			total -= sizes[oldest]
			sorted = sorted[1:]
		}
	}
}
コード例 #23
0
ファイル: slack.go プロジェクト: thequux/votebot
func (s *SlackSession) Run() {
	client := slack.New(s.Token)
	//client.SetDebug(true)
	rtm := client.NewRTM()
	s.rtm = rtm
	s.log = log.WithField("token", s.Token)

	log.SetLevel(log.DebugLevel)
	s.log.Info("RTM session ready")
	go rtm.ManageConnection()
Loop:
	for {
		select {
		case msg := <-rtm.IncomingEvents:
			s.log.WithField("type", msg.Type).Debug(
				"Received event: " + fmt.Sprintf("%#v", msg.Data))
			switch ev := msg.Data.(type) {
			case *slack.ConnectedEvent:
				s.log = log.WithFields(log.Fields{
					"team":   rtm.GetInfo().Team.Name,
					"myuser": rtm.GetInfo().User.Name,
				})
				s.TeamID = rtm.GetInfo().Team.ID
				s.updateTeam(rtm.GetInfo().Team)
				s.rxAtMessage = regexp.MustCompile(fmt.Sprintf(`^(?:<@%s>: |%s |%s: )(.*)`,
					regexp.QuoteMeta(rtm.GetInfo().User.ID),
					regexp.QuoteMeta(rtm.GetInfo().User.Name),
					regexp.QuoteMeta(rtm.GetInfo().User.Name)))
				for _, user := range rtm.GetInfo().Users {
					s.updateUser(user)
				}
				/*
					pmp := slack.NewPostMessageParameters()
					if _,_,err := rtm.PostMessage("#botdev", "votebot here", pmp); err != nil {
						log.WithError(err).Warn("Failed to post intro message")
					}
				*/
				s.log.Info("Connected")
			case *slack.HelloEvent:
				// Ignore hellos
			case *slack.InvalidAuthEvent:
				s.log.Error("Invalid credentials", ev)
				break Loop
			case *slack.RTMError:
				s.log.WithFields(log.Fields{"code": ev.Code, "msg": ev.Msg}).Warn("RTM error")
			case *slack.UnmarshallingErrorEvent:
				s.log.WithError(ev).Info("Unmarshalling error")
			case *slack.UserChangeEvent:
				s.updateUser(ev.User)
			case *slack.MessageEvent:
				s.handleMessage(ev.User, ev.Channel, &ev.Msg)
			}
		}
	}
}
コード例 #24
0
ファイル: acl.go プロジェクト: firelyu/docker_auth
func (mc *MatchConditions) Matches(ai *AuthRequestInfo) bool {
	vars := []string{
		"${account}", regexp.QuoteMeta(ai.Account),
		"${type}", regexp.QuoteMeta(ai.Type),
		"${name}", regexp.QuoteMeta(ai.Name),
		"${service}", regexp.QuoteMeta(ai.Service),
	}
	return matchString(mc.Account, ai.Account, vars) &&
		matchString(mc.Type, ai.Type, vars) &&
		matchString(mc.Name, ai.Name, vars) &&
		matchIP(mc.IP, ai.IP)
}
コード例 #25
0
ファイル: bundles_test.go プロジェクト: bac/juju
func (s *BundlesDirSuite) TestGet(c *gc.C) {
	basedir := c.MkDir()
	bunsDir := filepath.Join(basedir, "random", "bundles")
	downloader := api.NewCharmDownloader(s.st.Client())
	d := charm.NewBundlesDir(bunsDir, downloader)

	checkDownloadsEmpty := func() {
		files, err := ioutil.ReadDir(filepath.Join(bunsDir, "downloads"))
		c.Assert(err, jc.ErrorIsNil)
		c.Check(files, gc.HasLen, 0)
	}

	// Check it doesn't get created until it's needed.
	_, err := os.Stat(bunsDir)
	c.Assert(err, jc.Satisfies, os.IsNotExist)

	// Add a charm to state that we can try to get.
	apiCharm, sch := s.AddCharm(c)

	// Try to get the charm when the content doesn't match.
	_, err = d.Read(&fakeBundleInfo{apiCharm, nil, "..."}, nil)
	c.Check(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/dummy-1" from API server: `)+`expected sha256 "...", got ".*"`)
	checkDownloadsEmpty()

	// Try to get a charm whose bundle doesn't exist.
	otherURL := corecharm.MustParseURL("cs:quantal/spam-1")
	_, err = d.Read(&fakeBundleInfo{apiCharm, otherURL, ""}, nil)
	c.Check(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/spam-1" from API server: `)+`.* not found`)
	checkDownloadsEmpty()

	// Get a charm whose bundle exists and whose content matches.
	ch, err := d.Read(apiCharm, nil)
	c.Assert(err, jc.ErrorIsNil)
	assertCharm(c, ch, sch)
	checkDownloadsEmpty()

	// Get the same charm again, without preparing a response from the server.
	ch, err = d.Read(apiCharm, nil)
	c.Assert(err, jc.ErrorIsNil)
	assertCharm(c, ch, sch)
	checkDownloadsEmpty()

	// Check the abort chan is honoured.
	err = os.RemoveAll(bunsDir)
	c.Assert(err, jc.ErrorIsNil)
	abort := make(chan struct{})
	close(abort)

	ch, err = d.Read(apiCharm, abort)
	c.Check(ch, gc.IsNil)
	c.Check(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/dummy-1" from API server: download aborted`))
	checkDownloadsEmpty()
}
コード例 #26
0
ファイル: toolsmetadata_test.go プロジェクト: bac/juju
func (s *ToolsMetadataSuite) TestPatchLevels(c *gc.C) {
	if runtime.GOOS == "windows" {
		c.Skip("Skipping on windows, test only set up for Linux tools")
	}
	currentVersion := jujuversion.Current
	currentVersion.Build = 0
	versionStrings := []string{
		currentVersion.String() + "-precise-amd64",
		currentVersion.String() + ".1-precise-amd64",
	}
	metadataDir := osenv.JujuXDGDataHomeDir() // default metadata dir
	toolstesting.MakeTools(c, metadataDir, "released", versionStrings)
	ctx := coretesting.Context(c)
	code := cmd.Main(newToolsMetadataCommand(), ctx, []string{"--stream", "released"})
	c.Assert(code, gc.Equals, 0)
	output := ctx.Stdout.(*bytes.Buffer).String()
	expectedOutput := fmt.Sprintf(`
Finding tools in .*
.*Fetching tools from dir "released" to generate hash: %s
.*Fetching tools from dir "released" to generate hash: %s
.*Writing tools/streams/v1/index2\.json
.*Writing tools/streams/v1/index\.json
.*Writing tools/streams/v1/com\.ubuntu\.juju-released-tools\.json
`[1:], regexp.QuoteMeta(versionStrings[0]), regexp.QuoteMeta(versionStrings[1]))
	c.Assert(output, gc.Matches, expectedOutput)
	metadata := toolstesting.ParseMetadataFromDir(c, metadataDir, "released", false)
	c.Assert(metadata, gc.HasLen, 2)

	filename := fmt.Sprintf("juju-%s-precise-amd64.tgz", currentVersion)
	size, sha256 := toolstesting.SHA256sum(c, filepath.Join(metadataDir, "tools", "released", filename))
	c.Assert(metadata[0], gc.DeepEquals, &tools.ToolsMetadata{
		Release:  "precise",
		Version:  currentVersion.String(),
		Arch:     "amd64",
		Size:     size,
		Path:     "released/" + filename,
		FileType: "tar.gz",
		SHA256:   sha256,
	})

	filename = fmt.Sprintf("juju-%s.1-precise-amd64.tgz", currentVersion)
	size, sha256 = toolstesting.SHA256sum(c, filepath.Join(metadataDir, "tools", "released", filename))
	c.Assert(metadata[1], gc.DeepEquals, &tools.ToolsMetadata{
		Release:  "precise",
		Version:  currentVersion.String() + ".1",
		Arch:     "amd64",
		Size:     size,
		Path:     "released/" + filename,
		FileType: "tar.gz",
		SHA256:   sha256,
	})
}
コード例 #27
0
ファイル: gb_test.go プロジェクト: yonglehou/gb
func TestInfoCmd(t *testing.T) {
	gb := T{T: t}
	defer gb.cleanup()

	gb.tempDir("src")
	gb.cd(gb.tempdir)
	gb.run("info")
	gb.grepStdout(`^GB_PROJECT_DIR="`+regexp.QuoteMeta(gb.tempdir)+`"$`, "missing GB_PROJECT_DIR")
	gb.grepStdout(`^GB_SRC_PATH="`+regexp.QuoteMeta(filepath.Join(gb.tempdir, "src")+string(filepath.ListSeparator)+filepath.Join(gb.tempdir, "vendor", "src"))+`"$`, "missing GB_SRC_PATH")
	gb.grepStdout(`^GB_PKG_DIR="`+regexp.QuoteMeta(filepath.Join(gb.tempdir, "pkg", runtime.GOOS+"-"+runtime.GOARCH))+`"$`, "missing GB_PKG_DIR")
	gb.grepStdout(`^GB_BIN_SUFFIX="-`+runtime.GOOS+"-"+runtime.GOARCH+`"$`, "missing GB_BIN_SUFFIX")
	gb.grepStdout(`^GB_GOROOT="`+regexp.QuoteMeta(runtime.GOROOT())+`"$`, "missing GB_GOROOT")
}
コード例 #28
0
ファイル: bundles_test.go プロジェクト: xushiwei/juju
func (s *BundlesDirSuite) TestGet(c *gc.C) {
	basedir := c.MkDir()
	bunsdir := filepath.Join(basedir, "random", "bundles")
	downloader := api.NewCharmDownloader(s.st.Client())
	d := charm.NewBundlesDir(bunsdir, downloader)

	// Check it doesn't get created until it's needed.
	_, err := os.Stat(bunsdir)
	c.Assert(err, jc.Satisfies, os.IsNotExist)

	// Add a charm to state that we can try to get.
	apiCharm, sch := s.AddCharm(c)

	// Try to get the charm when the content doesn't match.
	_, err = d.Read(&fakeBundleInfo{apiCharm, nil, "..."}, nil)
	c.Assert(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/dummy-1" from API server: `)+`expected sha256 "...", got ".*"`)

	// Try to get a charm whose bundle doesn't exist.
	otherURL := corecharm.MustParseURL("cs:quantal/spam-1")
	_, err = d.Read(&fakeBundleInfo{apiCharm, otherURL, ""}, nil)
	c.Assert(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/spam-1" from API server: `)+`.* not found`)

	// Get a charm whose bundle exists and whose content matches.
	ch, err := d.Read(apiCharm, nil)
	c.Assert(err, jc.ErrorIsNil)
	assertCharm(c, ch, sch)

	// Get the same charm again, without preparing a response from the server.
	ch, err = d.Read(apiCharm, nil)
	c.Assert(err, jc.ErrorIsNil)
	assertCharm(c, ch, sch)

	// Abort a download.
	err = os.RemoveAll(bunsdir)
	c.Assert(err, jc.ErrorIsNil)
	abort := make(chan struct{})
	done := make(chan bool)
	go func() {
		ch, err := d.Read(apiCharm, abort)
		c.Assert(ch, gc.IsNil)
		c.Assert(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/dummy-1" from API server: aborted`))
		close(done)
	}()
	close(abort)
	gitjujutesting.Server.Response(500, nil, nil)
	select {
	case <-done:
	case <-time.After(coretesting.LongWait):
		c.Fatalf("timed out waiting for abort")
	}
}
コード例 #29
0
ファイル: path.go プロジェクト: gotschmarcel/goserv
func (p *pathParser) group() error {
	// Flush, if in the middle of something.
	if p.pBuf.Len() > 1 {
		p.flushPart()
	}

	// Walk over runes until end or until the group is complete.
	quote := true
	level := 1
Loop:
	for !p.p.AtEnd() {
		r := p.p.Next()

		switch {
		case r == '(':
			// Increase group level
			level++
		case r == ')':
			// Decrease group level
			level--
		case r == ':' || r == '/':
			p.p.Back()
			break Loop
		case r == '?':
			// Still quote, group incomplete
			if level == 0 {
				quote = false
			}

			break Loop
		default:
			// Copy runes until the closing brace is found.
			p.pBuf.WriteRune(r)
		}
	}

	part := p.pBuf.String()

	if quote {
		part = fmt.Sprintf("(%s)", part)
		part = regexp.QuoteMeta(part)
	} else {
		part = fmt.Sprintf("(%s)?", regexp.QuoteMeta(part))
		p.simple = false
	}

	p.rxBuf.WriteString(part)
	p.pBuf.Reset()

	return nil
}
コード例 #30
0
ファイル: acl.go プロジェクト: Joeylulu/docker_auth
func (e *ACLEntry) Matches(ai *AuthRequestInfo) bool {
	vars := []string{
		"${account}", regexp.QuoteMeta(ai.Account),
		"${type}", regexp.QuoteMeta(ai.Type),
		"${name}", regexp.QuoteMeta(ai.Name),
		"${service}", regexp.QuoteMeta(ai.Service),
	}
	if matchString(e.Match.Account, ai.Account, vars) &&
		matchString(e.Match.Type, ai.Type, vars) &&
		matchString(e.Match.Name, ai.Name, vars) {
		return true
	}
	return false
}