{"id":15089842,"url":"https://github.com/spirius/fc","last_synced_at":"2025-07-07T02:40:16.600Z","repository":{"id":57591787,"uuid":"107541972","full_name":"spirius/fc","owner":"spirius","description":"Format converter and template engine","archived":false,"fork":false,"pushed_at":"2020-02-27T22:01:00.000Z","size":2020,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T22:11:33.062Z","etag":null,"topics":["converter","encoder-decoder","hcl","jq","json","template-engine","toml","yaml"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spirius.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-19T12:16:40.000Z","updated_at":"2021-08-30T00:53:45.000Z","dependencies_parsed_at":"2022-09-26T19:43:24.253Z","dependency_job_id":null,"html_url":"https://github.com/spirius/fc","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"purl":"pkg:github/spirius/fc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spirius%2Ffc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spirius%2Ffc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spirius%2Ffc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spirius%2Ffc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spirius","download_url":"https://codeload.github.com/spirius/fc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spirius%2Ffc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264003265,"owners_count":23542594,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["converter","encoder-decoder","hcl","jq","json","template-engine","toml","yaml"],"created_at":"2024-09-25T09:01:55.829Z","updated_at":"2025-07-07T02:40:16.576Z","avatar_url":"https://github.com/spirius.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Format converter (FC) is a structured data format converter and template rendering engine.\n\n# Table of contents\n\n* [Overview](#overview)\n* [Download](#download)\n* [Usage](#usage)\n* [Templating](#templating)\n  * [Additional template functions](#additional-template-functions)\n    * [include](#include-path-input---string)\n    * [decode_*](#decode_-data---map)\n    * [encode_*](#encode_-data---map)\n    * [import](#import-url-opts---map)\n    * [metadata](#metadata---any)\n    * [jq](#jq-expr-data---any)\n* [Notes](#Notes)\n\n# Overview\n\nFC is a tool for converting structured data between different\nrepresentations, like JSON -\u003e YAML or HCL -\u003e TOML, etc. It also includes templating engine\nbased on [golang's built-in template language](https://golang.org/pkg/text/template/) and packaged with [sprig](http://masterminds.github.io/sprig/) extensions.\n\nIn essence gofc consists from decoder and encoder connected to each-other. It expects input data on **stdin** and outputs on **stdout**.\n\nSupported input formats are: **JSON**, **YAML**, **TOML** and **HCL**.\n\nSupported output formats are: **JSON**, **YAML**, **TOML**, **HCL** and **template**.\n\nHCL2 format have types of constructs - \n[arguments](https://www.terraform.io/docs/configuration/syntax.html#arguments) and [blocks](https://www.terraform.io/docs/configuration/syntax.html#blocks). In gofc `arguments` are used as primary input stream, so conversion from HCL -\u003e JSON will output only `arguments` and `blocks` will be ignored. `Blocks` are available in template engine via [metadata](#metadata---any) function.\n\n# Download\n\ngofc is provided as a static binary.\nYou can [download](https://github.com/spirius/fc/releases/latest)\nit from releases page.\n\nIf you want to compile it, make sure\nto install libjq and libonig devel packages.\n\n# Usage\n\n```\nUsage:\ngofc -i DECODER [ARG1, [...]] -o ENCODER [ARG1, [...]]\n```\n\n```\nOptions:\n -i            - input decoder\n -o            - output encoder\n -check-update - check if new version is available\n -self-update  - update to latest version\n\nSupported coders:\njson, j        - JSON decoder/encoder\nyaml, yml, y   - YANL decoder/encoder\nhcl, h         - HCL decoder/encoder, only Attributes are supported, blocks are ignored\ntoml, t        - TOML decoder/encoder\ntpl            - template encoder\n  ARG1          - template file path\n```\n\n**Convert from JSON to YAML**\n\n```bash\n$ echo '{\"key\":\"value\"}' | gofc -i j -o y\nkey: value\n```\n\n**Convert YAML file to JSON file**\n```bash\n$ gofc -i y -o j \u003c input.yml \u003e output.json\n```\n\n# Templating\n\nUsing gofc it is easy to render templates. You can use content with any of the supported input formats and pass it as a context object to templating engine.\n\nSuppose you want to render some complex configuration file (nginx config) based on your own human-readable configuration files. We can store the human-readable part as HCL in some config file:\n\n```hcl\n// input.hcl\npaths = {\n  \"/\" = {\n    upstream = \"us1\"\n  }\n  \"/static/\" = {\n    upstream = \"us2\"\n  }\n}\n```\n\n\n```\n# paths.conf.tpl\n{{ range $path, $conf := $.paths }}\nlocation {{ $path }} {\n  proxy_pass http://{{ $conf.upstream }};\n}\n{{ end }}\n```\n\nWe can use `input.hcl` to render `paths.conf.tpl` with following command\n\n```\ngofc \u003c input.hcl -i hcl -o tpl paths.conf.tpl \u003e paths.conf\n```\n\nAnd result will be rendered as \n\n```\nlocation / {\n  proxy_pass http://us1;\n}\n\nlocation /static/ {\n  proxy_pass http://us2;\n}\n```\n\n### Additional template functions\n\nIn addition to template [built-in functions](https://golang.org/pkg/text/template/#hdr-Functions) and [sprig extensions](http://masterminds.github.io/sprig), gofc adds following additional functions into templating engine.\n\n#### `include $path $input -\u003e string`\nRenders template specified by `$path` using `$input` as template context. Includes are done relative to the current template file.\n\n#### `decode_* $data -\u003e map`\nDecodes string `$data` into map. You can use any supported format instead of `*`.\n\nFor example: `decode_json $data`.\n\n#### `encode_* $data -\u003e map`\nEncodes `$data` into string. You can use any supported format instead of `*`.\n\nFor example `encode_yaml $obj`.\n\n#### `import $url $opts -\u003e map`\nReads and optionally decodes content under `$url`. Supported schemes are `file://` and `s3://`. If scheme is not specified, `file://` will be used.\n\n`$opts` is comma-separated string of options. Possible options are:\n\n * `raw` - return file content as string without parsing.\n\n * `metadata` - return file metadata as well. Changes return type to map:\n ```\n {\n   \"url\":\n   \"body\":\n\n   // if scheme is s3\n   \"key\":\n   \"bucket\":\n   \"version\":\n }\n ```\n * `nofail` - Enables `metadata` option and disabled failing of template generation in case of errors. If error occurred, it is stored in `error` field of the result.\n\n * `pattern` - treats path component of `$url` as [pattern](https://golang.org/pkg/path/filepath/#Match) and changes return type to list of files. If `nofail` or `metadata` options are enabled, they will be applied per-object in the result.\n\nExamples:\n\nRead a config file\n```\n$config := import config.yml\n```\n\nRead list of resources with file versions from S3 bucket.\n```\n$res := import s3://bucket/resources/*.yml \"metadata,pattern\"\n```\n\n#### `metadata -\u003e any`\n\nGet metadata of the input. Applicable only for HCL format. The blocks are returned as metadata.\n\n#### `jq $expr $data -\u003e any`\n\nRun [jq](https://stedolan.github.io/jq/) filter on `$data`. Note, that only first value from\nresult will be returned.\n\nExample\n```\nmetadata | jq \"reduce (.[] | select(.type == 'locals') | .attributes) as $i({};. + $i)\" | toJson\n```\n\nIf applied on\n```hcl\nlocals {\n  key1 = \"var1\"\n}\nlocals {\n  key2 = \"var2\"\n  key3 = \"var3\"\n}\n```\n\nWill produce following\n```json\n{\n  \"key1\": \"var1\",\n  \"key2\": \"var2\",\n  \"key3\": \"var3\"\n}\n```\n\n# Notes\n\n* HCL and TOML are not supporting primitive types or arrays as root element.\n\n```bash\necho 42 | gofc -i json -o yaml  # works as expected\necho 42 | gofc -i json -o hcl   # will fail\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspirius%2Ffc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspirius%2Ffc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspirius%2Ffc/lists"}