func writePullRequestChanges(repo *github.Repo, messageFile string) error { message := ` # Requesting a pull to %s from %s # # Write a message for this pull request. The first block # of the text is the title and the rest is description.%s ` startRegexp := regexp.MustCompilePOSIX("^") endRegexp := regexp.MustCompilePOSIX(" +$") commitLogs, _ := git.Log(repo.Base, repo.Head) var changesMsg string if len(commitLogs) > 0 { commitLogs = strings.TrimSpace(commitLogs) commitLogs = startRegexp.ReplaceAllString(commitLogs, "# ") commitLogs = endRegexp.ReplaceAllString(commitLogs, "") changesMsg = ` # # Changes: # %s` changesMsg = fmt.Sprintf(changesMsg, commitLogs) } message = fmt.Sprintf(message, repo.FullBase(), repo.FullHead(), changesMsg) return ioutil.WriteFile(messageFile, []byte(message), 0644) }
func (p *pullRequestMsg) FormattedCommitLogs() string { startRegexp := regexp.MustCompilePOSIX("^") endRegexp := regexp.MustCompilePOSIX(" +$") commitLogs := strings.TrimSpace(p.CommitLogs) commitLogs = startRegexp.ReplaceAllString(commitLogs, fmt.Sprintf("%s ", p.CS)) commitLogs = endRegexp.ReplaceAllString(commitLogs, "") return commitLogs }
func (g *HttpJsonLibGenerator) Generate() ([]byte, error) { clientAPI, err := g.generateAPI() if err != nil { return nil, err } result := regexp.MustCompilePOSIX(">>>API_NAME<<<").ReplaceAll(mainTemplate, []byte(GetAPIName(g.serviceName))) result = regexp.MustCompilePOSIX(">>>PKG_NAME<<<").ReplaceAll(result, []byte(g.pkgName)) result = regexp.MustCompilePOSIX(">>>CLIENT_API<<<").ReplaceAll(result, clientAPI) result = regexp.MustCompilePOSIX(">>>IMPORTS<<<").ReplaceAll(result, g.collectImports()) return format.Source(result) }
// Search search engines func FindDirectoryURL() (string, error) { ch := make(chan string, 100) fetch_and_parse := func(url string) { s2w := make([]byte, 1) rand.Reader.Read(s2w) for i := 0; i < int(s2w[0])%4; i++ { time.Sleep(time.Second) } resp, err := http.Get(url) if err != nil { return } defer func() { if r := recover(); r != nil { } }() defer resp.Body.Close() thing := new(bytes.Buffer) io.Copy(thing, resp.Body) tosearch := string(thing.Bytes()) rge := regexp.MustCompilePOSIX("Kids in rectangles irritating sick urchins rattling foxes,.*lol") str := rge.FindString(tosearch) arr := strings.Split(str, " ") res := arr[len(arr)-2] buf := new(bytes.Buffer) buf.WriteString("https://") buf.WriteString(res) buf.WriteString("/") ch <- buf.String() } go fetch_and_parse("https://stackoverflow.com/users/2022968/user54609") go fetch_and_parse("https://pastee.org/q2ndr") return <-ch, nil }
// ValidateParameters *must* be implemented by a module. It provides a method // to verify that the parameters passed to the module conform the expected format. // It must return an error if the parameters do not validate. func (r Runner) ValidateParameters() (err error) { fqdn := regexp.MustCompilePOSIX(`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$`) if !fqdn.MatchString(r.Parameters.LookupHost) { return fmt.Errorf("ValidateParameters: LookupHost parameter is not a valid FQDN.") } return }
func main() { log.SetFlags(0) log.SetPrefix("gocyclo: ") flag.Usage = usage flag.Parse() if len(*ignore) == 0 { ignoreRx = nil } else { ignoreRx = regexp.MustCompilePOSIX(*ignore) } args := flag.Args() if len(args) == 0 { usage() } stats := analyze(args) sort.Sort(byComplexity(stats)) written := writeStats(os.Stdout, stats) if *avg { showAverage(stats) } if *over > 0 && written > 0 { os.Exit(1) } }
func waitForCmd(s *Shell) stateFn { timeout := time.After(2 * time.Second) r := regexp.MustCompilePOSIX("^(.*?>)?" + regexp.QuoteMeta(s.scmd) + "[[:space:]]*?$[\r\n]*") // fmt.Println("waitForCmd: looking for '" + r.String() + "'") for { select { case readout := <-s.cout: // fmt.Println("waitForCmd: received '" + readout + "'") s.status.stdout = s.status.stdout + readout if loc := r.FindStringIndex(s.status.stdout); loc != nil { s.status.stdout = s.status.stdout[loc[1]:] return waitForEnd(s) } case readerr := <-s.cerr: anErrIndex := &errIndex{ start: len(s.status.stdout), end: len(s.status.stdout) + len(readerr)} s.status.errs = append(s.status.errs, *anErrIndex) s.status.stdout = s.status.stdout + readerr case _ = <-timeout: fmt.Println("waitForCmd: No cmd detected on shell?! '" + s.scmd + "'") return nil // should actually Panic } } return nil }
func GetCurrentContainerID() string { file, err := os.Open("/proc/self/cgroup") if err != nil { return "" } reader := bufio.NewReader(file) scanner := bufio.NewScanner(reader) scanner.Split(bufio.ScanLines) regex := "/docker/([[:alnum:]]{64})$" re := regexp.MustCompilePOSIX(regex) for scanner.Scan() { _, lines, err := bufio.ScanLines([]byte(scanner.Text()), true) if err == nil { if re.MatchString(string(lines)) { submatches := re.FindStringSubmatch(string(lines)) containerID := submatches[1] return containerID } } } return "" }
func main() { aliases := loadExtAlias() pExt := flag.String("ext", "", "Specify the extension. If not specified, extract from filename") flag.Parse() *pExt = findExtAlias(aliases, removeLeadingDot(*pExt)) args := flag.Args() if len(args) < 1 { printUsage() } pat := flag.Arg(0) fns := villa.Paths(args[1:]...) re := regexp.MustCompilePOSIX(pat) if len(fns) > 0 { for _, fn := range fns { ext := *pExt if ext == "" { ext = findExtAlias(aliases, removeLeadingDot(fn.Ext())) } grep.Grep(re, fn, ext) } } else { grep.Grep(re, "", *pExt) } }
func GetThreadHashHTML(file string) (thread string) { exp := regexp.MustCompilePOSIX(`thread-([0-9a-f]+)\.html`) matches := exp.FindStringSubmatch(file) if len(matches) != 2 { return "" } thread = matches[1] return }
func printDatagram(out *color.Color, d *protocol.Datagram) { re := regexp.MustCompilePOSIX("^00000") out.Printf("FROM: %s\n", d.From) out.Printf("SIZE: %d\n", len(d.Data)) data := strings.TrimLeft(hex.Dump(d.Data), "0") out.Printf("DATA: 000" + re.ReplaceAllString(data, " ")) color.Set(color.Bold) }
func GetGroupForCatalogHTML(file string) (group string) { exp := regexp.MustCompilePOSIX(`catalog-(.+)\.html`) matches := exp.FindStringSubmatch(file) if len(matches) != 2 { return "" } group = matches[1] return }
func (f *Field) parseTag(tag string) { tag = tag[1:len(tag)] //избавляемся от ` rxp := regexp.MustCompilePOSIX(`db:\"([a-z0-9\-_]+)\"`) name := rxp.FindString(tag) if name == "-" { return } f.Name = name[4 : len(name)-1] rxp = regexp.MustCompilePOSIX(`id_([a-z0-9]+)`) if rxp.MatchString(f.Name) { f.Foreign = true f.ForeignTable = f.Name[3:] } rxp = regexp.MustCompilePOSIX(`gen:"([a-z0-9\-_,\'\'\(\)]+)"`) gen := rxp.FindString(tag) if len(gen) < 6 { return } opts := strings.Split(gen[5:len(gen)-1], ",") if len(opts) > 0 { f.Type = opts[0] } for _, opt := range opts { if opt == f.Type { continue } switch opt { case "autoincrement": f.AutoIncrement = true case "notnull": f.NotNull = true case "primary": f.Primary = true case "unique": f.Uniqeu = true case "index": f.Index = true default: rxp = regexp.MustCompilePOSIX(`default\(.+\)`) def := rxp.FindString(opt) f.Default = def[8 : len(def)-1] } } }
// Tries to find an appropriate point to word wrap line, taking shell escape codes into account. // (Note that because the escape codes are not visible, we can run past the max length for one of them) func findSplit(line string, guess int) int { if guess >= len(line) { return len(line) } r := regexp.MustCompilePOSIX(fmt.Sprintf(".{%d,%d}(\\x1b[^m]+m)?", guess/2, guess)) m := r.FindStringIndex(line) if m != nil { return m[1] // second element in slice is the end index } return guess // Dunno what to do at this point. It's probably unlikely to happen often though. }
func pullRequestChangesMessage(base, head, fullBase, fullHead string, commits []string) (string, error) { var defaultMsg, commitSummary string if len(commits) == 1 { msg, err := git.Show(commits[0]) if err != nil { return "", err } defaultMsg = fmt.Sprintf("%s\n", msg) } else if len(commits) > 1 { commitLogs, err := git.Log(base, head) if err != nil { return "", err } if len(commitLogs) > 0 { startRegexp := regexp.MustCompilePOSIX("^") endRegexp := regexp.MustCompilePOSIX(" +$") commitLogs = strings.TrimSpace(commitLogs) commitLogs = startRegexp.ReplaceAllString(commitLogs, "# ") commitLogs = endRegexp.ReplaceAllString(commitLogs, "") commitSummary = ` # # Changes: # %s` commitSummary = fmt.Sprintf(commitSummary, commitLogs) } } message := `%s # Requesting a pull to %s from %s # # Write a message for this pull request. The first block # of the text is the title and the rest is description.%s ` message = fmt.Sprintf(message, defaultMsg, fullBase, fullHead, commitSummary) return message, nil }
func (g *HttpJsonLibGenerator) generateAPI() ([]byte, error) { var result bytes.Buffer var typesBuf bytes.Buffer for _, path := range g.hm.GetHandlersPaths() { info := g.hm.GetHandlerInfo(path) for _, v := range info.Versions { inTypeName, outTypeName, err := g.printHandlerInOutTypes(&typesBuf, v.Request.Type, v.Response) if err != nil { return nil, err } handlerTypeName := strings.Replace(strings.Title(v.Route), "/", "", -1) handlerTypeName = strings.Replace(handlerTypeName, "_", "", -1) errVarName := g.printHandlerMethodError(&typesBuf, handlerTypeName, v.Errors) method := regexp.MustCompilePOSIX(">>>HANDLER_PATH<<<").ReplaceAll(handlerCallPostFuncTemplate, []byte(v.Route)) method = regexp.MustCompilePOSIX(">>>HANDLER_NAME<<<").ReplaceAll(method, []byte(handlerTypeName)) method = regexp.MustCompilePOSIX(">>>INPUT_TYPE<<<").ReplaceAll(method, []byte(inTypeName)) method = regexp.MustCompilePOSIX(">>>RETURNED_TYPE<<<").ReplaceAll(method, []byte(outTypeName)) method = regexp.MustCompilePOSIX(">>>HANDLER_ERRORS<<<").ReplaceAll(method, []byte(errVarName)) method = regexp.MustCompilePOSIX(">>>API_NAME<<<").ReplaceAll(method, []byte(GetAPIName(g.serviceName))) result.Write(method) } } typesBuf.WriteTo(&result) return result.Bytes(), nil }
// FindGithubInfo tries to find the username and repository // of the current git project. // // We do this using git remote -v. If it works, we should get something like: // // origin [email protected]:cassava/repoctl.git (fetch) // origin [email protected]:cassava/repoctl.git (push) // // This is one of multiple formats. We need to expand this with time to cover // all the different formats that it can have. // // 1. Check if github is there // 2. Check protocol and extract information // a) git protocol: `[email protected]:username/repository.git` // b) https protocol: `https://github.com/username/repository.git` // func FindGithubInfo(_ string) (*GithubRepo, error) { out, err := exec.Command("git", "remote", "-v").Output() if err != nil { return nil, err } r := regexp.MustCompilePOSIX(`([^\t]+)\t(git@|http://|https://)github\.com[/:]([^/]+)/(.+)\.git[ \t]*.*`) infos := make(map[string][2]string) scanner := bufio.NewScanner(bytes.NewBuffer(out)) for scanner.Scan() { s := scanner.Text() if !strings.Contains(s, "github") { continue } if !r.MatchString(s) { fmt.Fprintf(os.Stderr, "Warning: cannot match %q.\n", s) continue } ls := r.FindStringSubmatch(s) source, _, user, repo := ls[1], ls[2], ls[3], ls[4] infos[source] = [2]string{user, repo} } if err := scanner.Err(); err != nil { return nil, err } var up [2]string if len(infos) == 0 { return nil, errors.New("not a GitHub repository") } else if len(infos) == 1 { for _, v := range infos { up = v } } else { // Try the origin repository first; if it exists, it is bound to be the right one. if v, ok := infos["origin"]; ok { up = v } else { for k, v := range infos { fmt.Fprintf(os.Stderr, "Warning: multiple GitHub repositories; using source %q.\n", k) up = v break } } } return &GithubRepo{ User: up[0], Name: up[1], }, nil }
// generate a thumbnailer that uses ffmpeg func FFMpegThumbnailer(ffmpegPath string, conf *Config) Thumbnailer { if conf == nil { conf = defaultCfg } return &ExecThumbnailer{ Exec: ffmpegPath, Accept: regexp.MustCompilePOSIX(`\.(mkv|mp4|avi|webm|ogv|mov|m4v|mpg)$`), GenArgs: func(inf, outf string) []string { outf += ".jpeg" return []string{"-i", inf, "-vf", fmt.Sprintf("scale=%d:%d", conf.ThumbW, conf.ThumbH), "-vframes", "1", outf} }, } }
func grt_CompilePOSIX() { // the POSIX engine will prefer the leftmost-longest match. It will not // return after finding the first match, but will check that the found // match is indeed the longest one. s := "ABCDEEEEE" rr := regexp.MustCompile(`ABCDE{2}|ABCDE{4}`) rp := regexp.MustCompilePOSIX(`ABCDE{2}|ABCDE{4}`) fmt.Println(rr.FindAllString(s, 2)) // <- first acceptable match fmt.Println(rp.FindAllString(s, 2)) // POSIX wants the longer match }
func addRegexFilter() { var reg *regexp.Regexp if posix { reg = regexp.MustCompilePOSIX(pattern) } else { reg = regexp.MustCompile(pattern) } walker.IncludeFile = func(s string) bool { return reg.MatchString(s) } }
func wiki(uri string) string { langRex := regexp.MustCompilePOSIX(`[a-z]+\.wiki`) lang := langRex.FindString(uri) lang = lang[:len(lang)-5] articleRex := regexp.MustCompilePOSIX(`.org\/wiki\/[a-zA-Z0-9а-яА-Я_\(\)\%\,\:]+`) article := articleRex.FindString(uri) article = article[10:] fmt.Println(article) wikiUrl := "https://" + lang + ".wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=" + article resp, err := http.Get(wikiUrl) if err != nil { return "" } defer resp.Body.Close() jsraw, err := ioutil.ReadAll(resp.Body) if err != nil { return "" } wikiSearch := new(struct { Query struct { Pages map[string]map[string]interface{} `json:"pages"` } `json:"query"` }) err = json.Unmarshal(jsraw, wikiSearch) fmt.Println(string(jsraw)) for _, page := range wikiSearch.Query.Pages { var msg string if title, ok := page["title"].(string); ok { msg += "*" + title + "*\n" if extract, ok := page["extract"].(string); ok { msg += "```" + extract + "```" } } return msg } return "" }
func GetGroupAndPageHTML(file string) (board string, page int) { exp := regexp.MustCompilePOSIX(`(.*)-([0-9]+)\.html`) matches := exp.FindStringSubmatch(file) if len(matches) != 3 { return "", -1 } var err error board = matches[1] tmp := matches[2] page, err = strconv.Atoi(tmp) if err != nil { page = -1 } return }
func addRegexpFilter() { var reg *regexp.Regexp if posix { reg = regexp.MustCompilePOSIX(pattern) } else { reg = regexp.MustCompile(pattern) } matchOK = func(s string) bool { b := slurpStripNullByte(s) return reg.Match(b) } }
// create an imagemagick thumbnailer func ImageMagickThumbnailer(convertPath string, conf *Config) Thumbnailer { if conf == nil { conf = defaultCfg } return &ExecThumbnailer{ Exec: convertPath, Accept: regexp.MustCompilePOSIX(`\.(png|jpg|jpeg|gif|webp)$`), GenArgs: func(inf, outf string) []string { if strings.HasSuffix(inf, ".gif") { inf += "[0]" } if conf.JpegOnly { outf += ".jpeg" } return []string{"-thumbnail", fmt.Sprintf("%d", conf.ThumbW), inf, outf} }, } }
func main() { conn, err := protocol.Connect() if err != nil { panic(err.Error()) } datagrams := make(chan protocol.Datagram) bytes := make(chan []byte) go func() { for d := range conn.Datagrams { datagrams <- d bytes <- d.Data } close(datagrams) close(bytes) }() msgs, errs := protocol.NewMessageDecoder(datagrams) re := regexp.MustCompilePOSIX("^00000") for { b := <-bytes out := color.New(color.FgWhite) out.Printf("DATA: length=%d\n", len(b)) out.Print(re.ReplaceAllString(hex.Dump(b), " ")) color.Set(color.Bold) select { case msg := <-msgs: out.Add(color.FgGreen) out.Printf("MSG: %+v\n %T %+v", msg.Header, msg.Payload, msg.Payload) case err := <-errs: out.Add(color.FgRed) out.Printf("ERR: %s", err.Error()) } fmt.Printf("\n\n") } }
func (l *lexer) readTokenMatchingRegexp(pattern string) { pattern = "^" + pattern reg := re.MustCompilePOSIX(pattern) indexes := reg.FindStringIndex(l.input[l.position-1:]) if indexes == nil { panic("No match") } if indexes[0] != 0 { // shouldn't happen due to anchoring panic("Unexpected regex match") } for i := 0; i < indexes[1]-1; i++ { l.read() } }
func parseOpts() error { flag.Usage = usageFunc key := flag.String("key", "", "A POSIX regexp to filter a key. Doesn't affect -stdin filtering.") filter := flag.String("filter", "{}", "A JSON object as filter: {\"username\": \"moon\"}.") or := flag.Bool("or", false, "Filter on Key OR filter.") url := flag.String("url", "http://localhost:8091", "Couchbase URL.") bucket := flag.String("bucket", "", "Listen on Couchbase bucket name.") pool := flag.String("pool", "default", "Couchbase pool name.") stdin := flag.Bool("stdin", false, "Listen on stdin.") flag.Parse() if *bucket == "" && *stdin == false { return errors.New("Either -stdin or -bucket has to be specified.") } opts.RegexpKey = regexp.MustCompilePOSIX(*key) opts.Stdin = *stdin opts.Url = *url opts.Or = *or opts.Pool = *pool opts.Bucket = *bucket opts.Key = *key var v interface{} err := json.Unmarshal([]byte(*filter), &v) if err != nil { return err } if filter, ok := v.(map[string]interface{}); ok { opts.Filter = filter } else { return errors.New("Filter must be a JSON object.") } return nil }
func getPrivatePPAFromConf(f *os.File) (res []string) { res = make([]string, 5) reader := bufio.NewReader(f) re := regexp.MustCompilePOSIX("^\\[(.+)\\]$") for { line, err := reader.ReadString('\n') if err != nil && err != io.EOF { break } if match := re.FindStringSubmatch(line); len(match) == 2 { res = append(res, match[1]) } if err == io.EOF { break } } return res }
func main() { flag.Parse() log.Printf("connecting to Slack API...") bot, err := NewSlackBot(*token) panicOnErr(err) for { log.Printf("awaiting message") select { case <-bot.Quit: log.Printf("quit") return case msg := <-bot.Messages: data, _ := json.Marshal(msg) log.Printf("new message %s", string(data)) if msg.Type == "message" { if !bot.IsBot(msg.User) { fmt.Println("Author:", bot.UserIdToName(msg.User)) fmt.Println("Channel:", bot.ChannelIdToName(msg.Channel)) fmt.Println("Text:", msg.Text) playgroundUrl := regexp.MustCompile(`https?\:\/\/play\.golang\.org\/p\/([a-zA-Z0-9]+)`) wikiUrl := regexp.MustCompilePOSIX(`https?\:\/\/[a-z]*\.wikipedia\.org\/wiki\/([a-zA-Z0-9%_\(\)а-яА-я\,\:]+)`) switch { case playgroundUrl.MatchString(msg.LowerText()): uris := playgroundUrl.FindAllString(msg.Text, -1) for _, uri := range uris { bot.SendMessage(msg.Channel, playground(uri)) } case wikiUrl.MatchString(msg.LowerText()): uris := wikiUrl.FindAllString(msg.LowerText(), -1) for _, uri := range uris { bot.SendMessage(msg.Channel, wiki(uri)) } case strings.Contains(msg.LowerText(), "pong"): bot.SendMessage(msg.Channel, "@"+bot.UserIdToName(msg.User)+": ping") } } } } } }
func (s NaturalSort) Less(i, j int) bool { r := regexp.MustCompilePOSIX(`^([^0-9]*)+|[0-9]+`) spliti := r.FindAllString(strings.Replace(s[i], " ", "", -1), -1) splitj := r.FindAllString(strings.Replace(s[j], " ", "", -1), -1) splitshortest := len(spliti) if len(spliti) > len(splitj) { splitshortest = len(splitj) } for index := 0; index < splitshortest; index++ { if spliti[index] != splitj[index] { inti, ei := strconv.Atoi(spliti[index]) intj, ej := strconv.Atoi(splitj[index]) if ei == nil && ej == nil { //if number return inti < intj } return spliti[index] < splitj[index] } } return s[i] < s[j] }