bicep
bicep copied to clipboard
Provide a CLI command for formatting bicep files (i.e. `bicep format`)
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
.
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?
Hi @alex-frankel, I know about VS Code formatting. Looking more for formatting via CLI.
Git Hooks + Autoformat on the way would be awesome
@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.
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.
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!
We would be happy to implement that in our PowerShell module (https://github.com/PSBicep/PSBicep)
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.
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
Cool! I'll give it a try
It will be released in which version ?