pkg/yqlib/doc/usage/hcl.md
Encode and decode to and from HashiCorp Configuration Language (HCL).
HCL is commonly used in HashiCorp tools like Terraform for configuration files. The yq HCL encoder and decoder support:
Given a sample.hcl file of:
io_mode = "async"
then
yq -oy sample.hcl
will output
io_mode: "async"
Given a sample.hcl file of:
service "cat" {
process "main" {
command = ["/usr/local/bin/awesome-app", "server"]
}
process "management" {
command = ["/usr/local/bin/awesome-app", "management"]
}
}
then
yq sample.hcl
will output
service "cat" {
process "main" {
command = ["/usr/local/bin/awesome-app", "server"]
}
process "management" {
command = ["/usr/local/bin/awesome-app", "management"]
}
}
Given a sample.hcl file of:
service "cat" {
process "main" {
command = ["/usr/local/bin/awesome-app", "server"]
}
process "management" {
command = ["/usr/local/bin/awesome-app", "management"]
}
}
then
yq '.service.cat.process.main.command += "meow"' sample.hcl
will output
service "cat" {
process "main" {
command = ["/usr/local/bin/awesome-app", "server", "meow"]
}
process "management" {
command = ["/usr/local/bin/awesome-app", "management"]
}
}
Given a sample.hcl file of:
service "cat" {
process "main" {
command = ["/usr/local/bin/awesome-app", "server"]
}
process "management" {
command = ["/usr/local/bin/awesome-app", "management"]
}
}
then
yq -oy sample.hcl
will output
service:
cat:
process:
main:
command:
- "/usr/local/bin/awesome-app"
- "server"
management:
command:
- "/usr/local/bin/awesome-app"
- "management"
Given a sample.hcl file of:
# Configuration
port = 8080 # server port
then
yq -oy sample.hcl
will output
# Configuration
port: 8080 # server port
Given a sample.hcl file of:
# Configuration
port = 8080
then
yq sample.hcl
will output
# Configuration
port = 8080
Given a sample.hcl file of:
# Arithmetic with literals and application-provided variables
sum = 1 + addend
# String interpolation and templates
message = "Hello, ${name}!"
# Application-provided functions
shouty_message = upper(message)
then
yq sample.hcl
will output
# Arithmetic with literals and application-provided variables
sum = 1 + addend
# String interpolation and templates
message = "Hello, ${name}!"
# Application-provided functions
shouty_message = upper(message)
Given a sample.hcl file of:
resource "aws_instance" "web" {
ami = "ami-12345"
}
resource "aws_instance" "db" {
ami = "ami-67890"
}
then
yq sample.hcl
will output
resource "aws_instance" "web" {
ami = "ami-12345"
}
resource "aws_instance" "db" {
ami = "ami-67890"
}