func (c *Client) AddVoucher(v *Voucher, prefix string) (err error) { v.Identifier, err = c.nextVoucherIdentifier(prefix) if err != nil { return } resp, err := c.Get(voucherAddUrl) if err != nil { return } fmt.Println("Looking for Transaction Key") tKey, _, err := extractSubmatch(resp, voucherAddTKeyRegex) if err != nil { err = errors.New("Could not extract Transaction Key.") return } resp, err = c.PostForm(voucherAddUrl, *v.Values(tKey)) respBuf := bytes.Buffer{} _, err = respBuf.ReadFrom(resp.Body) if err != nil { fmt.Println("No body when reading creation response.") err = errors.New("Could not create voucher.") } m, err := regexp.MatchReader("class=\"message success", bytes.NewReader(respBuf.Bytes())) if err != nil || !m { fmt.Println("No success message. Body was:", respBuf.String()) err = errors.New("Could not create voucher.") } else { fmt.Printf("Created Voucher %+v", v) } return }
func main() { timeout := flag.Int("t", 10, "set a timeout in seconds for the read") listen := flag.Bool("l", false, "start a listener instead of a dial") flag.Parse() if len(flag.Args()) < 3 { fmt.Printf("USAGE: %s <regex> <host> <port>\n", os.Args[0]) } term := flag.Args()[0] host := flag.Args()[1] port := flag.Args()[2] target := fmt.Sprintf("%s:%s", host, port) var con net.Conn if *listen { list, err := net.Listen("tcp", target) checkError(err) ncon, err := list.Accept() checkError(err) con = ncon } else { ncon, err := net.Dial("tcp", target) checkError(err) con = ncon } err := con.SetReadDeadline(time.Now().Add(time.Second * time.Duration(*timeout))) checkError(err) r := bufio.NewReaderSize(con, 1) match, err := regexp.MatchReader(term, r) checkError(err) if !match { os.Exit(1) } }
func (expect *ExpectSubprocess) ExpectRegex(regex string) (bool, error) { return regexp.MatchReader(regex, expect.buf) }
func isImageResource(url string) bool { r := bytes.NewReader([]byte(url)) result, _ := regexp.MatchReader("/v1/images/*", r) return result }