• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.circleci/H21-Jul-2021-181164

cmd/H21-Jul-2021-3,4962,870

ext/H21-Jul-2021-4,7743,748

gohcl/H21-Jul-2021-1,9021,577

guide/H03-May-2022-1,9171,386

hcldec/H21-Jul-2021-3,4742,788

hcled/H21-Jul-2021-4026

hclparse/H21-Jul-2021-13669

hclsimple/H21-Jul-2021-195127

hclsyntax/H21-Jul-2021-26,66223,732

hcltest/H21-Jul-2021-764667

hclwrite/H21-Jul-2021-8,2187,086

integrationtest/H21-Jul-2021-645538

json/H21-Jul-2021-5,9675,301

specsuite/H21-Jul-2021-882764

CHANGELOG.mdH A D21-Jul-202111.9 KiB173104

LICENSEH A D21-Jul-202115.6 KiB354256

README.mdH A D21-Jul-20217.2 KiB220171

diagnostic.goH A D21-Jul-20214.7 KiB14461

diagnostic_text.goH A D21-Jul-20218.3 KiB312239

diagnostic_text_test.goH A D21-Jul-20214.3 KiB236210

didyoumean.goH A D21-Jul-2021807 2513

doc.goH A D21-Jul-20211.2 KiB351

eval_context.goH A D21-Jul-2021644 2616

expr_call.goH A D21-Jul-20211.4 KiB4729

expr_list.goH A D21-Jul-20211.2 KiB3823

expr_map.goH A D21-Jul-20211.3 KiB4527

expr_unwrap.goH A D21-Jul-20212.4 KiB6932

go.modH A D21-Jul-2021863 2421

go.sumH A D21-Jul-20215.8 KiB6362

merged.goH A D21-Jul-20216.3 KiB227157

merged_test.goH A D21-Jul-202112.1 KiB684654

ops.goH A D21-Jul-202113.7 KiB433312

ops_test.goH A D21-Jul-202111 KiB404383

pos.goH A D21-Jul-20217.9 KiB276158

pos_scanner.goH A D21-Jul-20214.6 KiB15383

pos_scanner_test.goH A D21-Jul-20214 KiB194189

pos_test.goH A D21-Jul-202111.8 KiB468452

schema.goH A D21-Jul-2021538 2213

spec.mdH A D21-Jul-202131.8 KiB692517

static_expr.goH A D21-Jul-2021955 4123

structure.goH A D21-Jul-20215.2 KiB15262

structure_at_pos.goH A D21-Jul-20214.2 KiB11851

traversal.goH A D21-Jul-20217.9 KiB294195

traversal_for_expr.goH A D21-Jul-20214.6 KiB12552

traversal_for_expr_test.goH A D21-Jul-20214.1 KiB219202

README.md

