grim/resticide

32bc94f303e2
Give the --help output some tlc, also added a --version argument
package loader
import (
"encoding/json"
"errors"
"fmt"
"gopkg.in/xeipuuv/gojsonschema.v0"
"bitbucket.org/rw_grim/resticide/test"
)
var (
schema *gojsonschema.Schema
)
func init() {
tmp, err := loadSchema()
if err != nil {
panic(err.Error())
}
schema = tmp
}
func loadSchema() (*gojsonschema.Schema, error) {
schemaBytes, err := Asset("schema.json")
if err != nil {
return nil, err
}
schemaString := string(schemaBytes[:])
schemaLoader := gojsonschema.NewStringLoader(schemaString)
return gojsonschema.NewSchema(schemaLoader)
}
func parseHeaders(h map[string]interface{}) map[string][]string {
headers := map[string][]string{}
for name, values := range h {
header := []string{}
for _, value := range values.([]interface{}) {
header = append(header, value.(string))
}
headers[name] = header
}
return headers
}
func parseFile(file map[string]interface{}) map[string]string {
tf := map[string]string{}
for k, v := range file {
tf[k] = v.(string)
}
return tf
}
func parseFormData(fd map[string]interface{}) map[string]test.FormData {
formData := map[string]test.FormData{}
for name, ifields := range fd {
fields := ifields.(map[string]interface{})
formDataValue := test.FormData{}
if value, found := fields["value"]; found {
formDataValue.Value = value.(string)
}
if json, found := fields["json"]; found {
formDataValue.JSON = json.(map[string]interface{})
}
if filename, found := fields["filename"]; found {
formDataValue.Filename = filename.(string)
}
formData[name] = formDataValue
}
return formData
}
func parseURLEncoded(encoded map[string]interface{}) map[string]string {
urlEncoded := map[string]string{}
for name, ival := range encoded {
urlEncoded[name] = ival.(string)
}
return urlEncoded
}
func parseResponse(r interface{}) test.Response {
resp := r.(map[string]interface{})
tr := test.Response{}
tr.StatusCode = int(resp["statuscode"].(float64))
if headers, found := resp["headers"].(map[string]interface{}); found {
tr.Headers = parseHeaders(headers)
}
if body, found := resp["body"]; found {
tr.Body = body.(string)
}
if jsonSchmea, found := resp["json-schema"]; found {
tr.JSONSchema = jsonSchmea.(map[string]interface{})
}
if file, found := resp["file"]; found {
tr.File = parseFile(file.(map[string]interface{}))
}
return tr
}
func parseRequest(r interface{}) test.Request {
tr := test.Request{}
req := r.(map[string]interface{})
// path is required
tr.Path = req["path"].(string)
if val, found := req["query"]; found {
tr.Query = val.(map[string]string)
}
if headers, found := req["headers"].(map[string]interface{}); found {
tr.Headers = parseHeaders(headers)
}
if method, found := req["method"]; found {
tr.Method = method.(string)
} else {
tr.Method = "GET"
}
if body, found := req["body"]; found {
tr.Body = body.(string)
}
if binary, found := req["binary"]; found {
tr.Binary = binary.(string)
}
if formData, found := req["form-data"]; found {
tr.FormData = parseFormData(formData.(map[string]interface{}))
}
if urlEncoded, found := req["x-www-form-urlencoded"]; found {
tr.URLEncoded = parseURLEncoded(urlEncoded.(map[string]interface{}))
}
if json, found := req["json"]; found {
tr.JSON = json
}
// parse the response
tr.Response = parseResponse(req["response"].(map[string]interface{}))
return tr
}
func jsonLoader(data []byte) (string, []test.Request, error) {
var t map[string]interface{}
// unmarshal the data
err := json.Unmarshal(data, &t)
if err != nil {
return "", nil, err
}
// validate it
result, err := schema.Validate(gojsonschema.NewGoLoader(t))
if err != nil {
return "", nil, err
}
// was it valid?
if !result.Valid() {
msg := "validation failed"
for _, err := range result.Errors() {
msg += fmt.Sprintf("\n %s %s", err)
}
return "", nil, errors.New(msg)
}
// now build our return values
name := t["name"]
requests := []test.Request{}
if t["requests"] != nil {
for _, req := range t["requests"].([]interface{}) {
requests = append(requests, parseRequest(req))
}
} else {
requests = append(requests, parseRequest(t["request"].(map[string]interface{})))
}
return name.(string), requests, nil
}