Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new fmtcmd package (linting) #80

Merged
merged 1 commit into from
Jan 30, 2016
Merged

Add new fmtcmd package (linting) #80

merged 1 commit into from
Jan 30, 2016

Conversation

dcarley
Copy link
Contributor

@dcarley dcarley commented Jan 6, 2016

This uses the printer package to normalise HCL files. It's inspired by
github.com/fatih/hclfmt - but it differs in that it's intended to be more
generic, re-usable, and follow the semantics of gofmt or go fmt.

I intend to utilise this in Terraform by implementing terraform fmt
sub-command which will normalise all files in the current working directory
(like go fmt ./..) or contents over STDIN (with terraform fmt - that
could be used by editor plugins). So that Terraform users can benefit from
linting without installing another package/binary.

I hope that by placing most of the logic in the HCL package it should be
easy to implement sub-commands in other projects that also use HCL.

Some notes about the implementation:

  • the significant difference from gofmt is that STDIN/STDOUT are passed in
    and errors aren't logged/written directly to STDERR, which gives consumers
    (e.g. Terraform) the ability to set appropriate exit codes
  • I chose to use inline fixtures instead of files because there were a
    number of times where I needed to reference the contents and group them
    together with diff output
  • it seemed simplest to construct the expected outputs by looping over the
    relevant fixtures and building up a string/byte slice, hope it isn't too
    confusing to read
  • the test failure reporting is kind of rough because the outputs are so
    large, but I didn't want to add another diff function
  • I chose to have a separate test for sub-directories rather than making it
    the default in the fixtures so that I didn't need to add additional logic
    to the fixture rendering
  • the fixtures are sorted by filename before any of the tests runs so that
    they match the order that they are read from disk by filepath.Walk()

This uses the `printer` package to normalise HCL files. It's inspired by
github.com/fatih/hclfmt - but it differs in that it's intended to be more
generic, re-usable, and follow the semantics of `gofmt` or `go fmt`.

I intend to utilise this in Terraform by implementing `terraform fmt`
sub-command which will normalise all files in the current working directory
(like `go fmt ./..`) or contents over STDIN (with `terraform fmt -` that
could be used by editor plugins). So that Terraform users can benefit from
linting without installing another package/binary.

I hope that by placing most of the logic in the HCL package it should be
easy to implement sub-commands in other projects that also use HCL.

Some notes about the implementation:

- the significant difference from `gofmt` is that STDIN/STDOUT are passed in
  and errors aren't logged/written directly to STDERR, which gives consumers
  (e.g. Terraform) the ability to set appropriate exit codes
- I chose to use inline fixtures instead of files because there were a
  number of times where I needed to reference the contents and group them
  together with diff output
- it seemed simplest to construct the expected outputs by looping over the
  relevant fixtures and building up a string/byte slice, hope it isn't too
  confusing to read
- the test failure reporting is kind of rough because the outputs are so
  large, but I didn't want to add another diff function
- I chose to have a separate test for sub-directories rather than making it
  the default in the fixtures so that I didn't need to add additional logic
  to the fixture rendering
- the fixtures are sorted by filename before any of the tests runs so that
  they match the order that they are read from disk by `filepath.Walk()`
@dcarley
Copy link
Contributor Author

dcarley commented Jan 26, 2016

Any thoughts on this?

One thing that occurred to me, while implementing tests for the Terraform part, was whether STDOUT was the most appropriate thing to pass around? It looks like it might result in copying lines/strings to c.Ui.Output().

@mitchellh
Copy link
Contributor

Looks great! I think the prototype for this function is correct and we should look at how to implement this in Terraform and worry about the UI there. The writer can always be a bytes buffer or IO pipe we handle separately there.

@mitchellh
Copy link
Contributor

Sorry for the late response I totally missed this :)

mitchellh added a commit that referenced this pull request Jan 30, 2016
Add new fmtcmd package (linting)
@mitchellh mitchellh merged commit e96d231 into hashicorp:master Jan 30, 2016
@dcarley
Copy link
Contributor Author

dcarley commented Jan 30, 2016

Thanks! Terraform PR to follow soon..

@echohack
Copy link

This isn't terribly important, but can you comment on why vertical whitespacing is used? @dcarley

This is somewhat surprising to me because most languages I have seen either remove such whitespacing in their linters (python, json, rust) or have no opinion about it (ruby, go).

screen shot 2016-06-27 at 11 53 54

screen shot 2016-06-27 at 11 54 25

While I am a fan of having an automated standard for everyone to lint by, I'm not really a fan of hitting spacebar a ton. Maybe we can change this?

mtekel added a commit to alphagov/paas-cf that referenced this pull request Nov 11, 2016
@dcarley has implemented new canonical formatting functionality, which has
been accepted to terraform (hashicorp/hcl#80). Use
the new command to check if any files need formatting.
mtekel added a commit to alphagov/paas-cf that referenced this pull request Nov 11, 2016
@dcarley has implemented new canonical formatting functionality, which has
been accepted to terraform (hashicorp/hcl#80). Use
the new command to check if any files need formatting.
mtekel added a commit to alphagov/paas-cf that referenced this pull request Nov 11, 2016
@dcarley has implemented new canonical formatting functionality, which has
been accepted to terraform (hashicorp/hcl#80). Use
the new command to check if any files need formatting.
mtekel added a commit to alphagov/paas-cf that referenced this pull request Nov 11, 2016
@dcarley has implemented new canonical formatting functionality, which has
been accepted to terraform (hashicorp/hcl#80). Use
the new command to check if any files need formatting.
mtekel added a commit to alphagov/paas-cf that referenced this pull request Nov 11, 2016
@dcarley has implemented new canonical formatting functionality, which has
been accepted to terraform (hashicorp/hcl#80). Use
the new command to check if any files need formatting.
mtekel added a commit to alphagov/paas-cf that referenced this pull request Nov 11, 2016
@dcarley has implemented new canonical formatting functionality, which has
been accepted to terraform (hashicorp/hcl#80). Use
the new command to check if any files need formatting.
dcarley pushed a commit to alphagov/paas-bootstrap that referenced this pull request Dec 16, 2016
@dcarley has implemented new canonical formatting functionality, which has
been accepted to terraform (hashicorp/hcl#80). Use
the new command to check if any files need formatting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants