github.com/fairyhunter13/air@v1.40.5/README-zh_cn.md (about) 1 # Air [![Go](https://github.com/fairyhunter13/air/workflows/Go/badge.svg)](https://github.com/fairyhunter13/air/actions?query=workflow%3AGo+branch%3Amaster) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/dcb95264cc504cad9c2a3d8b0795a7f8)](https://www.codacy.com/gh/fairyhunter13/air/dashboard?utm_source=github.com&utm_medium=referral&utm_content=fairyhunter13/air&utm_campaign=Badge_Grade) [![Go Report Card](https://goreportcard.com/badge/github.com/fairyhunter13/air)](https://goreportcard.com/report/github.com/fairyhunter13/air) [![codecov](https://codecov.io/gh/fairyhunter13/air/branch/master/graph/badge.svg)](https://codecov.io/gh/fairyhunter13/air) 2 3 :cloud: 热重载 Go 应用的工具 4 5 ![air](docs/air.png) 6 7 [English](README.md) | 简体中文 8 9 ## 开发动机 10 11 当我用 Go 和 [gin](https://github.com/gin-gonic/gin) 框架开发网站时,gin 缺乏实时重载的功能是令人遗憾的。我曾经尝试过 [fresh](https://github.com/pilu/fresh) ,但是它用起来不太灵活,所以我试着用更好的方式来重写它。Air 就这样诞生了。此外,非常感谢 [pilu](https://github.com/pilu)。没有 fresh 就不会有 air :) 12 13 Air 是为 Go 应用开发设计的另外一个热重载的命令行工具。只需在你的项目根目录下输入 `air`,然后把它放在一边,专注于你的代码即可。 14 15 **注意**:该工具与生产环境的热部署无关。 16 17 ## 特色 18 19 * 彩色的日志输出 20 * 自定义构建或必要的命令 21 * 支持外部子目录 22 * 在 Air 启动之后,允许监听新创建的路径 23 * 更棒的构建过程 24 25 ### ✨ beta 版本的特性 26 27 支持使用参数来配置 air 字段: 28 29 如果你只是想配置构建命令和运行命令,您可以直接使用以下命令,而无需配置文件: 30 31 `air --build.cmd "go build -o bin/api cmd/run.go" --build.bin "./bin/api"` 32 33 对于以列表形式输入的参数,使用逗号来分隔项目: 34 35 `air --build.cmd "go build -o bin/api cmd/run.go" --build.bin "./bin/api" --build.exclude_dir "templates,build"` 36 37 ## 安装 38 39 ### 推荐使用 install.sh 40 41 ```bash 42 # binary 文件会是在 $(go env GOPATH)/bin/air 43 curl -sSfL https://raw.githubusercontent.com/fairyhunter13/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin 44 45 # 或者把它安装在 ./bin/ 路径下 46 curl -sSfL https://raw.githubusercontent.com/fairyhunter13/air/master/install.sh | sh -s 47 48 air -v 49 ``` 50 51 P.S. 非常感谢 mattn 的 [PR](https://github.com/cosmtrek/air/pull/1),使得 Air 支持 Windows 平台。 52 53 ### 使用 `go install` 54 55 使用 Go 的版本为 1.16 或更高: 56 57 ```bash 58 go install github.com/fairyhunter13/air@latest 59 ``` 60 61 ### Docker 62 63 请拉取这个 Docker 镜像 [fairyhunter13/air](https://hub.docker.com/r/fairyhunter13/air). 64 65 ```bash 66 docker run -it --rm \ 67 -w "<PROJECT>" \ 68 -e "air_wd=<PROJECT>" \ 69 -v $(pwd):<PROJECT> \ 70 -p <PORT>:<APP SERVER PORT> \ 71 fairyhunter13/air 72 -c <CONF> 73 ``` 74 75 例如,我的项目之一是在 Docker 上运行的: 76 77 ```bash 78 docker run -it --rm \ 79 -w "/go/src/github.com/cosmtrek/hub" \ 80 -v $(pwd):/go/src/github.com/cosmtrek/hub \ 81 -p 9090:9090 \ 82 fairyhunter13/air 83 ``` 84 85 ## 使用方法 86 87 您可以添加 `alias air='~/.air'` 到您的 `.bashrc` 或 `.zshrc` 后缀的文件. 88 89 首先,进入你的项目文件夹 90 91 ```bash 92 cd /path/to/your_project 93 ``` 94 95 最简单的方法是执行 96 97 ```bash 98 # 优先在当前路径查找 `.air.toml` 后缀的文件,如果没有找到,则使用默认的 99 air -c .air.toml 100 ``` 101 102 您可以运行以下命令初始化,把默认配置添加到当前路径下的`.air.toml` 文件。 103 104 ```bash 105 air init 106 ``` 107 108 在这之后,你只需执行 `air` 命令,无需添加额外的变量,它就能使用 `.air.toml` 文件中的配置了。 109 110 ```bash 111 air 112 ``` 113 114 如欲修改配置信息,请参考 [air_example.toml](air_example.toml) 文件. 115 116 ### 运行时参数 117 118 您可以通过把变量添加在 air 命令之后来传递参数。 119 120 ```bash 121 # 会执行 ./tmp/main bench 122 air bench 123 124 # 会执行 ./tmp/main server --port 8080 125 air server --port 8080 126 ``` 127 128 You can separate the arguments passed for the air command and the built binary with `--` argument. 129 130 ```bash 131 # 会运行 ./tmp/main -h 132 air -- -h 133 134 # 会使用个性化配置来运行 air,然后把 -h 后的变量和值添加到运行的参数中 135 air -c .air.toml -- -h 136 ``` 137 138 ### Docker-compose 139 140 ``` 141 services: 142 my-project-with-air: 143 image: fairyhunter13/air 144 # working_dir value has to be the same of mapped volume 145 working_dir: /project-package 146 ports: 147 - <any>:<any> 148 environment: 149 - ENV_A=${ENV_A} 150 - ENV_B=${ENV_B} 151 - ENV_C=${ENV_C} 152 volumes: 153 - ./project-relative-path/:/project-package/ 154 ``` 155 156 ### 调试 157 158 运行 `air -d` 命令能打印所有日志。 159 160 ## Q&A 161 162 ### 遇到 "command not found: air" 或 "No such file or directory" 该怎么办? 163 164 ```zsh 165 export GOPATH=$HOME/xxxxx 166 export PATH=$PATH:$GOROOT/bin:$GOPATH/bin 167 export PATH=$PATH:$(go env GOPATH)/bin <---- 请确认这行在您的配置信息中!!! 168 ``` 169 170 ## 部署 171 172 请注意:这需要 Go 1.16+ ,因为我使用 `go mod` 来管理依赖。 173 174 ```bash 175 # 1. 首先复刻(fork)这个项目 176 177 # 2. 其次克隆(clone)它 178 mkdir -p $GOPATH/src/github.com/cosmtrek 179 cd $GOPATH/src/github.com/cosmtrek 180 git clone git@github.com:<YOUR USERNAME>/air.git 181 182 # 3. 再次安装依赖 183 cd air 184 make ci 185 186 # 4. 这样就可以快乐地探索和玩耍啦! 187 make install 188 ``` 189 190 顺便说一句: 欢迎 PR~ 191 192 ### 发布新版本 193 194 ``` 195 # 1. checkout 到 master 分支 196 git checkout master 197 198 # 2. 添加需要发布的版本号 199 git tag v1.xx.x 200 201 # 3. 推送到远程 202 git push origin v1.xx.x 203 204 ci 会加工和处理,然后会发布新版本。等待大约五分钟,你就能获取到新版本了。 205 ``` 206 207 ## 赞助 208 209 <a href="https://www.buymeacoffee.com/36lcNbW" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" style="height: 51px !important;width: 217px !important;" ></a> 210 211 非常感谢以下的支持者们。我会一直你们的好意的。 212 213 * Peter Aba 214 * Apostolis Anastasiou 215 * keita koga 216 217 ## 许可证 218 219 [GNU General Public License v3.0](LICENSE)