1# HCL
2
3HCL is a toolkit for creating structured configuration languages that are
4both human- and machine-friendly, for use with command-line tools.
5Although intended to be generally useful, it is primarily targeted
6towards devops tools, servers, etc.
7
8> **NOTE:** This is major version 2 of HCL, whose Go API is incompatible with
9> major version 1. Both versions are available for selection in Go Modules
10> projects. HCL 2 _cannot_ be imported from Go projects that are not using Go Modules. For more information, see
11> [our version selection guide](https://github.com/hashicorp/hcl/wiki/Version-Selection).
12
13HCL has both a _native syntax_, intended to be pleasant to read and write for
14humans, and a JSON-based variant that is easier for machines to generate
15and parse.
16
17The HCL native syntax is inspired by [libucl](https://github.com/vstakhov/libucl),
18[nginx configuration](http://nginx.org/en/docs/beginners_guide.html#conf_structure),
19and others.
20
21It includes an expression syntax that allows basic inline computation and,
22with support from the calling application, use of variables and functions
23for more dynamic configuration languages.
24
25HCL provides a set of constructs that can be used by a calling application to
26construct a configuration language. The application defines which attribute
27names and nested block types are expected, and HCL parses the configuration
28file, verifies that it conforms to the expected structure, and returns
29high-level objects that the application can use for further processing.
30
31```go
32package main
33
34import (
35	"log"
36
37	"github.com/hashicorp/hcl/v2/hclsimple"
38)
39
40type Config struct {
41	IOMode  string        `hcl:"io_mode"`
42	Service ServiceConfig `hcl:"service,block"`
43}
44
45type ServiceConfig struct {
46	Protocol   string          `hcl:"protocol,label"`
47	Type       string          `hcl:"type,label"`
48	ListenAddr string          `hcl:"listen_addr"`
49	Processes  []ProcessConfig `hcl:"process,block"`
50}
51
52type ProcessConfig struct {
53	Type    string   `hcl:"type,label"`
54	Command []string `hcl:"command"`
55}
56
57func main() {
58	var config Config
59	err := hclsimple.DecodeFile("config.hcl", nil, &config)
60	if err != nil {
61		log.Fatalf("Failed to load configuration: %s", err)
62	}
63	log.Printf("Configuration is %#v", config)
64}
65```
66
67A lower-level API is available for applications that need more control over
68the parsing, decoding, and evaluation of configuration. For more information,
69see [the package documentation](https://pkg.go.dev/github.com/hashicorp/hcl/v2).
70
71## Why?
72
73Newcomers to HCL often ask: why not JSON, YAML, etc?
74
75Whereas JSON and YAML are formats for serializing data structures, HCL is
76a syntax and API specifically designed for building structured configuration
77formats.
78
79HCL attempts to strike a compromise between generic serialization formats
80such as JSON and configuration formats built around full programming languages
81such as Ruby. HCL syntax is designed to be easily read and written by humans,
82and allows _declarative_ logic to permit its use in more complex applications.
83
84HCL is intended as a base syntax for configuration formats built
85around key-value pairs and hierarchical blocks whose structure is well-defined
86by the calling application, and this definition of the configuration structure
87allows for better error messages and more convenient definition within the
88calling application.
89
90It can't be denied that JSON is very convenient as a _lingua franca_
91for interoperability between different pieces of software. Because of this,
92HCL defines a common configuration model that can be parsed from either its
93native syntax or from a well-defined equivalent JSON structure. This allows
94configuration to be provided as a mixture of human-authored configuration
95files in the native syntax and machine-generated files in JSON.
96
97## Information Model and Syntax
98
99HCL is built around two primary concepts: _attributes_ and _blocks_. In
100native syntax, a configuration file for a hypothetical application might look
101something like this:
102
103```hcl
104io_mode = "async"
105
106service "http" "web_proxy" {
107  listen_addr = "127.0.0.1:8080"
108
109  process "main" {
110    command = ["/usr/local/bin/awesome-app", "server"]
111  }
112
113  process "mgmt" {
114    command = ["/usr/local/bin/awesome-app", "mgmt"]
115  }
116}
117```
118
119The JSON equivalent of this configuration is the following:
120
121```json
122{
123  "io_mode": "async",
124  "service": {
125    "http": {
126      "web_proxy": {
127        "listen_addr": "127.0.0.1:8080",
128        "process": {
129          "main": {
130            "command": ["/usr/local/bin/awesome-app", "server"]
131          },
132          "mgmt": {
133            "command": ["/usr/local/bin/awesome-app", "mgmt"]
134          },
135        }
136      }
137    }
138  }
139}
140```
141
142Regardless of which syntax is used, the API within the calling application
143is the same. It can either work directly with the low-level attributes and
144blocks, for more advanced use-cases, or it can use one of the _decoder_
145packages to declaratively extract into either Go structs or dynamic value
146structures.
147
148Attribute values can be expressions as well as just literal values:
149
150```hcl
151# Arithmetic with literals and application-provided variables
152sum = 1 + addend
153
154# String interpolation and templates
155message = "Hello, ${name}!"
156
157# Application-provided functions
158shouty_message = upper(message)
159```
160
161Although JSON syntax doesn't permit direct use of expressions, the interpolation
162syntax allows use of arbitrary expressions within JSON strings:
163
164```json
165{
166  "sum": "${1 + addend}",
167  "message": "Hello, ${name}!",
168  "shouty_message": "${upper(message)}"
169}
170```
171
172For more information, see the detailed specifications:
173
174* [Syntax-agnostic Information Model](spec.md)
175* [HCL Native Syntax](hclsyntax/spec.md)
176* [JSON Representation](json/spec.md)
177
178## Changes in 2.0
179
180Version 2.0 of HCL combines the features of HCL 1.0 with those of the
181interpolation language HIL to produce a single configuration language that
182supports arbitrary expressions.
183
184This new version has a completely new parser and Go API, with no direct
185migration path. Although the syntax is similar, the implementation takes some
186very different approaches to improve on some "rough edges" that existed with
187the original implementation and to allow for more robust error handling.
188
189It's possible to import both HCL 1 and HCL 2 into the same program using Go's
190_semantic import versioning_ mechanism:
191
192```go
193import (
194    hcl1 "github.com/hashicorp/hcl"
195    hcl2 "github.com/hashicorp/hcl/v2"
196)
197```
198
199## Acknowledgements
200
201HCL was heavily inspired by [libucl](https://github.com/vstakhov/libucl),
202by [Vsevolod Stakhov](https://github.com/vstakhov).
203
204HCL and HIL originate in [HashiCorp Terraform](https://terraform.io/),
205with the original parsers for each written by
206[Mitchell Hashimoto](https://github.com/mitchellh).
207
208The original HCL parser was ported to pure Go (from yacc) by
209[Fatih Arslan](https://github.com/fatih). The structure-related portions of
210the new native syntax parser build on that work.
211
212The original HIL parser was ported to pure Go (from yacc) by
213[Martin Atkins](https://github.com/apparentlymart). The expression-related
214portions of the new native syntax parser build on that work.
215
216HCL 2, which merged the original HCL and HIL languages into this single new
217language, builds on design and prototyping work by
218[Martin Atkins](https://github.com/apparentlymart) in
219[zcl](https://github.com/zclconf/go-zcl).
220