jsoncmd

package
v0.0.0-...-9c55465 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JSONFormatCmd = &gcli.Command{
	Name:    "format",
	Aliases: []string{"fmt", "pretty"},
	Desc:    "pretty format input JSON(5) contents",
	Config: func(c *gcli.Command) {
		c.BoolOpt2(&jfOpts.json5, "json5, 5", "mark input contents is json5 format")

		c.AddArg("json", "input JSON(5) contents for format, allow: @c, @in")
	},
	Func: func(c *gcli.Command, _ []string) error {
		src, err := apputil.ReadSource(c.Arg("json").String())
		if err != nil {
			return err
		}

		return outputFmtJSON(src)
	},
}

JSONFormatCmd instance

View Source
var JSONQueryCmd = &gcli.Command{
	Name:    "view",
	Aliases: []string{"get", "cat", "query"},
	Desc:    "format and query value from JSON(5) contents",
	Config: func(c *gcli.Command) {
		c.BoolOpt2(&jvOpts.json5, "json5, 5", "mark input contents is json5 format")
		c.StrOpt2(&jvOpts.query, "query, path, q, p", "The path for query sub value")
		c.BoolOpt2(&jvOpts.compressed, "compressed, c", "compressed output, not format")

		c.AddArg("json", "input JSON contents for format")
		c.AddArg("path", "The path for query sub value, same of --path")
	},
	Func: func(c *gcli.Command, _ []string) error {
		src, err := apputil.ReadSource(c.Arg("json").String())
		if err != nil {
			return err
		}

		if !c.Arg("path").IsEmpty() {
			jvOpts.query = c.Arg("path").String()
		}

		if jvOpts.query == "" {
			return outputFmtJSON(src)
		}

		var mp maputil.Data
		if !jvOpts.json5 {

			if err = json.Unmarshal([]byte(src), &mp); err != nil {
				return err
			}
		} else {
			if err = json5.Unmarshal([]byte(src), &mp); err != nil {
				return err
			}
		}

		value := mp.Get(jvOpts.query)
		s, err := strutil.ToStringWith(value)
		if err == nil {
			stdio.Writeln(s)
			return nil
		}

		bs, err1 := json.Marshal(value)
		if err1 != nil {
			return err1
		}

		if jvOpts.compressed {
			stdio.WritelnBytes(bs)
			return nil
		}

		// format output
		var buf bytes.Buffer
		err = json.Indent(&buf, bs, "", "    ")
		if err != nil {
			return err
		}

		stdio.WriteBytes(buf.Bytes())
		return nil
	},
}

JSONQueryCmd instance

View Source
var JSONToYAMLCmd = &gcli.Command{
	Name:    "yaml",
	Aliases: []string{"to-yaml", "to-yml"},
	Desc:    "convert create table SQL to markdown table",
	Config: func(c *gcli.Command) {
		c.AddArg("json", "input JSON contents for convert")
	},
	Func: func(c *gcli.Command, _ []string) error {
		src, err := apputil.ReadSource(c.Arg("json").String())
		if err != nil {
			return err
		}

		bs, err := yaml.JSONToYAML([]byte(src))
		if err != nil {
			return err
		}

		fmt.Println(string(bs))
		return nil
	},
}

JSONToYAMLCmd instance

View Source
var JSONToolCmd = &gcli.Command{
	Name: "json",
	Desc: "json format, convert tool commands",
	Subs: []*gcli.Command{
		JSONQueryCmd,
		JSONToYAMLCmd,
		JSONFormatCmd,
	},
}

JSONToolCmd instance

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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