示例#1
0
文件: util.go 项目: donny-dont/drone
// argsToEnv uses reflection to convert a map[string]interface to a list
// of environment variables.
func argsToEnv(from map[string]interface{}, to map[string]string) error {

	for k, v := range from {
		if v == nil {
			to[k] = ""
			continue
		}

		t := reflect.TypeOf(v)
		vv := reflect.ValueOf(v)

		k = "PLUGIN_" + strings.ToUpper(k)

		switch t.Kind() {
		case reflect.Bool:
			to[k] = strconv.FormatBool(vv.Bool())

		case reflect.String:
			to[k] = vv.String()

		case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
			to[k] = fmt.Sprintf("%v", vv.Int())

		case reflect.Float32, reflect.Float64:
			to[k] = fmt.Sprintf("%v", vv.Float())

		case reflect.Map:
			yml, _ := yaml.Marshal(vv.Interface())
			out, _ := json.YAMLToJSON(yml)
			to[k] = string(out)

		case reflect.Slice:
			out, err := yaml.Marshal(vv.Interface())
			if err != nil {
				return err
			}

			in := []string{}
			err = yaml.Unmarshal(out, &in)
			if err == nil {
				to[k] = strings.Join(in, ",")
			} else {
				out, err = json.YAMLToJSON(out)
				if err != nil {
					return err
				}
				to[k] = string(out)
			}
		}
	}

	return nil
}
示例#2
0
文件: index.go 项目: slack/helm
// loadUnversionedIndex loads a pre-Alpha.5 index.yaml file.
//
// This format is deprecated. This function will be removed prior to v2.0.0.
func loadUnversionedIndex(data []byte) (*IndexFile, error) {
	fmt.Fprintln(os.Stderr, "WARNING: Deprecated index file format. Try 'helm repo update'")
	i := map[string]unversionedEntry{}

	// This gets around an error in the YAML parser. Instead of parsing as YAML,
	// we convert to JSON, and then decode again.
	var err error
	data, err = yaml.YAMLToJSON(data)
	if err != nil {
		return nil, err
	}
	if err := json.Unmarshal(data, &i); err != nil {
		return nil, err
	}

	if len(i) == 0 {
		return nil, ErrNoAPIVersion
	}
	ni := NewIndexFile()
	for n, item := range i {
		if item.Chartfile == nil || item.Chartfile.Name == "" {
			parts := strings.Split(n, "-")
			ver := ""
			if len(parts) > 1 {
				ver = strings.TrimSuffix(parts[1], ".tgz")
			}
			item.Chartfile = &chart.Metadata{Name: parts[0], Version: ver}
		}
		ni.Add(item.Chartfile, item.URL, "", item.Checksum)
	}
	return ni, nil
}
示例#3
0
文件: scanner.go 项目: kego/ke
func ProcessFile(file string) ([]byte, error) {

	ext := filepath.Ext(file)
	isYaml := ext == ".yaml" || ext == ".yml"
	isJson := ext == ".json"

	if !isYaml && !isJson {
		return nil, nil
	}

	bytes, err := ioutil.ReadFile(file)
	if err != nil {
		return nil, kerr.Wrap("NMWROTKPLJ", err)
	}

	if isYaml {
		j, err := yaml.YAMLToJSON(bytes)
		if err != nil {
			return nil, kerr.Wrap("FAFJCYESRH", err)
		}
		bytes = j
	}

	return bytes, nil
}
示例#4
0
文件: yaml_to_json.go 项目: y13i/j2y
func YamlToJson(inputBytes []byte, minify bool) []byte {
	buffer, err := yaml.YAMLToJSON(inputBytes)

	if err != nil {
		fmt.Println("YAML -> JSON convert error.")
		fmt.Printf("err: %v\n", err)
		os.Exit(1)
	}

	if minify {
		return buffer
	} else {
		jsonData, err := simplejson.NewJson(buffer)

		if err != nil {
			fmt.Println("JSON parse error.")
			fmt.Printf("err: %v\n", err)
			os.Exit(1)
		}

		prettyBytes, err := jsonData.EncodePretty()

		if err != nil {
			fmt.Println("JSON encode error.")
			fmt.Printf("err: %v\n", err)
			os.Exit(1)
		}

		return prettyBytes
	}
}
示例#5
0
文件: swagson.go 项目: sfodje/swagson
// handleMeta extracts data from api:meta markup
// there should only be one api:meta markup tag per project
// if there is more than one, the first tag will be used
func handleMeta(swagDoc *specs.SwagDoc, docs []string) error {
	swagDoc.Swagger = "2.0"
	y := []byte(docs[0])
	j, err := yaml.YAMLToJSON(y)
	err = json.Unmarshal(j, swagDoc)
	return err
}
示例#6
0
文件: cfgfile.go 项目: Collinux/snap
// Read an input configuration file, parsing it (as YAML or JSON)
// into the input 'interface{}', v
func Read(path string, v interface{}, schema string) []serror.SnapError {
	// read bytes from file
	b, err := cfgReader.ReadFile(path)
	if err != nil {
		return []serror.SnapError{serror.New(err)}
	}
	// convert from YAML to JSON (remember, JSON is actually valid YAML)
	jb, err := yaml.YAMLToJSON(b)
	if err != nil {
		return []serror.SnapError{serror.New(fmt.Errorf("error converting YAML to JSON: %v", err))}
	}
	// validate the resulting JSON against the input the schema
	if errors := cfgValidator.validateSchema(schema, string(jb)); errors != nil {
		// if invalid, construct (and return?) a SnapError from the errors identified
		// during schema validation
		return errors
	}
	// if valid, parse the JSON byte-stream (above)
	if parseErr := json.Unmarshal(jb, v); parseErr != nil {
		// remove any YAML-specific prefix that might have been added by then
		// yaml.Unmarshal() method or JSON-specific prefix that might have been
		// added if the resulting JSON string could not be marshalled into our
		// input interface correctly (note, if there is no match to either of
		// these prefixes then the error message will be passed through unchanged)
		tmpErr := strings.TrimPrefix(parseErr.Error(), "error converting YAML to JSON: yaml: ")
		errRet := strings.TrimPrefix(tmpErr, "error unmarshaling JSON: json: ")
		return []serror.SnapError{serror.New(fmt.Errorf("Error while parsing configuration file: %v", errRet))}
	}
	return nil
}
示例#7
0
func main() {
	args := os.Args[1:]
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "missing input YAML file")
		return
	}

	// Load YAML file:
	ymlbytes, err := ioutil.ReadFile(args[0])
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}

	jsonbytes, err := yaml.YAMLToJSON(ymlbytes)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}

	outName := FileName(args[0]) + ".json"
	err = ioutil.WriteFile(outName, jsonbytes, 0644)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
}
示例#8
0
func Parsefile(y []byte) ([]byte, error) {
	file, err := yaml.YAMLToJSON(y)
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return nil, err
	}
	return file, nil
}
示例#9
0
文件: state.go 项目: vektorlab/otter
/*
Load a StateMap from a YAML byte array
*/
func StateMapFromYaml(data []byte) (*StateMap, error) {
	j, err := yaml.YAMLToJSON(data)
	if err != nil {
		return nil, err
	}
	return StateMapFromJson(j)

}
func yamlToJSON(y []byte) ([]byte, error) {
	j, err := yaml.YAMLToJSON(y)
	if err != nil {
		return nil, fmt.Errorf("yaml to json failed: %v\n%v\n", err, y)
	}

	return j, nil
}
示例#11
0
文件: loader.go 项目: tsandall/opa
// unmarshalYAML re-implements yaml.Unmarshal so that the JSON decoder can have
// UseNumber set.
func unmarshalYAML(y []byte, o interface{}) error {
	bs, err := yaml.YAMLToJSON(y)
	if err != nil {
		return fmt.Errorf("error converting YAML to JSON: %v", err)
	}
	buf := bytes.NewBuffer(bs)
	decoder := util.NewJSONDecoder(buf)
	return decoder.Decode(o)
}
示例#12
0
文件: loader.go 项目: kulikov/serve
func LoadFile(path string) (*gabs.Container, error) {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, errors.New(color.RedString("Manifest file `%s` not found: %v", path, err))
	}

	if jsonData, err := yaml.YAMLToJSON(data); err != nil {
		return nil, errors.New(color.RedString("Error on parse manifest %s: %v!", path, err))
	} else {
		return gabs.ParseJSON(jsonData)
	}
}
示例#13
0
// ToJSON converts a single YAML document into a JSON document
// or returns an error. If the document appears to be JSON the
// YAML decoding path is not used (so that error messages are)
// JSON specific.
func ToJSON(data []byte, checkValid bool) ([]byte, error) {
	if hasJSONPrefix(data) {
		if checkValid {
			var obj interface{}
			if err := json.Unmarshal(data, &obj); err != nil {
				return nil, err
			}
		}
		return data, nil
	}
	return yaml.YAMLToJSON(data)
}
示例#14
0
文件: swagson.go 项目: sfodje/swagson
// swagDocToJson converts a swagDoc struct to json and returns a pointer
func swagDocToJson(swagDoc *specs.SwagDoc) (*[]byte, error) {
	y, err := yaml.Marshal(swagDoc)
	if err != nil {
		return nil, err
	}

	j, err := yaml.YAMLToJSON(y)
	if err != nil {
		return nil, err
	}

	return &j, err
}
示例#15
0
func str2yaml() {
	y := []byte(`name: jeno
age: 33
food:
  - pacal
  - rum
`)
	j2, err := yaml.YAMLToJSON(y)
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return
	}
	fmt.Println(string(j2))
}
示例#16
0
// Decode reads a YAML document as JSON from the stream or returns
// an error. The decoding rules match json.Unmarshal, not
// yaml.Unmarshal.
func (d *YAMLToJSONDecoder) Decode(into interface{}) error {
	if d.scanner.Scan() {
		data, err := yaml.YAMLToJSON(d.scanner.Bytes())
		if err != nil {
			return err
		}
		return json.Unmarshal(data, into)
	}
	err := d.scanner.Err()
	if err == nil {
		err = io.EOF
	}
	return err
}
示例#17
0
文件: main.go 项目: dearing/tools
//go:generate goversion -major=1 -minor=0 -patch=0
func main() {

	bytes, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		log.Panicln(err)
	}

	json, err := yaml.YAMLToJSON(bytes)
	if err != nil {
		log.Panicln(err)
	}

	print(string(json))
}
示例#18
0
func main() {
	yml, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		log.Fatalf("Could not read from standard in: %v", err)
	}
	jsn, err := yaml.YAMLToJSON(yml)
	if err != nil {
		log.Fatalf("Could not convert to yaml: %v", err)
	}
	formatted, err := FormatJSON(jsn)
	if err != nil {
		log.Fatalf("Could not format valid json: %v", err)
	}
	fmt.Println(string(formatted))
}
示例#19
0
// Decode reads a YAML document as JSON from the stream or returns
// an error. The decoding rules match json.Unmarshal, not
// yaml.Unmarshal.
func (d *YAMLToJSONDecoder) Decode(into interface{}) error {
	bytes, err := d.reader.Read()
	if err != nil && err != io.EOF {
		return err
	}

	if len(bytes) != 0 {
		data, err := yaml.YAMLToJSON(bytes)
		if err != nil {
			return err
		}
		return json.Unmarshal(data, into)
	}
	return err
}
示例#20
0
// load is a modified copy of k8s.io/kubernetes/pkg/client/unversioned/clientcmd.Load
// Load takes a byte slice and deserializes the contents into Config object.
// Encapsulates deserialization without assuming the source is a file.
func load(data []byte) (*clientcmdConfig, error) {
	config := clientcmdNewConfig()
	// if there's no data in a file, return the default object instead of failing (DecodeInto reject empty input)
	if len(data) == 0 {
		return config, nil
	}
	// Note: This does absolutely no kind/version checking or conversions.
	data, err := yaml.YAMLToJSON(data)
	if err != nil {
		return nil, err
	}
	if err := json.Unmarshal(data, config); err != nil {
		return nil, err
	}
	return config, nil
}
示例#21
0
文件: swagson.go 项目: sfodje/swagson
// handleRoute extracts data from the api:route markup
func handleRoute(swagDoc *specs.SwagDoc, docs []string) error {
	for _, doc := range docs {
		var paths = make(map[string]specs.SwagPath)
		y := []byte(doc)
		j, err := yaml.YAMLToJSON(y)
		err = json.Unmarshal(j, &paths)
		swagDoc.Paths = &paths
		if err != nil {
			return err
		}
		for k, v := range paths {
			paths[k] = v
		}
	}
	return nil
}
示例#22
0
文件: main.go 项目: Pendoragon/code
func main() {
	filename, _ := filepath.Abs("./pod.yaml")
	yamlFile, err := ioutil.ReadFile(filename)

	if err != nil {
		panic(err)
	}

	var pod k8s_api.Pod
	j, err := yaml.YAMLToJSON(yamlFile)
	err = json.Unmarshal(j, &pod)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Value: %#v\n", pod)
}
func (job *Job) loadJsonSchema(URL string) (subSchema *JsonSubSchema, err error) {
	var body io.ReadCloser
	if strings.HasPrefix(URL, "file://") {
		body, err = os.Open(URL[7 : len(URL)-1]) // need to strip trailing '#'
		if err != nil {
			return
		}
	} else {
		u, err := url.Parse(URL)
		if err != nil {
			return subSchema, err
		}
		var resp *http.Response
		// TODO: may be better to use https://golang.org/pkg/net/http/#NewFileTransport here??
		switch u.Scheme {
		case "http", "https":
			resp, err = http.Get(URL)
			if err != nil {
				return subSchema, err
			}
			body = resp.Body
		default:
			fmt.Printf("Unknown scheme: '%s'\n", u.Scheme)
			fmt.Printf("URL: '%s'\n", URL)
		}
	}
	defer body.Close()
	data, err := ioutil.ReadAll(body)
	if err != nil {
		return
	}
	// json is valid YAML, so we can safely convert, even if it is already json
	j, err := yaml.YAMLToJSON(data)
	if err != nil {
		return
	}
	subSchema = new(JsonSubSchema)
	err = json.Unmarshal(j, subSchema)
	if err != nil {
		return
	}
	subSchema.setSourceURL(sanitizeURL(URL))
	err = subSchema.postPopulate(job)
	return
}
示例#24
0
func (this *YAMLFile) Load() (map[string]string, error) {
	encodedYAML, err := ioutil.ReadFile(this.path)
	if err != nil {
		return nil, err
	}

	encodedJSON, err := yaml.YAMLToJSON(encodedYAML)
	if err != nil {
		return nil, err
	}

	decodedJSON := map[string]interface{}{}
	if err := json.Unmarshal(encodedJSON, &decodedJSON); err != nil {
		return nil, err
	}

	return FlattenJSON(decodedJSON, "")
}
示例#25
0
文件: swagson.go 项目: sfodje/swagson
// handleModel extracts data from the api:model markup
func handleModel(swagDoc *specs.SwagDoc, docs []string) error {
	var def = make(map[string]specs.SwagSchema)
	for _, doc := range docs {
		var model map[string]specs.SwagSchema
		y := []byte(doc)
		// for some reason yaml.Unmarshal throws error: "panic: reflect: reflect.Value.Set using unaddressable value"
		// with structs that have nested pointers
		// as a workaround, convert yaml string to json string and unmarshal
		j, err := yaml.YAMLToJSON(y)
		err = json.Unmarshal(j, &model)
		if err != nil {
			return err
		}
		for k, v := range model {
			def[k] = v
		}
	}
	swagDoc.Definitions = &def
	return nil
}
示例#26
0
func getInternalApplications(pathToYml string) []byte {
	//read YAML configuration to get registered applications and associated details (i.e. ports/dependencies)
	rawYaml, err := ioutil.ReadFile(pathToYml)

	if err != nil {
		fmt.Printf("Unable to read YML file at [%s].\n", pathToYml)
		os.Exit(1)
	}

	rawJson, err := yaml.YAMLToJSON(rawYaml)

	if err != nil {
		fmt.Printf("Unable to convert the following Yaml to Json: \n %v\n", rawYaml)
		os.Exit(1)
	}

	//convert json string to []byte so it can be unmarshalled
	bytes := []byte(string(rawJson))

	return bytes
}
示例#27
0
func main() {
	if len(os.Args) == 1 {
		fmt.Printf("Usage: yaml2json [file]\n")
		os.Exit(1)
	}

	file := os.Args[1]
	input, err := ioutil.ReadFile(file)
	if err != nil {
		fmt.Printf("Failed to read file: %v\n", err)
		os.Exit(1)
	}

	json, err := yaml.YAMLToJSON(input)
	if err != nil {
		fmt.Printf("%v\n", err)
		os.Exit(1)
	}

	fmt.Print(string(json))
}
func getConfiguration(w http.ResponseWriter, r *http.Request, handler string) *common.Configuration {
	b := io.LimitReader(r.Body, *maxLength*1024)
	y, err := ioutil.ReadAll(b)
	if err != nil {
		util.LogAndReturnError(handler, http.StatusBadRequest, err, w)
		return nil
	}

	// Reject the input if it exceeded the length limit,
	// since we may not have read all of it into the buffer.
	if _, err = b.Read(make([]byte, 0, 1)); err != io.EOF {
		e := fmt.Errorf("configuration exceeds maximum length of %d KB.", *maxLength)
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return nil
	}

	j, err := yaml.YAMLToJSON(y)
	if err != nil {
		e := errors.New(err.Error() + "\n" + string(y))
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return nil
	}

	c := &common.Configuration{}
	if err := json.Unmarshal(j, c); err != nil {
		e := errors.New(err.Error() + "\n" + string(j))
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return nil
	}

	if len(c.Resources) < 1 {
		e := fmt.Errorf("configuration is empty")
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return nil
	}

	return c
}
func getTemplate(w http.ResponseWriter, r *http.Request, handler string) *manager.Template {
	util.LogHandlerEntry(handler, r)
	b := io.LimitReader(r.Body, *maxLength*1024)
	y, err := ioutil.ReadAll(b)
	if err != nil {
		util.LogAndReturnError(handler, http.StatusBadRequest, err, w)
		return nil
	}

	// Reject the input if it exceeded the length limit,
	// since we may not have read all of it into the buffer.
	if _, err = b.Read(make([]byte, 0, 1)); err != io.EOF {
		e := fmt.Errorf("template exceeds maximum length of %d KB", *maxLength)
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return nil
	}

	if err := r.Body.Close(); err != nil {
		util.LogAndReturnError(handler, http.StatusInternalServerError, err, w)
		return nil
	}

	j, err := yaml.YAMLToJSON(y)
	if err != nil {
		e := fmt.Errorf("%v\n%v", err, string(y))
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return nil
	}

	t := &manager.Template{}
	if err := json.Unmarshal(j, t); err != nil {
		e := fmt.Errorf("%v\n%v", err, string(j))
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return nil
	}

	return t
}
示例#30
0
func generateFunctions(ymlFile string) []byte {
	data, err := ioutil.ReadFile(ymlFile)
	if err != nil {
		log.Fatalf("ERROR: Problem reading from file '%v' - %s", ymlFile, err)
	}
	// json is valid YAML, so we can safely convert, even if it is already json
	rawJSON, err := yaml.YAMLToJSON(data)
	if err != nil {
		log.Fatalf("ERROR: Problem converting file '%v' to json format - %s", ymlFile, err)
	}
	rawJSON, err = jsontest.FormatJson(rawJSON)
	if err != nil {
		log.Fatalf("ERROR: Problem pretty printing json in '%v' - %s", ymlFile, err)
	}

	// the following strings.Replace function call safely escapes backticks (`) in rawJSON
	escapedJSON := "`" + strings.Replace(text.Indent(fmt.Sprintf("%v", string(rawJSON)), ""), "`", "` + \"`\" + `", -1) + "`"

	response := `
// Returns json schema for the payload part of the task definition. Please
// note we use a go string and do not load an external file, since we want this
// to be *part of the compiled executable*. If this sat in another file that
// was loaded at runtime, it would not be burned into the build, which would be
// bad for the following two reasons:
//  1) we could no longer distribute a single binary file that didn't require
//     installation/extraction
//  2) the payload schema is specific to the version of the code, therefore
//     should be versioned directly with the code and *frozen on build*.
//
// Run ` + "`generic-worker show-payload-schema`" + ` to output this schema to standard
// out.
func taskPayloadSchema() string {
    return ` + escapedJSON + `
}`
	return []byte(response)
}