helmfmt is a small CLI to auto-align indentation in Helm templates. It walks chart templates recursively and normalizes indentation for control blocks, variables, and simple functions, while respecting comments.
It can be configured for Zed IDE/VS Code/VIM, and as a pre-commit hook.
Only lines starting with Go-template tags. YAML indentation is untouched.
These Go-template tags are indented:
- Control blocks:
range,with,define,block - Branching:
if,else,else if,end - Vars:
{{ $var := ... }} - Some functions:
fail,printfetc. - Comments:
{{/* ... */}}
These are not indented by default but can be configured:
tpl,template,includeandtoYamlbecause they can break YAML indentation
Before
{{- if .Values.createNamespace }}
{{- range .Values.namespaces }}
apiVersion: v1
kind: Namespace
metadata:
name: {{ . }}
{{- with $.Values.namespaceLabels }}
labels:
{{ toYaml . | indent 4 }}
{{- end }}
---
{{- end }}
{{- end }}After
{{- if .Values.createNamespace }}
{{- range .Values.namespaces }}
apiVersion: v1
kind: Namespace
metadata:
name: {{ . }}
{{- with $.Values.namespaceLabels }}
labels:
{{ toYaml . | indent 4 }}
{{- end }}
---
{{- end }}
{{- end }}go install github.com/digitalstudium/helmfmt@latestThen you should add $HOME/go/bin/ to PATH if not already done:
echo 'export PATH=$PATH:$HOME/go/bin' >> ~/.bashrc && source ~/.bashrcgit clone https://github.com/digitalstudium/helmfmt
cd helmfmt
go buildIf you compiled it via go build then you can run it with:
./helmfmt <chart-path>or add it to PATH via:
sudo install ./helmfmt /usr/local/bin/Download it from releases
helmfmt <chart-path>
helmfmt --files <file1> <file2> ...
helmfmt --files <file1> <file2> ... --stdout
helmfmt --enable-indent=toYaml,include --files <file1> <file2> ...Example run:
helmfmt ./mychart
[UPDATED] mychart/templates/deployment.yaml
Processed: 3, Updated: 1, Errors: 0or
helmfmt --files ./mychart/templates/deployment.yaml ./mychart/templates/svc.yaml
[UPDATED] mychart/templates/deployment.yaml
Processed: 2, Updated: 1, Errors: 0helmfmt can be configured using a .helmfmt file in JSON format. The tool looks for configuration files in this order:
~/.helmfmt(home directory)./.helmfmt(project directory, overrides home directory configuration)
{
"indent_size": 2,
"extensions": [".yaml", ".yml", ".tpl"],
"rules": {
"indent": {
"tpl": {
"disabled": true,
"exclude": []
},
"toYaml": {
"disabled": true,
"exclude": []
},
"template": {
"disabled": true,
"exclude": []
},
"include": {
"disabled": true,
"exclude": []
},
"printf": {
"disabled": false,
"exclude": []
},
"fail": {
"disabled": false,
"exclude": []
}
}
}
}Each rule can be configured with:
disabled: Set totrueto disable the rule entirelyexclude: Array of file patterns to exclude from this rule
Enable tpl and toYaml indentation:
{
"rules": {
"indent": {
"tpl": {
"disabled": false
},
"toYaml": {
"disabled": false
}
}
}
}Exclude test files from include indentation:
{
"rules": {
"indent": {
"include": {
"disabled": false,
"exclude": ["tests/*", "**/test-*.yaml"]
}
}
}
}Use 4 spaces for indentation:
{
"indent_size": 4
}You can override configuration rules using command-line flags:
# Enable specific rules (overrides config file)
helmfmt --enable-indent=tpl,toYaml <chart-path>To use helmfmt as a pre-commit hook, add the following to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/digitalstudium/helmfmt
rev: v0.4.2
hooks:
- id: helmfmtAdd these lines to your settings.json:
"languages": {
"Helm": {
"formatter": {
"external": {
"command": "helmfmt",
}
}
}
}In addition, you should install:
- helm language extension https://github.com/cabrinha/helm.zed
- and helm language server https://github.com/mrjosh/helm-ls
Add these lines to your settings:
"advancedLocalFormatters.formatters": [
{
"command": ["helmfmt"],
"languages": ["helm"]
}
],
"[helm]": {
"editor.defaultFormatter": "webfreak.advanced-local-formatters"
}In addition these extensions should be installed:
- https://github.com/WebFreak001/vscode-advanced-local-formatters
- https://github.com/vscode-kubernetes-tools/vscode-kubernetes-tools
Add to your .vimrc:
autocmd BufRead,BufNewFile */templates/*.yaml,*/templates/*.yml set filetype=helm
autocmd FileType helm nnoremap <buffer> <leader>f :%!helmfmt<CR>Press \f in any Helm template to format it.
If you installed the towolf/vim-helm plugin for enhanced syntax highlighting, the filetype is set automatically. Just add:
autocmd FileType helm nnoremap <buffer> <leader>f :%!helmfmt<CR>In both cases, ensure helmfmt is in your $PATH.
- More Helm funcs (dict, etc.)
- Format spaces inside tags
Issues and PRs welcome!