【Heroku】MacにHeroku CLIをインストールする

# はじめに

Railsなどを中心に勉強中のエンジニア初心者が他の記事を参考にしたり、実際に実装してみたりして、アウトプットの一環としてまとめたものです。
間違っていることもあると思われるので、その際は指摘いただけると幸いです。

# Heroku CLIインストール用の公式ドキュメント

https://devcenter.heroku.com/articles/heroku-cli#download-and-install

# Heroku CLIのインストール

### 実行コマンド

```bash
brew tap heroku/brew && brew install heroku
```

### 実行結果

```bash
% brew tap heroku/brew && brew install heroku
Running `brew update --auto-update`...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/services).
==> New Formulae
aptos               flagd               okta-awscli         scriptisto
aws-sam-cli         gokey               openvino            simdutf
bzip3               grayskull           pari-nflistdata     twm
cascadia            kubent              pipdeptree          video-compare
cdebug              kwctl               plz-cli             xinit
check-jsonschema    kwok                podsync             xmodmap
copa                liblerc             prs                 xorg-server
dstack              libsais             quartz-wm           xrdb
ecoji               ocm                 ruby@3.1

You have 15 outdated formulae installed.
You can upgrade them with brew upgrade
or list them with brew outdated.

==> Tapping heroku/brew
Cloning into '/opt/homebrew/Library/Taps/heroku/homebrew-brew'...
remote: Enumerating objects: 1874, done.
remote: Counting objects: 100% (259/259), done.
remote: Compressing objects: 100% (183/183), done.
remote: Total 1874 (delta 74), reused 247 (delta 63), pack-reused 1615
Receiving objects: 100% (1874/1874), 245.10 KiB | 1.36 MiB/s, done.
Resolving deltas: 100% (470/470), done.
Tapped 2 formulae (17 files, 305.0KB).
==> Fetching dependencies for heroku/brew/heroku: heroku/brew/heroku-node
==> Fetching heroku/brew/heroku-node
==> Downloading https://nodejs.org/download/release/v14.19.0/node-v14.19.0-darwi
######################################################################## 100.0%
==> Fetching heroku/brew/heroku
==> Downloading https://cli-assets.heroku.com/heroku-v7.67.2/heroku-v7.67.2.tar.
######################################################################## 100.0%
==> Installing heroku from heroku/brew
==> Installing dependencies for heroku/brew/heroku: heroku/brew/heroku-node
==> Installing heroku/brew/heroku dependency: heroku/brew/heroku-node
🍺  /opt/homebrew/Cellar/heroku-node/14.19.0: 6 files, 73.5MB, built in 6 seconds
==> Installing heroku/brew/heroku
==> Caveats
To use the Heroku CLI's autocomplete --
  Via homebrew's shell completion:
    1) Follow homebrew's install instructions https://docs.brew.sh/Shell-Completion
        NOTE: For zsh, as the instructions mention, be sure compinit is autoloaded
              and called, either explicitly or via a framework like oh-my-zsh.
    2) Then run
      $ heroku autocomplete --refresh-cache
  OR
  Use our standalone setup:
    1) Run and follow the install steps:
      $ heroku autocomplete

zsh completions have been installed to:
  /opt/homebrew/share/zsh/site-functions
==> Summary
🍺  /opt/homebrew/Cellar/heroku/7.67.2: 22,545 files, 66.1MB, built in 21 seconds
==> Running `brew cleanup heroku`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> `brew cleanup` has not been run in the last 30 days, running now...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

Pruned 0 symbolic links and 2 directories from /opt/homebrew
==> Caveats
==> heroku
To use the Heroku CLI's autocomplete --
  Via homebrew's shell completion:
    1) Follow homebrew's install instructions https://docs.brew.sh/Shell-Completion
        NOTE: For zsh, as the instructions mention, be sure compinit is autoloaded
              and called, either explicitly or via a framework like oh-my-zsh.
    2) Then run
      $ heroku autocomplete --refresh-cache
  OR
  Use our standalone setup:
    1) Run and follow the install steps:
      $ heroku autocomplete

zsh completions have been installed to:
  /opt/homebrew/share/zsh/site-functions
```

