fatih / hclfmt

Format and prettify HCL files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prefer one line variable declarations

mpeter opened this issue · comments

Hello! I really like this project and the parser you wrote. It's been helpful for me to understand Go, being only exposed to it through Terraform code base up until now. I have one issue with how you're currently doing the formatting, and I was wondering if this was configurable or not. Here's the issue:

The standard convention when defining terraform variables is to do it similar to this:

# empty variable -- always written on 1 line
variable simple_string_empty { }

# variable with default string parameter -- always written on 1 line
variable simple_string_default { default = "value" }

# example complex variable, written on multiple lines (Maps and Lists)
variable complex_map { 
    default = {
        key1 = "val1"
        key2 = "val2"
    }
}

My question is about the simple variables. When writing a terraform config, it's common to have several, or even 10-20+ of them in a single file. Since these are basically "boiler plate", we want them as terse as possible. The formatter currently outputs these simple variables like this:

# empty variable -- always written on 1 line
variable simple_string_empty {
}

# variable with default string parameter -- always written on 1 line
variable simple_string_default { 
    default = "/dev/sdh" 
}

For empty variables, this increase the LOC in a file 2x, and for simple variables with a default, it increases it by 3x. I spent some time looking at the parser, to see if there were some configurations options that might be set, but didn't have much luck. Is there any way your parser could generically differentiate between simple and complex types, and print them differently?

I don't even necessarily need you to implement this feature if this is not a priority, but if you could confirm that this should be possible, and pick out the relevant section of the codebase, I'd be happy to take a crack at it myself.

TL;DR; -- Love the formatter mostly, but it's blowing up my variable definitions and making it hard to read. Can you fix, or show me where I can fix?

Hi @mpeter. Thanks for the feedback. hclfmt uses the hcl/printer package to format the file. So all the logic is there. You can find it in the official hcl repo: https://github.com/hashicorp/hcl

So if you want to have this functionality, I suggest you to open the PR there. Even though I wrote the whole HCL parser and printer in Go myself, I've transfered all my code to the HCL repo, and there are more people to tackle this for you :). Thanks!