计划整理YAML文件?

Ale*_*ine 5 yaml lint

因此,我的团队目前正在开发一个使用大量YAML的应用程序,我们需要为通用格式设置一些规则。最好的方法是使用命令行程序,以便我们可以将其插入CI。

最重要的是,我们要强制2空格缩进和列表缩进,如下所示:

list:
- not indented
- not indented            # this is BAD

list:
  - indented
  - indented              # this is GOOD

mapping:
     5 space indentation  # this is BAD
Run Code Online (Sandbox Code Playgroud)

防止尾随空格和无意义的语法也很重要。

我找到了一些检查YAML有效性的网站(即是否可以加载它),但没有掉毛。yaml-lint也是如此,这是一个仅“检查是否可以加载您的YAML文件”的Ruby应用程序。

是否有针对YAML的 lint,类似于python的flake8或javascript的eslint?

Adr*_*rgé 5

您正在寻找yamllint。在你的 CI 中:

sudo pip install yamllint
yamllint file1.yml ...
Run Code Online (Sandbox Code Playgroud)

它具有高度可配置性。具体来说,对于 2 个空格缩进和强制列表缩进,conf 将为:

rules:
  indentation: {spaces: 2, indent-sequences: yes}
Run Code Online (Sandbox Code Playgroud)

(它还处理尾随空格、行长度等)

玩得开心!