y2j

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2022 License: Apache-2.0 Imports: 8 Imported by: 3

README

🌳 Go YAML to JSON Converter

GoDoc

Install

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

Standalone

go install github.com/rwxrob/y2j/cmd/y2j@latest

Composed

package cmds

import (
	Z "github.com/rwxrob/bonzai/z"
	"github.com/rwxrob/y2j"
)

var Cmd = &Z.Cmd{
	Name:     `cmds`,
	Commands: []*Z.Cmd{help.Cmd, y2j.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.

Design Considerations

  • Nothing but maps.

    It is extremely rare to encounter a YAML array in the wild. It is common, however, to have a map with a single array as it's value.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &Z.Cmd{

	Name:      `y2j`,
	Summary:   `convert basic YAML to JSON`,
	Usage:     `[help|<file>]`,
	Comp:      compfile.New(),
	Version:   `v0.5.0`,
	Copyright: `Copyright 2021 Robert S Muhlestein`,
	License:   `Apache-2.0`,
	Commands:  []*Z.Cmd{help.Cmd},

	Description: `
		The {{cmd .Name}} command converts YAML (including references and
		anchors) to compressed JSON (with a single training newline) using
		the popular Go yaml.v3 package and its special <yaml:",inline"> tag.
		Because of this only YAML maps are supported as a base type (no
		arrays). An array can easily be done as the value of a map key.

		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 {{cmd .Name}} 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(_ *Z.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
			}
		}

		buf, err = Convert(buf)
		if err != nil {
			return err
		}
		fmt.Println(string(buf))

		return nil
	},
}

Functions

func Convert added in v0.5.0

func Convert(buf []byte) ([]byte, error)

Convert converts YAML map data into JSON data. Only maps will work.

Types

This section is empty.

Directories

Path Synopsis
cmd
y2j command

Jump to

Keyboard shortcuts

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