# Herokuのバージョン確認

インストールが完了したら、バージョンを確認する。

### 実行コマンド

```bash
heroku --version
```

### 実行結果

```bash
% heroku --version
 ›   Warning: Our terms of service have changed: https://dashboard.heroku.com/terms-of-service
heroku/7.67.2 darwin-x64 node-v14.19.0
```

# ****Heroku autocompleteの設定****

インストール時に、下記のように指示が出ているため、こちらを実行する。

```bash
To use the Heroku CLI's autocomplete --
  Via homebrew's shell completion:
    1) Follow homebrew's install instructions https://docs.brew.sh/Shell-Completion
        NOTE: For zsh, as the instructions mention, be sure compinit is autoloaded
              and called, either explicitly or via a framework like oh-my-zsh.
    2) Then run
      $ heroku autocomplete --refresh-cache
  OR
  Use our standalone setup:
    1) Run and follow the install steps:
      $ heroku autocomplete                         # これを実行

zsh completions have been installed to:
  /opt/homebrew/share/zsh/site-functions
```

### 実行コマンド

```bash
heroku autocomplete
```

### 実行結果

`heroku autocomplete`を実行すると、何やら質問され適当にキーボード押すとブラウザが立ち上がりログインを求められる（質問時に`q`を押すと`exit`）。

```bash
% heroku autocomplete
Building the autocomplete cache... ?

Opening browser to https://cli-auth.heroku.com/auth/cli/browser/f377a2cd-f125-4750-8aa1-176a3a571ec4?requestor=SFMyNTY.g2gDbQAAAA02MC4xMzUuOTMuMTI4bgYAYOZX5IUBYgABUYA.NMGEkVEwu1c4EBO3Rj7dRmU9MThekhwgbDT2JNn3MjY
Logging in... done

Setup Instructions for HEROKU CLI Autocomplete ---

1) Add the autocomplete env var to your zsh profile and source it
$ printf "$(heroku autocomplete:script zsh)" >> ~/.zshrc; source ~/.zshrc                             # これを実行

NOTE: After sourcing, you can run `$ compaudit -D` to ensure no permissions conflicts are present

2) Test it out, e.g.:
$ heroku <TAB>                 # Command completion
$ heroku apps:info --<TAB>     # Flag completion
$ heroku apps:info --app=<TAB> # Flag option completion

Visit the autocomplete Dev Center doc at https://devcenter.heroku.com/articles/heroku-cli-autocomplete

Enjoy!
```

# zshに設定をパスを書き込む

上記のコマンド実行結果に記載された設定を実施する。

```bash
% printf "$(heroku autocomplete:script zsh)" >> ~/.zshrc; source ~/.zshrc
```

# 自動補完機能の確認

`heroku l`まで打ち込んで「TAB」を押すと補完機能が働き、下記のように表示されるはず。

```bash
$ heroku l
labs           -- list experimental features
labs:disable   -- disables an experimental feature
labs:enable    -- enables an experimental feature
labs:info      -- show feature info
leave          -- remove yourself from a team app
local          -- run heroku app locally
local:run      -- run a one-off command
local:version  -- display node-foreman version
lock           -- prevent team members from joining an app
logs           -- display recent log output
```

# 参考

https://labo.kon-ruri.co.jp/heroku-cli-install/

https://yuruli.info/install-heroku-cli-to-mac-catalina/

# 最後に

いかがでしたでしょうか。
今回はMacにHeroku CLIをインストールする方法についてまとめてみました。
ここ違うよ！でしたり、こうした方がいいよ！などがあればコメントいただけると幸いです。

他にも下記のような記事を投稿しております。
興味がありましたら、ぜひご覧ください。

https://qiita.com/sekkey_777/items/b71f126527081d46744b

https://qiita.com/sekkey_777/items/729dada2998d3914e138

https://qiita.com/sekkey_777/items/8a8b3ee2af3c46df50f0