|
| 1 | +# :cloud: Air - Go アプリケーションのためのライブリロード |
| 2 | + |
| 3 | +[](https://github.com/air-verse/air/actions?query=workflow%3AGo+branch%3Amaster) [](https://www.codacy.com/gh/air-verse/air/dashboard?utm_source=github.com&utm_medium=referral&utm_content=air-verse/air&utm_campaign=Badge_Grade) [](https://goreportcard.com/report/github.com/air-verse/air) [](https://codecov.io/gh/air-verse/air) |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +English | [简体中文](README-zh_cn.md) | [繁體中文](README-zh_tw.md) | [日本語](README-ja.md) |
| 8 | + |
| 9 | +## 動機 |
| 10 | + |
| 11 | +Go でウェブサイトを開発し始め、 [gin](https://github.com/gin-gonic/gin) を使っていた時、gin にはライブリロード機能がないのが残念でした。 |
| 12 | + |
| 13 | +そこで探し回って [fresh](https://github.com/pilu/fresh) を試してみましたが、あまり柔軟ではないようでした。なので、もっと良いものを書くことにしました。そうして、 Air が誕生しました。 |
| 14 | + |
| 15 | +加えて、 [pilu](https:///github.com/pilu) に感謝します。fresh がなければ、 Air もありませんでした。:) |
| 16 | + |
| 17 | +Air は Go アプリケーション開発用のライブリロードコマンドラインユーティリティです。プロジェクトのルートディレクトリで `air` を実行し、放置し、コードに集中してください。 |
| 18 | + |
| 19 | +注:このツールは本番環境へのホットデプロイとは無関係です。 |
| 20 | + |
| 21 | +## 特徴 |
| 22 | + |
| 23 | +- カラフルなログ出力 |
| 24 | +- ビルドやその他のコマンドをカスタマイズ |
| 25 | +- サブディレクトリを除外することをサポート |
| 26 | +- Air 起動後は新しいディレクトリを監視します |
| 27 | +- より良いビルドプロセス |
| 28 | + |
| 29 | +### 引数から指定された設定を上書き |
| 30 | + |
| 31 | +air は引数による設定をサポートします: |
| 32 | + |
| 33 | +利用可能なコマンドライン引数を以下のコマンドで確認できます: |
| 34 | + |
| 35 | +``` |
| 36 | +air -h |
| 37 | +``` |
| 38 | +または |
| 39 | +``` |
| 40 | +air --help |
| 41 | +``` |
| 42 | + |
| 43 | +もしビルドコマンドと起動コマンドを設定したい場合は、設定ファイルを使わずに以下のようにコマンドを使うことができます: |
| 44 | + |
| 45 | +```shell |
| 46 | +air --build.cmd "go build -o bin/api cmd/run.go" --build.bin "./bin/api" |
| 47 | +``` |
| 48 | + |
| 49 | +入力値としてリストを取る引数には、アイテムを区切るためにコンマを使用します: |
| 50 | + |
| 51 | +```shell |
| 52 | +air --build.cmd "go build -o bin/api cmd/run.go" --build.bin "./bin/api" --build.exclude_dir "templates,build" |
| 53 | +``` |
| 54 | + |
| 55 | +## インストール |
| 56 | + |
| 57 | +### `go install` を使う場合(推奨) |
| 58 | + |
| 59 | +go 1.23以上を使う場合: |
| 60 | + |
| 61 | +```bash |
| 62 | +go install github.com/air-verse/air@latest |
| 63 | +``` |
| 64 | + |
| 65 | +### `install.sh` を使う場合 |
| 66 | + |
| 67 | +```shell |
| 68 | +# バイナリは $(go env GOPATH)/bin/air にインストールされます |
| 69 | +curl -sSfL https://raw.githubusercontent.com/air-verse/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin |
| 70 | + |
| 71 | +# または./bin/にインストールすることもできます |
| 72 | +curl -sSfL https://raw.githubusercontent.com/air-verse/air/master/install.sh | sh -s |
| 73 | + |
| 74 | +air -v |
| 75 | +``` |
| 76 | + |
| 77 | +### [goblin.run](https://goblin.run) を使う場合 |
| 78 | + |
| 79 | +```shell |
| 80 | +# バイナリは /usr/local/bin/air にインストールされます |
| 81 | +curl -sSfL https://goblin.run/github.com/air-verse/air | sh |
| 82 | + |
| 83 | +# 任意のパスに配置することもできます |
| 84 | +curl -sSfL https://goblin.run/github.com/air-verse/air | PREFIX=/tmp sh |
| 85 | +``` |
| 86 | + |
| 87 | +### Docker/Podman |
| 88 | + |
| 89 | +[cosmtrek/air](https://hub.docker.com/r/cosmtrek/air) という Docker イメージをプルしてください。 |
| 90 | + |
| 91 | +```shell |
| 92 | +docker/podman run -it --rm \ |
| 93 | + -w "<PROJECT>" \ |
| 94 | + -e "air_wd=<PROJECT>" \ |
| 95 | + -v $(pwd):<PROJECT> \ |
| 96 | + -p <PORT>:<APP SERVER PORT> \ |
| 97 | + cosmtrek/air |
| 98 | + -c <CONF> |
| 99 | +``` |
| 100 | + |
| 101 | +#### Docker/Podman .${SHELL}rc |
| 102 | + |
| 103 | +通常のアプリケーションのように継続的に air を使いたい場合は、 ${SHELL}rc (Bash, Zsh, etc…)に関数を作成してください。 |
| 104 | + |
| 105 | +```shell |
| 106 | +air() { |
| 107 | + podman/docker run -it --rm \ |
| 108 | + -w "$PWD" -v "$PWD":"$PWD" \ |
| 109 | + -p "$AIR_PORT":"$AIR_PORT" \ |
| 110 | + docker.io/cosmtrek/air "$@" |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +`<PROJECT>`はコンテナ内のプロジェクトのパスです。 例:`/go/example` |
| 115 | +コンテナに接続したい場合は、 `--entrypoint=bash` を追加してください。 |
| 116 | + |
| 117 | +<details> |
| 118 | + <summary>例</summary> |
| 119 | + |
| 120 | +Docker で動作するとあるプロジェクト: |
| 121 | + |
| 122 | +```shell |
| 123 | +docker run -it --rm \ |
| 124 | + -w "/go/src/github.com/cosmtrek/hub" \ |
| 125 | + -v $(pwd):/go/src/github.com/cosmtrek/hub \ |
| 126 | + -p 9090:9090 \ |
| 127 | + cosmtrek/air |
| 128 | +``` |
| 129 | + |
| 130 | +別の例: |
| 131 | + |
| 132 | +```shell |
| 133 | +cd /go/src/github.com/cosmtrek/hub |
| 134 | +AIR_PORT=8080 air -c "config.toml" |
| 135 | +``` |
| 136 | + |
| 137 | +これは `$PWD` を現在のディレクトリに置き換え、 `$AIR_PORT` は公開するポートを指定し、 `$@` は-cのようなアプリケーション自体の引数を受け取るためのものです。 |
| 138 | + |
| 139 | +</details> |
| 140 | + |
| 141 | +## 使い方 |
| 142 | + |
| 143 | +`.bashrc` または `.zshrc` に `alias air='~/.air'` を追加すると、入力の手間が省けます。 |
| 144 | + |
| 145 | +まずプロジェクトを移動します。 |
| 146 | + |
| 147 | +```shell |
| 148 | +cd /path/to/your_project |
| 149 | +``` |
| 150 | + |
| 151 | +最もシンプルな使い方は以下の通りです。 |
| 152 | + |
| 153 | +```shell |
| 154 | +# カレントディレクトリに`.air.toml`が見つからない場合は、デフォルト値を使用します |
| 155 | +air -c .air.toml |
| 156 | +``` |
| 157 | + |
| 158 | +次のコマンドを実行することで、カレントディレクトリに `.air.toml` 設定ファイルを初期化できます。 |
| 159 | + |
| 160 | +```shell |
| 161 | +air init |
| 162 | +``` |
| 163 | + |
| 164 | +その次に、追加の引数なしで `air` コマンドを実行すると、 `.air.toml` ファイルが設定として使用されます。 |
| 165 | + |
| 166 | +```shell |
| 167 | +air |
| 168 | +``` |
| 169 | + |
| 170 | +[air_example.toml](air_example.toml)を参考にして設定を編集します。 |
| 171 | + |
| 172 | +### 実行時引数 |
| 173 | + |
| 174 | +air コマンドの後に引数を追加することで、ビルドしたバイナリを実行するための引数を渡すことができる。 |
| 175 | + |
| 176 | +```shell |
| 177 | +# ./tmp/main benchを実行します |
| 178 | +air bench |
| 179 | + |
| 180 | +# ./tmp/main server --port 8080を実行します |
| 181 | +air server --port 8080 |
| 182 | +``` |
| 183 | + |
| 184 | +air コマンドに渡す引数とビルドするバイナリを `--` 引数で区切ることができる。 |
| 185 | + |
| 186 | +```shell |
| 187 | +# ./tmp/main -hを実行します |
| 188 | +air -- -h |
| 189 | + |
| 190 | +# カスタム設定で air を実行し、ビルドされたバイナリに -h 引数を渡す |
| 191 | +air -c .air.toml -- -h |
| 192 | +``` |
| 193 | + |
| 194 | +### Docker Compose |
| 195 | + |
| 196 | +```yaml |
| 197 | +services: |
| 198 | + my-project-with-air: |
| 199 | + image: cosmtrek/air |
| 200 | + # working_dir の値はマップされたボリュームの値と同じでなければなりません |
| 201 | + working_dir: /project-package |
| 202 | + ports: |
| 203 | + - <any>:<any> |
| 204 | + environment: |
| 205 | + - ENV_A=${ENV_A} |
| 206 | + - ENV_B=${ENV_B} |
| 207 | + - ENV_C=${ENV_C} |
| 208 | + volumes: |
| 209 | + - ./project-relative-path/:/project-package/ |
| 210 | +``` |
| 211 | +
|
| 212 | +### デバッグ |
| 213 | +
|
| 214 | +`air -d`は全てのログを出力します。 |
| 215 | + |
| 216 | +## air イメージを使いたくない Docker ユーザーのためのインストールと使い方 |
| 217 | + |
| 218 | +`Dockerfile` |
| 219 | + |
| 220 | +```Dockerfile |
| 221 | +# 1.16以上の利用したいバージョンを選択してください |
| 222 | +FROM golang:1.23-alpine |
| 223 | +
|
| 224 | +WORKDIR /app |
| 225 | +
|
| 226 | +RUN go install github.com/air-verse/air@latest |
| 227 | +
|
| 228 | +COPY go.mod go.sum ./ |
| 229 | +RUN go mod download |
| 230 | +
|
| 231 | +CMD ["air", "-c", ".air.toml"] |
| 232 | +``` |
| 233 | + |
| 234 | +`docker-compose.yaml` |
| 235 | + |
| 236 | +```yaml |
| 237 | +version: "3.8" |
| 238 | +services: |
| 239 | + web: |
| 240 | + build: |
| 241 | + context: . |
| 242 | + # Dockerfile へのパスを正してください |
| 243 | + dockerfile: Dockerfile |
| 244 | + ports: |
| 245 | + - 8080:3000 |
| 246 | + # ライブリロードのために、コードベースディレクトリを /app ディレクトリにバインド/マウントすることが重要です |
| 247 | + volumes: |
| 248 | + - ./:/app |
| 249 | +``` |
| 250 | + |
| 251 | +## Q&A |
| 252 | + |
| 253 | +### "command not found: air" または "No such file or directory" |
| 254 | + |
| 255 | +```shell |
| 256 | +export GOPATH=$HOME/xxxxx |
| 257 | +export PATH=$PATH:$GOROOT/bin:$GOPATH/bin |
| 258 | +export PATH=$PATH:$(go env GOPATH)/bin #この設定を .profile で確認し、追加した場合は .profile を source するのを忘れないでください!!! |
| 259 | +``` |
| 260 | + |
| 261 | +### bin に ' が含まれる場合の wsl でのエラー |
| 262 | + |
| 263 | +bin の\`'をエスケープするには`\`を使用したほうが良いです。関連する issue: [#305](https://github.com/air-verse/air/issues/305) |
| 264 | + |
| 265 | +### 質問: ホットコンパイルのみを行い、何も実行しない方法は? |
| 266 | + |
| 267 | +[#365](https://github.com/air-verse/air/issues/365) |
| 268 | + |
| 269 | +```toml |
| 270 | +[build] |
| 271 | + cmd = "/usr/bin/true" |
| 272 | +``` |
| 273 | + |
| 274 | +### 静的ファイルの変更時にブラウザを自動的にリロードする方法 |
| 275 | + |
| 276 | +詳細のために [#512](https://github.com/air-verse/air/issues/512) の issue を参照してください。 |
| 277 | + |
| 278 | +- 静的ファイルを `include_dir` 、`include_ext` 、`include_file` に配置していることを確かめてください。 |
| 279 | +- HTML に `</body>` タグがあることを確かめてください。 |
| 280 | +- プロキシを有効にするには、以下の設定を行います: |
| 281 | + |
| 282 | +```toml |
| 283 | +[proxy] |
| 284 | + enabled = true |
| 285 | + proxy_port = <air proxy port> |
| 286 | + app_port = <your server port> |
| 287 | +``` |
| 288 | + |
| 289 | +## 開発 |
| 290 | + |
| 291 | +依存関係を管理するために `go mod` を使っているので、 Go 1.16+ が必要であることに注意してください。 |
| 292 | + |
| 293 | +```shell |
| 294 | +# プロジェクトをフォークしてください |
| 295 | +
|
| 296 | +# クローンしてください |
| 297 | +mkdir -p $GOPATH/src/github.com/cosmtrek |
| 298 | +cd $GOPATH/src/github.com/cosmtrek |
| 299 | +git clone [email protected]:<YOUR USERNAME>/air.git |
| 300 | +
|
| 301 | +# 依存関係をインストールしてください |
| 302 | +cd air |
| 303 | +make ci |
| 304 | +
|
| 305 | +# コードを探検してコーディングを楽しんでください! |
| 306 | +make install |
| 307 | +``` |
| 308 | + |
| 309 | +Pull Request を受け付けています。 |
| 310 | + |
| 311 | +### リリース |
| 312 | + |
| 313 | +```shell |
| 314 | +# master にチェックアウトします |
| 315 | +git checkout master |
| 316 | +
|
| 317 | +# リリースに必要なバージョンタグを付与します |
| 318 | +git tag v1.xx.x |
| 319 | +
|
| 320 | +# リモートにプッシュします |
| 321 | +git push origin v1.xx.x |
| 322 | +
|
| 323 | +# CI が実行され、新しいバージョンがリリースされます。約5分待つと最新バージョンを取得できます |
| 324 | +``` |
| 325 | + |
| 326 | +## スターヒストリー |
| 327 | + |
| 328 | +[](https://star-history.com/#air-verse/air&Date) |
| 329 | + |
| 330 | +## スポンサー |
| 331 | + |
| 332 | +[](https://www.buymeacoffee.com/cosmtrek) |
| 333 | + |
| 334 | +多くのサポーターの方々に心から感謝します。私はいつも皆さんの優しさを忘れません。 |
| 335 | + |
| 336 | +## ライセンス |
| 337 | + |
| 338 | +[GNU General Public License v3.0](LICENSE) |
0 commit comments