refmt

command module
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2022 License: AGPL-3.0 Imports: 19 Imported by: 0

README

refmt GoDoc Build Status Build status

Reformat HCL ⇄ JSON and HCL ⇄ YAML.

install
$ go get github.com/rjeczalik/refmt
usage
usage:

	refmt [-t type] INPUT_FILE|"-" OUTPUT_FILE|"-"

Converts from one encoding to another. Supported formats (and their file extensions):

	- HCL (.hcl or .tf)
	- JSON (.json)
	- YAML (.yaml or .yml)

If INPUT_FILE's extension is not recognized or INPUT_FILE is "-" (stdin),
refmt will try to guess input format.

If OUTPUT_FILE is "-" (stdout), destination format type is required to be
passed with -t flag.

	refmt [-t type] merge ORIGINAL_FILE|"-" MIXIN_FILE|"-" OUTPUT_FILE|"-"

Merges the object defined in ORIGINAL_FILE with the object from MIXIN_FILE, writing
the resulting object to the OUTPUT_FILE.

The ORIGINAL_FILE, MIXIN_FILE and OUTPUT_FILE can have different encodings.

If ORIGINAL_FILE's extension is not recognized or ORIGINAL_FILE is "-" (stdin),
refmt will try to guess original format.

If ORIGINAL_FILE does not exist or is empty, refmt is going to use empty
object instead.

If MIXIN_FILE's extension is not recognized or MIXIN_FILE is "-" (stdin),
refmt will try to guess mixin format.

If OUTPUT_FILE is "-" (stdout), destination format type is required to be
passed with -t flag.
docker usage
# build the refmt image
docker build -t refmt .

# convert test.yml to hcl
cat test.yml | docker run -i --rm refmt -t hcl - -
examples
$ refmt -t yaml main.yaml -
provider:
  aws:
    access_key: ${var.aws_access_key}
    secret_key: ${var.aws_secret_key}
resource:
  aws_instance:
    aws-instance:
      instance_type: t2.nano
      user_data: echo "hello world!" >> /tmp/helloworld.txt
$ refmt main.yaml main.json
{
        "provider": {
                "aws": {
                        "access_key": "${var.aws_access_key}",
                        "secret_key": "${var.aws_secret_key}"
                }
        },
        "resource": {
                "aws_instance": {
                        "aws-instance": {
                                "instance_type": "t2.nano",
                                "user_data": "echo \"hello world!\" >> /tmp/helloworld.txt"
                        }
                }
        }
}
$ refmt main.json main.hcl
provider "aws" {
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
}

resource "aws_instance" "aws-instance" {
  instance_type = "t2.nano"
  user_data = "echo \"hello world!\" >> /tmp/helloworld.txt"
}
pretty reformat in-place
$ refmt main.tf.json main.tf.json
merge configurations
$ cat ~/.kube/config
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /Volumes/rjk.io/.kube/ca.pem
    server: https://178.0.20.1
  name: default-cluster
contexts:
- context:
    cluster: default-cluster
    user: default-admin
  name: default-system
current-context: default-system
kind: Config
preferences: {}
users:
- name: default-admin
  user:
    client-certificate: /Volumes/rjk.io/.kube/admin.pem
    client-key: /Volumes/rjk.io/.kube/admin-key.pem
$ cat >>another-cluster <<EOF
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /Volumes/rjk.io/.kube/another-ca.pem
    server: https://10.0.0.1
  name: another-cluster
contexts:
- context:
    cluster: another-cluster
    user: another-admin
  name: another-system
current-context: another-system
kind: Config
preferences: {}
users:
- name: another-admin
  user:
    client-certificate: /Volumes/rjk.io/.kube/another-admin.pem
    client-key: /Volumes/rjk.io/.kube/another-admin-key.pem
EOF
$ refmt merge -t yaml ~/.kube/config ./another-cluster -
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /Volumes/rjk.io/.kube/ca.pem
    server: https://178.0.20.1
  name: default-cluster
- cluster:
    certificate-authority: /Volumes/rjk.io/.kube/another-ca.pem
    server: https://10.0.0.1
  name: another-cluster
contexts:
- context:
    cluster: default-cluster
    user: default-admin
  name: default-system
- context:
    cluster: another-cluster
    user: another-admin
  name: another-system
current-context: another-system
kind: Config
preferences: {}
users:
- name: default-admin
  user:
    client-certificate: /Volumes/rjk.io/.kube/admin.pem
    client-key: /Volumes/rjk.io/.kube/admin-key.pem
- name: another-admin
  user:
    client-certificate: /Volumes/rjk.io/.kube/another-admin.pem
    client-key: /Volumes/rjk.io/.kube/another-admin-key.pem
todo
  • inline docs
  • fix hcl marshaling:
    • fix excessive newlines
    • fix excessive quotes

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package object was forked from: https://github.com/koding/koding/blob/4e0b75243e60685f3271838d2eb08bd3801fae48/go/src/koding/kites/kloud/utils/object/fix.go
Package object was forked from: https://github.com/koding/koding/blob/4e0b75243e60685f3271838d2eb08bd3801fae48/go/src/koding/kites/kloud/utils/object/fix.go

Jump to

Keyboard shortcuts

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