Code Monkey home page Code Monkey logo

yj's Introduction

yj

GoDoc Tests

Packaging status

Convert between YAML, TOML, JSON, and HCL.

Preserves map order.

Format versions:

  • YAML: v1.2 (gopkg.in/yaml.v3)
  • TOML: v1.0.0 (github.com/BurntSushi/toml)
  • JSON: RFC 7159 (encoding/json)
  • HCL: v1 (github.com/hashicorp/hcl)
$ yj -h
Usage: yj [-][ytjcrneikhv]

Convert between YAML, TOML, JSON, and HCL.
Preserves map order.

-x[x]  Convert using stdin. Valid options:
          -yj, -y = YAML to JSON (default)
          -yy     = YAML to YAML
          -yt     = YAML to TOML
          -yc     = YAML to HCL
          -tj, -t = TOML to JSON
          -ty     = TOML to YAML
          -tt     = TOML to TOML
          -tc     = TOML to HCL
          -jj     = JSON to JSON
          -jy, -r = JSON to YAML
          -jt     = JSON to TOML
          -jc     = JSON to HCL
          -cy     = HCL to YAML
          -ct     = HCL to TOML
          -cj, -c = HCL to JSON
          -cc     = HCL to HCL
-n     Do not covert inf, -inf, and NaN to/from strings (YAML or TOML only)
-e     Escape HTML (JSON out only)
-i     Indent output (JSON or TOML out only)
-k     Attempt to parse keys as objects or numeric types (YAML out only)
-h     Show this help message
-v     Show version

Installation

Homebrew

The yj CLI is available via Homebrew:

brew install yj

Manual

Binaries for macOS, Linux, and Windows are attached to each release.

Docker

yj is also available as a Docker image.

Go Package

Packages contained in this repo may also be used to convert all supported data formats to a normalized tree of ordered Go objects.

See godoc for details.

NOTE: The current Go API should not be considered stable.

yj's People

Contributors

ccakes avatar dependabot[bot] avatar ehaupt avatar jabrown85 avatar leahneukirchen avatar rplessl avatar sclevine avatar wesley-dean-flexion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yj's Issues

Brew Install Broken on OS X

==> Downloading https://github.com/sclevine/yj/releases/download/v5.1.0/yj-macos
curl: (22) The requested URL returned error: 404

Error: yj: Failed to download resource "yj"
Download failed: https://github.com/sclevine/yj/releases/download/v5.1.0/yj-macos

Release files for OS X appear to have an architecture suffix now? (-amd64 versus -arm64)

(perhaps in preparation to support arm64v9 aka M1 targets for OS X? In which case I ❤️ these contributors!!)

Docker support

Hi. I love this tool! Thank you very much for putting it together!!

I use Docker for many (most?) of my work and it's very handy for me to be able to use containerized builds of tools that I frequently use. Seeing as how yj didn't have a Dockerfile to build an image, I put one together and submitted it as a PR (#7).

If you're open to it, it would be super-swell if the image could be made available on dockerhub. Assuming the PR is accepted, dockerhub could be set to automatically build the image when a commit to the yj repo on github is pushed.

Also, if you want, I can document the build / run process in the README.md file.

Thanks again!!

Installation error

go get github.com/sclevine/yj
go: downloading github.com/sclevine/yj v0.0.0-20200815061347-554173e71934
go: downloading github.com/hashicorp/hcl v1.0.0
go: downloading github.com/pelletier/go-toml v1.8.0
go: downloading gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
# github.com/sclevine/yj/toml
../go/pkg/mod/github.com/sclevine/[email protected]/toml/encoder.go:63:7: tree.SetPositionPath undefined (type *toml.Tree has no field or method SetPositionPath)
../go/pkg/mod/github.com/sclevine/[email protected]/toml/encoder.go:79:6: tree.SetPositionPath undefined (type *toml.Tree has no field or method SetPositionPath)

[go version go1.16.3 linux/amd64]

YJ 5.1.0 not work on Windows 11

I downloaded the compiled Version of YJ.EXE for Windows and run it on my system. VJ starts but nothing happends, only black screen on my command screen. I allowed the exe for run and start something on my system.

Failed to parse escaped quote in TOML

I have a toml file:

["\"foo\""]
"\"bar\"" = "\"baz\""

yj -tj <a.toml failed with:

Error parsing TOML: (1, 2): invalid table array key: invalid bare key character: \

According to the TOML specification:

converting null value to empty string

When its converting yaml which have null as value to HCL replacing the value as empty string which is failing in terraform validation

klusterle:
  host: null
prometheusInteg:
  enabled: true

HCL:

"klusterle" = {
  "host" =
}

"prometheusInteg" = {
  "enabled" = true

Provide the detailed usage example in Readme.md

I am using the latest release to convert json to toml.

./yj -jj redis.json

But I am getting the error as follows
Error: invalid flags specified: d s . s o

Please provide an example to convert from json file to toml file

Terraform/OpenTofu Provider

This tool would be really cool if could be used from within terraform. Since it's written in Go, creating a terraform provider might be a possibility.

Converting from yaml to hcl to yaml is losing an array.

Hi! I'm running into an issue where converting from YAML to HCL is kind of destructive, with json the issue doesn't show up.

yaml='
route:
  receiver: webhook
  group_by:
    - alertname
    - datacenter
    - app
receivers:
  - name: webhook
    webhook_configs:
      - url: http://localhost:500
      - url: http://localhost:500'

echo -e '-----\nYAML\n-----'
echo "${yaml}"
echo -e '-----\nYAML to YAML\n-----'
echo "${yaml}" | yj yy
echo -e '-----\nYAML to JSON\n-----'
echo "${yaml}" | yj yj
echo -e '-----\nYAML to HCL\n-----'
echo "${yaml}" | yj yc
echo -e '-----\nYAML to HCL to YAML\n-----'
echo "${yaml}" | yj yc | yj yy
echo -e '-----\nYAML to HCL to YAML to HCL\n-----'
echo "${yaml}" | yj yc | yj yy | yj yc

echo -e '-----\nYAML o HCL to YAML vs YAML\n-----'
diff <(echo "${yaml}" | yj yc | yj yy) <(echo "${yaml}" | yj yy)

Results in this output:

-----
YAML
-----
route:
  receiver: webhook
  group_by:
    - alertname
    - datacenter
    - app
receivers:
  - name: webhook
    webhook_configs:
      - url: http://localhost:500
      - url: http://localhost:500
-----
YAML to YAML
-----
route:
  receiver: webhook
  group_by:
    - alertname
    - datacenter
    - app
receivers:
  - name: webhook
    webhook_configs:
      - url: http://localhost:500
      - url: http://localhost:500
-----
YAML to JSON
-----
{"route":{"receiver":"webhook","group_by":["alertname","datacenter","app"]},"receivers":[{"name":"webhook","webhook_configs":[{"url":"http://localhost:500"},{"url":"http://localhost:500"}]}]}
-----
YAML to HCL
-----
"route" = {
  "receiver" = "webhook"

  "group_by" = ["alertname", "datacenter", "app"]
}

"receivers" = {
  "name" = "webhook"

  "webhook_configs" = {
    "url" = "http://localhost:500"
  }

  "webhook_configs" = {
    "url" = "http://localhost:500"
  }
}-----
YAML to HCL to YAML
-----
route
-----
YAML to HCL to YAML to HCL
-----
Error writing HCL: Expected object, got unknown token: 1:1 STRING "route"
-----
YAML o HCL to YAML vs YAML
-----
1c1,11
< route
---
> route:
>   receiver: webhook
>   group_by:
>     - alertname
>     - datacenter
>     - app
> receivers:
>   - name: webhook
>     webhook_configs:
>       - url: http://localhost:500
>       - url: http://localhost:500

Feature request: add version command or option

This is a very small feature request:
It would be useful for our installation mechanism if yj could print its version, say, using yj --version or yj version. Ideally the output is very simple, like v4.0.0.

Thanks for considering this.

Arm linux version

The provided binary does not work on arm unfortunately. Could you add an Arm version to the releases?

Official Linux release

We'd like to use this in a concourse pipeline with a Docker image that doesn't have golang installed. Unfortunately, this is really hard to do, because the only official release binary is for OS X!

Conversion of HCL to/from JSON

Hi

Have one query regarding conversion of HCL to/from JSON . Conversion from HCL to JSON seems fine however while converting back the generated json to HCL it is not same . As seen in the example below docker_ports was single element of type list / array when converted to json whereas when it is converted back to HCL from from json , docker_ports is represented as two elements instead of two .Any idea why it was not exactly same as orignal HCL after conversion? Any way to get back same HCL format ?

Original tfvars file

image_id = "ami-123"

cluster_min_nodes = 2

cluster_decimal_nodes = 2.2

cluster_max_nodes = true

availability_zone_names = [
  "us-east-1a",
  "us-west-1c",
]

docker_ports = [{
  internal = 8300
  external = 8300
  protocol = "tcp"
},
{
  internal = 8301
  external = 8301
  protocol = "tcp"
}
] 

Conversion of HCL to json

$ ./yj-linux-amd64 -cj < testing.tfvars

{
  "image_id": "ami-123",
  "cluster_min_nodes": 2,
  "cluster_decimal_nodes": 2.2,
  "cluster_max_nodes": true,
  "availability_zone_names": [
    "us-east-1a",
    "us-west-1c"
  ],
  "docker_ports": [
    {
      "internal": 8300,
      "external": 8300,
      "protocol": "tcp"
    },
    {
      "internal": 8301,
      "external": 8301,
      "protocol": "tcp"
    }
  ]
}

JSON to HCL conversion

/yj-linux-amd64 -jc < testing.tfvars.json
"availability_zone_names" = ["us-east-1a", "us-west-1c"]

"cluster_min_nodes" = 2

"docker_ports" = {
  "external" = 8300

  "internal" = 8300

  "protocol" = "tcp"
}

"docker_ports" = {
  "external" = 8301

  "internal" = 8301

  "protocol" = "tcp"
}

"image_id" = "ami-123"

Thanks a lot !

Add Dhall as conversion language?

Hello,
i know this is kinda a big task, but your tool is very valuable. but i try to move to "secure" languages. Would it be possible that you support Dhall also? Integration guide is https://docs.dhall-lang.org/howtos/How-to-integrate-Dhall.html

Dhall is a computationally more secure language and i want to safe my configcode in dhall and output it into json/yaml/hcl...

On i side note i did a PR to binenv, a tool i use: devops-works/binenv#88 to add your tool to this package manager :)

Yaml with multiple documents

Trying to convert a .yaml file with multiple documents (seperated by ---) will only convert the first document and discard the rest.

It would be nice to get output for all of them, potentially in an array.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.