作者:ericcapricor
项目:dei
// startAgent executes ssh-agent, and returns a Agent interface to it.
func startAgent(t *testing.T) (client Agent, socket string, cleanup func()) {
if testing.Short() {
// ssh-agent is not always available, and the key
// types supported vary by platform.
t.Skip("skipping test due to -short")
}
bin, err := exec.LookPath("ssh-agent")
if err != nil {
t.Skip("could not find ssh-agent")
}
cmd := exec.Command(bin, "-s")
out, err := cmd.Output()
if err != nil {
t.Fatalf("cmd.Output: %v", err)
}
/* Output looks like:
SSH_AUTH_SOCK=/tmp/ssh-P65gpcqArqvH/agent.15541; export SSH_AUTH_SOCK;
SSH_AGENT_PID=15542; export SSH_AGENT_PID;
echo Agent pid 15542;
*/
fields := bytes.Split(out, []byte(";"))
line := bytes.SplitN(fields[0], []byte("="), 2)
line[0] = bytes.TrimLeft(line[0], "\n")
if string(line[0]) != "SSH_AUTH_SOCK" {
t.Fatalf("could not find key SSH_AUTH_SOCK in %q", fields[0])
}
socket = string(line[1])
line = bytes.SplitN(fields[2], []byte("="), 2)
line[0] = bytes.TrimLeft(line[0], "\n")
if string(line[0]) != "SSH_AGENT_PID" {
t.Fatalf("could not find key SSH_AGENT_PID in %q", fields[2])
}
pidStr := line[1]
pid, err := strconv.Atoi(string(pidStr))
if err != nil {
t.Fatalf("Atoi(%q): %v", pidStr, err)
}
conn, err := net.Dial("unix", string(socket))
if err != nil {
t.Fatalf("net.Dial: %v", err)
}
ac := NewClient(conn)
return ac, socket, func() {
proc, _ := os.FindProcess(pid)
if proc != nil {
proc.Kill()
}
conn.Close()
os.RemoveAll(filepath.Dir(socket))
}
}
作者:GaizkaRubi
项目:mesos-dn
// trimCut cuts the given label at min(maxlen, len(label)) and ensures the left
// and right cutsets are trimmed from their respective ends.
func trimCut(label []byte, maxlen int, left, right string) []byte {
trim := bytes.TrimLeft(label, left)
size := min(len(trim), maxlen)
head := bytes.TrimRight(trim[:size], right)
if len(head) == size {
return head
}
tail := bytes.TrimLeft(trim[size:], right)
if len(tail) > 0 {
return append(head, tail[:size-len(head)]...)
}
return head
}
作者:qni
项目:go-ipf
func testWalkOutputs(t *testing.T, root node.Node, opts Options, expect []byte) {
expect = bytes.TrimLeft(expect, "\n")
buf := new(bytes.Buffer)
walk := func(current State) error {
s := fmt.Sprintf("%d %s\n", current.Depth, current.Node.(*mdag.ProtoNode).Data())
t.Logf("walk: %s", s)
buf.Write([]byte(s))
return nil
}
opts.Func = walk
if err := Traverse(root, opts); err != nil {
t.Error(err)
return
}
actual := buf.Bytes()
if !bytes.Equal(actual, expect) {
t.Error("error: outputs differ")
t.Logf("expect:\n%s", expect)
t.Logf("actual:\n%s", actual)
} else {
t.Logf("expect matches actual:\n%s", expect)
}
}
作者:hagn
项目:watchdo
func (s *Server) ListenAndServe() error {
uaddr, err := net.ResolveUDPAddr("udp", s.Addr)
if err != nil {
return err
}
conn, err := net.ListenUDP("udp", uaddr)
if err != nil {
return err
}
defer conn.Close()
log.Println("listening on", uaddr)
newmsg := make(chan Message)
go messageReceiver(s, newmsg)
for {
b := make([]byte, 1024)
n, addr, err := conn.ReadFrom(b)
if err != nil {
log.Println("error %v", err)
continue
}
heartbeat := Message{From: addr.String()}
b = bytes.TrimLeft(b[:n], "\n")
heartbeat.extract(b) // remove newline
newmsg <- heartbeat
}
}
作者:akave
项目:vfm
func (p ParagraphDetector) Detect(first, second Line, detectors Detectors) Handler {
block := md.ParagraphBlock{}
return HandlerFunc(func(next Line, ctx Context) (bool, error) {
if next.EOF() {
return p.close(block, ctx)
}
if len(block.Raw) == 0 {
block.Raw = append(block.Raw, md.Run(next))
return true, nil
}
prev := Line(block.Raw[len(block.Raw)-1])
// TODO(akavel): support HTML parser & related interactions [#paragraph-line-sequence]
if prev.isBlank() {
return p.close(block, ctx)
}
nextBytes := bytes.TrimRight(next.Bytes, "\n")
if !next.hasFourSpacePrefix() {
if reHorizontalRule.Match(nextBytes) ||
(p.InQuote && bytes.HasPrefix(bytes.TrimLeft(next.Bytes, " "), []byte(">"))) ||
(p.InList && reOrderedList.Match(nextBytes)) ||
(p.InList && reUnorderedList.Match(nextBytes)) {
return p.close(block, ctx)
}
}
block.Raw = append(block.Raw, md.Run(next))
return true, nil
})
}
作者:jostillmann
项目:i3statu
func Default(buffer chan []Module, confpath string) {
cmd := exec.Command("/usr/bin/i3status", "-c", confpath)
reader, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
cmd.Start()
bufreader := bufio.NewReader(reader)
for {
line, _, err := bufreader.ReadLine()
if err != nil {
// fmt.Println(err)
buffer <- []Module{Module{FullText: "error"}}
continue
}
line = bytes.TrimLeft(line, ",")
var modules []Module
err = json.Unmarshal(line, &modules)
if err != nil {
buffer <- []Module{Module{FullText: "error"}}
continue
}
buffer <- modules
}
}
作者:syncor
项目:a2sap
func parseRuleInfo(ruleinfo []byte) (map[string]string, error) {
if !bytes.HasPrefix(ruleinfo, expectedRuleChunkHeader) {
logger.LogSteamError(ErrPacketHeader)
return nil, ErrPacketHeader
}
ruleinfo = bytes.TrimLeft(ruleinfo, headerStr)
numrules := int(binary.LittleEndian.Uint16(ruleinfo[1:3]))
if numrules == 0 {
return nil, ErrNoRules
}
b := bytes.Split(ruleinfo[3:], []byte{0x00})
m := make(map[string]string)
var key string
for i, y := range b {
if i%2 != 1 {
key = strings.TrimRight(string(y), "\x00")
} else {
m[key] = strings.TrimRight(string(b[i]), "\x00")
}
}
return m, nil
}
作者:zche
项目:kat
// concatLine concatinates line with "\\\n" in function expression.
// TODO(ukai): less alloc?
func concatLine(v Value) Value {
switch v := v.(type) {
case literal:
for {
s := string(v)
i := strings.Index(s, "\\\n")
if i < 0 {
return v
}
v = literal(s[:i] + strings.TrimLeft(s[i+2:], " \t"))
}
case tmpval:
for {
b := []byte(v)
i := bytes.Index(b, []byte{'\\', '\n'})
if i < 0 {
return v
}
var buf bytes.Buffer
buf.Write(b[:i])
buf.Write(bytes.TrimLeft(b[i+2:], " \t"))
v = tmpval(buf.Bytes())
}
case expr:
for i := range v {
switch vv := v[i].(type) {
case literal, tmpval:
v[i] = concatLine(vv)
}
}
return v
}
return v
}
作者:simonz0
项目:currenc
func fetchYahooData(t time.Time) (*yahooCurrencyResponse, error) {
r, err := http.Get("http://finance.yahoo.com/connection/currency-converter-cache?date=" + string(toDate(t)))
if err != nil {
return nil, err
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
// Yahoo finance cache returns JavaScript. Remove that.
body = bytes.TrimSpace(body)
body = bytes.TrimLeft(body, "/**/YAHOO.Finance.CurrencyConverter.addConversionRates(")
body = bytes.TrimRight(body, ");")
target := new(yahooCurrencyResponse)
err = json.Unmarshal(body, target)
if err != nil {
return nil, err
}
return target, nil
}
作者:akave
项目:vfm
func trim(region md.Raw) md.Raw {
// rtrim
for len(region) > 0 {
n := len(region)
l := bytes.TrimRight(region[n-1].Bytes, mdutils.Whites)
if len(l) == 0 {
region = region[:n-1]
continue
}
if len(l) < len(region[n-1].Bytes) {
region = append(append(md.Raw{}, region[:n-1]...), md.Run{
Line: region[n-1].Line,
Bytes: l,
})
}
break
}
// ltrim
for len(region) > 0 {
l := bytes.TrimLeft(region[0].Bytes, mdutils.Whites)
if len(l) == 0 {
region = region[1:]
continue
}
if len(l) < len(region[0].Bytes) {
region = append(md.Raw{{
Line: region[0].Line,
Bytes: l,
}}, region[1:]...)
}
break
}
return region
}
作者:ri
项目:golang-stuf
func main() {
whitespace := " \t\r\n"
padded := []byte(" \t\r\n\r\n\r\n hello!!! \t\t\t\t")
trimmed := bytes.Trim(padded, whitespace)
log.Printf("Trim removed runes in %q from the ends of %q to produce %q", whitespace, padded, trimmed)
rhyme := []byte("aabbccddee")
trimFunced := bytes.TrimFunc(rhyme, trimOdd)
log.Printf("TrimFunc removed 'odd' runes from %q to produce %q", rhyme, trimFunced)
leftTrimmed := bytes.TrimLeft(padded, whitespace)
log.Printf("TrimLeft removed runes in %q from the left side of %q to produce %q", whitespace, padded, leftTrimmed)
leftTrimFunced := bytes.TrimLeftFunc(rhyme, trimOdd)
log.Printf("TrimLeftFunc removed 'odd' runes from the left side of %q to produce %q", rhyme, leftTrimFunced)
rightTrimmed := bytes.TrimRight(padded, whitespace)
log.Printf("TrimRight removed runes in %q from the right side of %q to produce %q", whitespace, padded, rightTrimmed)
rightTrimFunced := bytes.TrimRightFunc(rhyme, trimOdd)
log.Printf("TrimRightFunc removed 'odd' runes from the right side of %q to produce %q", rhyme, rightTrimFunced)
spaceTrimmed := bytes.TrimSpace(padded)
log.Printf("TrimSpace trimmed all whitespace from the ends of %q to produce %q", padded, spaceTrimmed)
}
作者:syncor
项目:a2sap
func getRulesInfo(host string, timeout int) ([]byte, error) {
conn, err := net.DialTimeout("udp", host, time.Duration(timeout)*time.Second)
if err != nil {
logger.LogSteamError(ErrHostConnection(err.Error()))
return nil, ErrHostConnection(err.Error())
}
conn.SetDeadline(time.Now().Add(time.Duration(timeout-1) * time.Second))
defer conn.Close()
_, err = conn.Write(rulesChallengeReq)
if err != nil {
logger.LogSteamError(ErrDataTransmit(err.Error()))
return nil, ErrDataTransmit(err.Error())
}
challengeNumResp := make([]byte, maxPacketSize)
_, err = conn.Read(challengeNumResp)
if err != nil {
logger.LogSteamError(ErrDataTransmit(err.Error()))
return nil, ErrDataTransmit(err.Error())
}
if !bytes.HasPrefix(challengeNumResp, expectedRulesRespHeader) {
logger.LogSteamError(ErrChallengeResponse)
return nil, ErrChallengeResponse
}
challengeNum := bytes.TrimLeft(challengeNumResp, headerStr)
challengeNum = challengeNum[1:5]
request := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x56}
request = append(request, challengeNum...)
_, err = conn.Write(request)
if err != nil {
logger.LogSteamError(ErrDataTransmit(err.Error()))
return nil, ErrDataTransmit(err.Error())
}
var buf [maxPacketSize]byte
numread, err := conn.Read(buf[:maxPacketSize])
if err != nil {
logger.LogSteamError(ErrDataTransmit(err.Error()))
return nil, ErrDataTransmit(err.Error())
}
var rulesInfo []byte
if bytes.HasPrefix(buf[:maxPacketSize], multiPacketRespHeader) {
// handle multi-packet response
first := buf[:maxPacketSize]
first = first[:numread]
rulesInfo, err = handleMultiPacketResponse(conn, first)
if err != nil {
logger.LogSteamError(ErrDataTransmit(err.Error()))
return nil, ErrDataTransmit(err.Error())
}
} else {
rulesInfo = make([]byte, numread)
copy(rulesInfo, buf[:numread])
}
return rulesInfo, nil
}
作者:cnhan
项目:pugo-stati
// parser bytes in reader to blocks
func (cp *CommonParser) ParseReader(r io.Reader) ([]Block, error) {
var (
currentBlock Block = nil
blocks []Block = nil
reader = bufio.NewReader(r)
)
for {
lineData, _, err := reader.ReadLine()
// first block
if currentBlock == nil {
if len(lineData) == 0 {
continue
}
if currentBlock = cp.Detect(bytes.TrimLeft(lineData, COMMON_PARSER_PREFIX)); currentBlock == nil {
return nil, errors.New("block-parse-first-error")
}
continue
}
if bytes.HasPrefix(lineData, []byte(COMMON_PARSER_PREFIX)) {
// try to switch
newBlock := cp.Detect(bytes.TrimLeft(lineData, COMMON_PARSER_PREFIX))
if newBlock != nil {
blocks = append(blocks, currentBlock)
currentBlock = newBlock
continue
}
}
// write block
if err := currentBlock.Write(append(lineData, []byte("\n")...)); err != nil {
return nil, err
}
if err != nil {
if err != io.EOF {
return nil, err
}
break
}
}
// do not forget last block
if currentBlock != nil {
blocks = append(blocks, currentBlock)
}
return blocks, nil
}
作者:ckolbec
项目:ircbo
func Decode(raw []byte) (msg *Message) {
msg = new(Message)
raw = bytes.TrimLeft(raw, " ")
if len(raw) <= 0 {
return nil
}
//If message has a prefix, pull it off
if raw[0] == ':' {
ind := bytes.IndexByte(raw, ' ')
msg.Prefix = string(raw[1:ind])
raw = raw[ind+1:]
raw = bytes.TrimLeft(raw, " ")
}
//If message has <trailing> pull it off
if msgStart := bytes.IndexByte(raw, ':'); msgStart > -1 {
trailBytes := raw[msgStart+1:]
raw = raw[0:msgStart]
//Check if the message contains a CTCP command
if len(trailBytes) > 0 && trailBytes[0] == '\x01' {
//Find the terminating 0x01
trailBytes = trailBytes[1:]
ctcpEnd := bytes.IndexByte(trailBytes, ' ')
if ctcpEnd < 0 { //Nothing in the ctcp but the command
msg.Ctcp = string(trailBytes[:len(trailBytes)-1])
} else {
msg.Ctcp = string(trailBytes[:ctcpEnd])
msg.Trailing = string(trailBytes[ctcpEnd+1 : len(trailBytes)-1])
}
} else {
msg.Trailing = string(trailBytes)
}
}
args := bytes.Fields(raw)
msg.Command = string(args[0])
msg.Args = make([]string, len(args)-1)
for i, v := range args[1:] {
msg.Args[i] = string(v)
}
return
}
作者:karalab
项目:etherapi
func (c *StateObject) setAddr(addr []byte, value common.Hash) {
v, err := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
if err != nil {
// if RLPing failed we better panic and not fail silently. This would be considered a consensus issue
panic(err)
}
c.trie.Update(addr, v)
}
作者:udoy
项目:nettaopro
// ParseFile creates a new Config and parses the file configuration from the
// named file.
func LoadConfig(name string) (*Config, error) {
file, err := os.Open(name)
if err != nil {
return nil, err
}
cfg := &Config{
file.Name(),
make(map[int][]string),
make(map[string]string),
make(map[string]int64),
sync.RWMutex{},
}
cfg.Lock()
defer cfg.Unlock()
defer file.Close()
var comment bytes.Buffer
buf := bufio.NewReader(file)
for nComment, off := 0, int64(1); ; {
line, _, err := buf.ReadLine()
if err == io.EOF {
break
}
if bytes.Equal(line, bEmpty) {
continue
}
off += int64(len(line))
if bytes.HasPrefix(line, bComment) {
line = bytes.TrimLeft(line, "#")
line = bytes.TrimLeftFunc(line, unicode.IsSpace)
comment.Write(line)
comment.WriteByte('\n')
continue
}
if comment.Len() != 0 {
cfg.comment[nComment] = []string{comment.String()}
comment.Reset()
nComment++
}
val := bytes.SplitN(line, bEqual, 2)
if bytes.HasPrefix([]byte(strings.TrimSpace(string(val[1]))), bDQuote) {
val[1] = bytes.Trim([]byte(strings.TrimSpace(string(val[1]))), `"`)
}
key := strings.TrimSpace(string(val[0]))
cfg.comment[nComment-1] = append(cfg.comment[nComment-1], key)
cfg.data[key] = strings.TrimSpace(string(val[1]))
cfg.offset[key] = off
}
AppConf = cfg
return cfg, nil
}
作者:elecha
项目:flee
func nextArg(message []byte, sep string) (string, []byte) {
message = bytes.TrimLeft(message, " \n\r\t")
data := bytes.SplitN(message, []byte(sep), 2)
if len(data) == 2 {
return string(data[0]), data[1]
} else {
return string(data[0]), []byte{}
}
}
作者:vetinar
项目:lda
func (l *LDIF) parseEntry(lines [][]byte) (entry *Entry, err error) {
// for i, line := range lines {
// fmt.Fprintf(os.Stderr, "% 2d %s\n", i, line)
// }
if l.version == 0 && bytes.HasPrefix(lines[0], []byte("version:")) {
line := bytes.TrimLeft(lines[0][8:], SPACES)
if version, err := strconv.Atoi(string(line)); err != nil {
return nil, err
} else {
if version != 1 {
return nil, errors.New("Invalid version spec " + string(line))
}
l.version = 1
lines = lines[1:]
}
}
if !bytes.HasPrefix(lines[0], []byte("dn:")) {
return nil, errors.New("Missing dn:")
}
_, val, err := l.parseLine(lines[0])
if err != nil {
return nil, err
}
dn := val
lines = lines[1:]
if bytes.HasPrefix(lines[0], []byte("changetype:")) {
_, val, err := l.parseLine(lines[0])
if err != nil {
return nil, err
}
l.changeType = val
lines = lines[1:]
}
if l.changeType != "" {
return nil, errors.New("change records not supported")
}
attrs := make(map[string][]string)
for i := 0; i < len(lines); i++ {
attr, val, err := l.parseLine(lines[i])
if err != nil {
if !l.RelaxedParser {
return nil, err
} else {
continue
}
}
if _, ok := attrs[attr]; ok {
attrs[attr] = append(attrs[attr], string(val))
} else {
attrs[attr] = []string{string(val)}
}
}
return NewEntry(dn, attrs), nil
}
作者:kiyo
项目:st
// dump request , body true/false, print true/false
func dumpResponse(r *http.Response, b, p bool, host string) []byte {
dump, err := httputil.DumpResponse(r, b)
if err != nil {
log.Println(err.Error())
}
// isGzip := false
// if v, ok := r.Header["Accept-Encoding"]; ok {
// if strings.Contains(v[0], "gzip") {
// isGzip = true
// log.Println("is gzip")
// }
// }
if p {
index := bytes.Index(dump, []byte("\r\n\r\n"))
headers := dump[:index]
body := bytes.TrimLeft(dump[index:], "\r\n\r\n")
// body = bytes.TrimLeft(body, string([]byte{13, 10, 13, 10}))
// if isGzip {
// reader := bytes.NewReader(body)
// g, err := gzip.NewReader(reader)
// if err != nil {
// log.Println(err.Error())
// }
// body, err = ioutil.ReadAll(g)
// if err != nil {
// log.Println(err.Error())
// }
// }
if *veryverbose {
now := time.Now()
dirname := "/tmp/stfdump/" + host
if _, err := os.Stat(dirname); err != nil {
if err := os.MkdirAll(dirname, 0755); err != nil {
log.Fatalln(err.Error())
}
}
filename := fmt.Sprintf("%s/%d<", dirname, now.UnixNano())
ioutil.WriteFile(filename, body, 0644)
}
if *colors {
// color.Printf("@{b}%[email protected]{|}", string(dump))
color.Printf("@{c}%[email protected]{|}\n", string(headers))
if *showBody {
color.Printf("@{g}%[email protected]{|}\n", string(body))
}
// color.Printf("@{g}%[email protected]{|}\n", ehex.EncodeToString(body))
// color.Printf("@{g}%[email protected]{|}\n", body)
} else {
// fmt.Print(string(dump))
fmt.Println(string(headers))
fmt.Println(string(body))
}
}
return dump
}
作者:vetinar
项目:lda
func (l *LDIF) parseLine(line []byte) (attr, val string, err error) {
off := 0
for len(line) > off && line[off] != ':' {
off++
if off >= len(line) {
return
}
}
if off == len(line) {
err = errors.New("Missing : in line")
return
}
if off > len(line)-2 {
err = errors.New("empty value")
return
}
attr = string(line[0:off])
if err = validAttr(attr); err != nil {
attr = ""
val = ""
return
}
switch line[off+1] {
case ':':
var n int
value := bytes.TrimLeft(line[off+2:], SPACES)
// fmt.Fprintf(os.Stderr, "LINE=%s\nVALUE=%s\n", line, value)
dec := make([]byte, base64.StdEncoding.DecodedLen(len(value)))
n, err = base64.StdEncoding.Decode(dec, value)
if err != nil {
return
}
val = string(dec[:n])
case '<': // FIXME missing return for *net.URL type
val = string(bytes.TrimLeft(line[off+2:], SPACES))
default:
val = string(bytes.TrimLeft(line[off+2:], SPACES))
}
return
}