yaml2json

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

Convert YAML to JSON with Go 1.18+

GoDoc

Install

This command can be installed as a standalone program or composed into a Bonzai command tree.

Standalone

go install github.com/rwxrob/yaml2json/yaml2json@latest

Composed

package cmds

import (
	"github.com/rwxrob/bonzai"
	"github.com/rwxrob/yaml2json"
)

var Cmd = &bonzai.Cmd{
	Name:     `cmds`,
	Commands: []*bonzai.Cmd{help.Cmd, yaml2json.Cmd},
}

Embedded Documentation

All documentation (like manual pages) has been embedded into the source code of the application. See the source or run the program with help to access it.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &bonzai.Cmd{

	Name:      `yaml2json`,
	Summary:   `convert basic YAML to JSON`,
	Usage:     `[h|help|<file>]`,
	Version:   `v0.0.1`,
	Copyright: `Copyright 2021 Robert S Muhlestein`,
	License:   `Apache-2.0`,
	Commands:  []*bonzai.Cmd{help.Cmd},

	Description: `
		The yaml2json command converts YAML (including references and
		anchors) to compressed JSON (with a single training newline) using
		the popular Go yaml.v2 package and its special <yaml:",inline"> tag.
		If no filename is passed standard input is assumed. JSON encoding is
		done using rwxrob/json instead of the "standard" encoding/json which
		unnecessarily escapes HTML characters by default.

		If h or help are passed as the first argument this help is assumed
		(preventing the uncommon use of files named 'h' or 'help').

		The intent of yaml2json is to get YAML into a form quickly for using
		with the more powerful jq command line utility until an equally
		powerful yaml alternative is available.

		`,

	Call: func(_ *bonzai.Cmd, args ...string) error {
		var buf []byte
		var err error

		if len(args) == 0 {
			buf, err = io.ReadAll(os.Stdin)
			if err != nil {
				return err
			}
		} else {
			buf, err = os.ReadFile(args[0])
			if err != nil {
				return err
			}
		}

		s := struct {
			O map[string]any `yaml:",inline"`
		}{}
		if err := yaml.Unmarshal(buf, &s); err != nil {
			return err
		}
		buf, err = json.Marshal(s.O)
		if err != nil {
			return err
		}
		fmt.Println(string(buf))
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL