bicep icon indicating copy to clipboard operation
bicep copied to clipboard

Provide a CLI command for formatting bicep files (i.e. `bicep format`)

Open JanuszNowak opened this issue 2 years ago • 10 comments

Bicep files can be formatted in multiple way. Like

param instLocation array = [ 'westeurope' ]`

or

param instLocation array = [
  'euw'
]

Missing option like in tf for formatting file, having common conversion. Like az bicep format or az bicep fmt .

JanuszNowak avatar Jul 16 '22 20:07 JanuszNowak

We have formatting support in the VS Code extension which you can find in the command palette by searching "format". Is that enough for what you are looking for, or specifically do you need a way of formatting via the CLI?

alex-frankel avatar Jul 18 '22 23:07 alex-frankel

Hi @alex-frankel, I know about VS Code formatting. Looking more for formatting via CLI.

JanuszNowak avatar Jul 19 '22 06:07 JanuszNowak

Git Hooks + Autoformat on the way would be awesome

coolhome avatar Jul 20 '22 15:07 coolhome

@coolhome, @alex-frankel I already created for pre-commit (one of Git Hook) implementation for azure bicep that is calling az bicep build all or modified bicep files before making commit https://github.com/Azure4DevOps/check-azure-bicep. So you can in automatic way commit compiling bicep.

JanuszNowak avatar Jul 20 '22 15:07 JanuszNowak

I would also like to see a bicep cli format command. Although I realise there's formatting support in VS Code, having a cli command can help enforce consistency of style.

rwaal avatar Sep 30 '22 08:09 rwaal

This would actually be very straightforward to implement - I'd be happy to share pointers if anyone in the community is interested in picking it up!

anthony-c-martin avatar Sep 30 '22 13:09 anthony-c-martin

We would be happy to implement that in our PowerShell module (https://github.com/PSBicep/PSBicep)

SimonWahlin avatar Sep 30 '22 13:09 SimonWahlin

This would actually be very straightforward to implement - I'd be happy to share pointers if anyone in the community is interested in picking it up!

Or maybe you meant implement it in bicep? I would absolutely be interested in opening a PR for that.

SimonWahlin avatar Sep 30 '22 14:09 SimonWahlin

Or maybe you meant implement it in bicep? I would absolutely be interested in opening a PR for that.

Yeah that's what I was thinking!

For pointers:

  • Obtaining the ProgramSyntax for a file: https://github.com/Azure/bicep/blob/ae66808697d2f668ab917b0e602c0c330dee5563/src/Bicep.Core.UnitTests/Utils/ParserHelper.cs#L14-L19
  • This is pretty much all the logic you need once you've obtained the ProgramSyntax: https://github.com/Azure/bicep/blob/cf2a3ad72dfb433f49aa776d057354e7de318ecb/src/Bicep.LangServer/Handlers/BicepDocumentFormattingHandler.cs#L45-L47
  • An interesting question is going to be where we obtain the format options - my recommendation would probably be to use the standard defaults and provide CLI arguments for choosing different options.
  • To create a new CLI command, I would follow through the logic for BuildCommand and duplicate it: https://github.com/Azure/bicep/blob/ad66b70bf5abda2422ff800d9d63c4135e9bc1ce/src/Bicep.Cli/Commands/BuildCommand.cs#L13
  • The test framework for the CLI is pretty easy to extend - should be possible to duplicate this and modify the commands being passed to the CLI: https://github.com/Azure/bicep/blob/ad66b70bf5abda2422ff800d9d63c4135e9bc1ce/src/Bicep.Cli.IntegrationTests/BuildCommandTests.cs#L28-L29

anthony-c-martin avatar Sep 30 '22 14:09 anthony-c-martin

Cool! I'll give it a try

SimonWahlin avatar Sep 30 '22 14:09 SimonWahlin

It will be released in which version ?

JanuszNowak avatar Oct 09 '22 15:10 JanuszNowak