func (d DeployList) Humanize(body io.Reader) string { dec := json.NewDecoder(body) var deploys Deploys e := dec.Decode(&deploys) Check(e == nil, "failed to unmarshal response", e) title := "DEPLOYID VERSION PROGRESS APPS\n" var b bytes.Buffer for _, deploy := range deploys { b.WriteString(deploy.DeployID) b.WriteString(" ") b.WriteString(deploy.Version) b.WriteString(" ") b.WriteString(strconv.Itoa(deploy.CurrentStep)) b.WriteString("/") b.WriteString(strconv.Itoa(deploy.TotalSteps)) b.WriteString(" ") for _, app := range deploy.AffectedApps { b.WriteString(app) } b.UnreadRune() b.WriteString("\n") } text := title + b.String() return Columnize(text) }
func readRawMsgBytes(buf *bytes.Buffer) ([]byte, error) { nilChar, _, err := buf.ReadRune() if err == io.EOF { return nil, nil } // see if there is a nil char if nilChar == '-' { return nil, nil } buf.UnreadRune() data := buf.Next(buf.Len()) return data, nil }
func ParseSData(buf *bytes.Buffer) (*sysmsg.StrctData, error) { var sData *sysmsg.StrctData startChar, _, err := buf.ReadRune() if err != nil { return sData, err } // [ = 91 // ] = 93 // \ = 92 if startChar == '-' { return nil, nil } // if it doesn't start with '[' return error if startChar != 91 { return sData, BadSDErr } buf.UnreadRune() // now, there exists SData, parse it readSData := true endSData := false //[exampleSDID iut="3" eventSource="Application" eventID="1011"][examplePriority@32473 class="high"] var element sysmsg.SDElement var readParam bool loop: for { startChar, _, err := buf.ReadRune() if err == io.EOF && endSData { return sData, nil } else if err != nil { return sData, err } switch { case startChar == '[' || readSData: //fmt.Println("begin SData") readSData = false endSData = false id, err := parseSDName(buf, sysmsg.SP_VAL_BYTE) //fmt.Println("Parsed ID ", id) if err != nil { return sData, err } element = sysmsg.SDElement{} element.Id = id element.Params = make([]sysmsg.SDParam, 0, 2) readParam = true break case startChar == sysmsg.SP_VAL_BYTE || readParam: //fmt.Println("begin SData SP") // step back one char if readParam { buf.UnreadRune() } readParam = false if endSData { break loop } name, err := parseSDName(buf, '=') if err != nil { return sData, err } val, err := parseSDParamVal(buf) if err != nil { return sData, err } param := sysmsg.SDParam{name, val} element.Params = append(element.Params, param) break case startChar == ']': //fmt.Println("end SData") endSData = true if sData == nil { sData = &sysmsg.StrctData{} sData.Elements = make([]sysmsg.SDElement, 0, 2) } sData.Elements = append(sData.Elements, element) continue loop } } fmt.Println("completed parsing SData", sData) return sData, nil }
func buffer2word(source bytes.Buffer, removeQuote bool) string { var buffer bytes.Buffer lastchar := ' ' quoteNow := NOTQUOTED yenCount := 0 for { ch, _, err := source.ReadRune() if err != nil { break } if ch == '~' && unicode.IsSpace(lastchar) { if home := dos.GetHome(); home != "" { buffer.WriteString(home) } else { buffer.WriteRune('~') } lastchar = '~' continue } if ch == '%' && quoteNow != '\'' && yenCount%2 == 0 { var nameBuf bytes.Buffer for { ch, _, err = source.ReadRune() if err != nil { buffer.WriteRune('%') nameBuf.WriteTo(&buffer) return buffer.String() } if ch == '%' { if value, ok := OurGetEnv(nameBuf.String()); ok { buffer.WriteString(value) } else { buffer.WriteRune('%') nameBuf.WriteTo(&buffer) buffer.WriteRune('%') } break } if ch == '=' { source.UnreadRune() buffer.WriteRune('%') nameBuf.WriteTo(&buffer) break } nameBuf.WriteRune(ch) } continue } if quoteNow != NOTQUOTED && ch == quoteNow && yenCount%2 == 0 { if !removeQuote { buffer.WriteRune(ch) } // Close Quotation. for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } quoteNow = NOTQUOTED } else if (ch == '\'' || ch == '"') && quoteNow == NOTQUOTED && yenCount%2 == 0 { if !removeQuote { buffer.WriteRune(ch) } // Open Qutation. for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } quoteNow = ch if ch == lastchar { buffer.WriteRune(ch) } } else { if ch == '\\' { yenCount++ } else if ch == '\'' || ch == '"' { for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } yenCount = 0 buffer.WriteRune(ch) } else { for ; yenCount > 0; yenCount-- { buffer.WriteRune('\\') } buffer.WriteRune(ch) } } lastchar = ch } for ; yenCount > 0; yenCount-- { buffer.WriteRune('\\') } return buffer.String() }
// Generic Request Handling func (n NetkiRequester) ProcessRequest(partner *NetkiPartner, uri string, method string, bodyData string) (*simplejson.Json, error) { var supported_methods = [...]string{"GET", "POST", "PUT", "DELETE"} var isSupportedMethod = false // make sure we have a supported HTTP method for _, val := range supported_methods { if val == method { isSupportedMethod = true } } if !isSupportedMethod { return &simplejson.Json{}, &NetkiError{fmt.Sprintf("Unsupported HTTP Method: %s", method), make([]string, 0)} } buf := new(bytes.Buffer) if bodyData != "" { _, err := buf.WriteString(bodyData) if err != nil { return &simplejson.Json{}, &NetkiError{"Unable to Write Request Data to Buffer", make([]string, 0)} } } // Create Our Request buffer := new(bytes.Buffer) buffer.WriteString(partner.ApiUrl) if !strings.HasSuffix(partner.ApiUrl, "/") { buffer.UnreadRune() } buffer.WriteString(uri) req, err := http.NewRequest(method, buffer.String(), buf) req.Header.Set("Content-Type", "application/json") if partner.PartnerId == "" && partner.UserKey != nil { sig, err := n.SignRequest(buffer.String(), bodyData, partner.UserKey) if err != nil { return &simplejson.Json{}, err } req.Header.Set("X-Identity", partner.GetUserPublicKey()) req.Header.Set("X-Signature", sig) req.Header.Set("X-Partner-Key", partner.GetKeySigningKey()) req.Header.Set("X-Partner-KeySig", hex.EncodeToString(partner.KeySignature)) } else { req.Header.Set("X-Partner-ID", partner.PartnerId) req.Header.Set("Authorization", partner.ApiKey) } // See if we have an injected HTTPClient var client *http.Client if n.HTTPClient == nil { client = http.DefaultClient } else { client = n.HTTPClient } // Send Our Request resp, err := client.Do(req) if err != nil { return &simplejson.Json{}, &NetkiError{fmt.Sprintf("HTTP Request Failed: %s", err), make([]string, 0)} } // DELETE with 204 Response, Don't Care About Response Data if method == "DELETE" && resp.StatusCode == http.StatusNoContent { return &simplejson.Json{}, nil } // Close the body when we're done with the function defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return &simplejson.Json{}, &NetkiError{fmt.Sprintf("HTTP Body Read Failed: %s", err), make([]string, 0)} } // Get Our JSON Data js, err := simplejson.NewJson(body) if err != nil { return &simplejson.Json{}, &NetkiError{fmt.Sprintf("Error Retrieving JSON Data: %s", err), make([]string, 0)} } // Return message if success is false if !js.Get("success").MustBool(false) { errMsg := new(bytes.Buffer) errMsg.WriteString(js.Get("message").MustString()) resp, _ := js.Get("failures").Array() if resp != nil { errMsg.WriteString(" [FAILURES: ") failures := make([]string, 0) for i := 0; i < len(js.Get("failures").MustArray()); i++ { failures = append(failures, js.Get("failures").GetIndex(i).Get("message").MustString()) } errMsg.WriteString(strings.Join(failures, ", ")) errMsg.WriteString("]") } return &simplejson.Json{}, &NetkiError{errMsg.String(), make([]string, 0)} } return js, nil }
func dequote(source *bytes.Buffer) string { var buffer bytes.Buffer lastchar := ' ' quoteNow := NOTQUOTED yenCount := 0 for { ch, _, err := source.ReadRune() if err != nil { break } if ch == '~' && unicode.IsSpace(lastchar) { if home := dos.GetHome(); home != "" { buffer.WriteString(home) } else { buffer.WriteRune('~') } lastchar = '~' continue } if ch == '%' && quoteNow != '\'' && yenCount%2 == 0 { var nameBuf bytes.Buffer for { ch, _, err = source.ReadRune() if err != nil { buffer.WriteRune('%') buffer.WriteString(nameBuf.String()) return buffer.String() } if ch == '%' { nameStr := nameBuf.String() value := os.Getenv(nameStr) if value != "" { buffer.WriteString(value) } else if m := rxUnicode.FindStringSubmatch(nameStr); m != nil { ucode, _ := strconv.ParseInt(m[1], 16, 32) buffer.WriteRune(rune(ucode)) } else if f, ok := PercentFunc[nameStr]; ok { buffer.WriteString(f()) } else { buffer.WriteRune('%') buffer.WriteString(nameStr) buffer.WriteRune('%') } break } if !unicode.IsLower(ch) && !unicode.IsUpper(ch) && !unicode.IsNumber(ch) && ch != '_' && ch != '+' { source.UnreadRune() buffer.WriteRune('%') buffer.WriteString(nameBuf.String()) break } nameBuf.WriteRune(ch) } continue } if quoteNow != NOTQUOTED && ch == quoteNow && yenCount%2 == 0 { // Close Quotation. for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } quoteNow = NOTQUOTED } else if (ch == '\'' || ch == '"') && quoteNow == NOTQUOTED && yenCount%2 == 0 { // Open Qutation. for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } quoteNow = ch if ch == lastchar { buffer.WriteRune(ch) } } else { if ch == '\\' { yenCount++ } else if ch == '\'' || ch == '"' { for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } yenCount = 0 buffer.WriteRune(ch) } else { for ; yenCount > 0; yenCount-- { buffer.WriteRune('\\') } buffer.WriteRune(ch) } } lastchar = ch } for ; yenCount > 0; yenCount-- { buffer.WriteRune('\\') } return buffer.String() }