I've found very few features in many editing software for formatting nginx configurations. However, Nginx configuration files are often complex and manually formatting them can become tedious and time-consuming. In this article, we will introduce how to use Alfred Workflow to quickly format Nginx configuration files on macOS and improve the readability and maintainability of the configuration files.
Prerequisites:
1. First of all, you must have it alfred
. I think this is an artifact (for mac).
2. You need to have the tool to format the nginx configuration file. Here we use the nginxfmt
download command under Python:pip3 install nginxfmt
Once the above is met, we can perform the operation
Implementation
Script content
#!/bin/bash
# 获取剪贴板中的nginx配置代码块
text="$(pbpaste)"
# 使用nginx-config-formatter来格式化配置
formatted_config=$(/Users/xiaobo/miniforge3/bin/nginxfmt --pipe <<< "$text")
# 将格式化后的配置复制到剪贴板
echo "$formatted_config" | pbcopy
The above is
/Users/xiaobo/miniforge3/bin/nginxfmt
your nginxfmt path, which can bewhich nginxfmt
obtained using
With the above, you can select the content you want to format. Execute option+command+L and the formatted nginx will appear on the clipboard.