LoginSignup
3
2

Twilio CLI 3.0

Last updated at Posted at 2022-01-24

2023年5月1日を持ちまして、株式会社KDDIウェブコミュニケーションズのTwilioリセール事業が終了したため、本記事に記載されている内容は正確ではないことを予めご了承ください。

はじめに

みなさん、こんにちは。
KDDIウェブコミュニケーションズの Twilio事業部エバンジェリストの高橋です。

今回は、2022年1月にリリースされた Twilio CLI Ver.3.0へのアップデート方法について書いていきます。

バージョンアップ内容

2021年12月17日にメジャーバージョンアップのお知らせがメールで届きましたが、そこには色々と注意点が記載されています。

If you have profiles and want to upgrade your CLI: After the CLI upgrade, run the twilio profiles:port command to migrate the API keys from the keychain to the config file for all your profiles.
This command ports ALL profile keys by default, although to only port a specific key append the profile-id as an additional argument (twilio profiles:port [PROFILE]).

profilesを使っている場合は、profiles:portオプションを使って認証情報を移行してくださいとのことです。
どうやら、Ver.2.xまでは、認証情報をOSのKeychainに保存していましたが、それをやめて独自にローカル保存するように変更されたようです。
profileというのは、Twilioのアカウント(サブアカウントを含む)を複数保有している場合に便利な機能で、プロファイルを切り替えながらCLIを実行することができるしくみのことです。

自分自身がプロファイルを複数持っているかどうかは、以下のコマンドで確認が可能です。

% twilio profiles:list

2つ目の注意点は次のとおりです。

If you have plugins installed: After the CLI upgrade and profile port, upgrade your plugin to the latest version to use the profiles in your plugin commands.

プラグインを使っている場合はアップデートしてねということですね。
Twilio CLI Pluginsには、結構便利なものがあるので使っている方も多いかと思います。
インストールされているプラグインの一覧は以下のコマンドで確認できます。

% twilio plugins

3つめの注意点は次のとおりです。

If you have built plugins: Upgrade your CLI-core dependency to v6.0.0.

もしプラグインを自分で作っている人は、CLI-coreをv6.0.0にアップデートしてくださいってことですね。流石に自分自身でプラグインを作っている人はあまりいないかもしれませんが、実はTwilio CLIはOpen CLI Frameworkで作られているため自分で独自のプラグインを作ることもできます。プラグインの作成にチャレンジしたい方は、以下のGitHubを参考にしてみてください。
https://github.com/twilio/twilio-cli/blob/main/docs/plugins.md

ではいよいよバージョンアップと上記の注意点について解説していきたいと思います。

バージョンアップ

まずは、Ver.3.xにアップデートしてみましょう。
Macでのアップデートは以下のコマンドで実行できます。

% brew upgrade twilio
Error:
  homebrew-core is a shallow clone.
  homebrew-cask is a shallow clone.
To `brew update`, first run:
  git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
  git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow
These commands may take a few minutes to run due to the large size of the repositories.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!
Warning: twilio/brew/twilio 2.28.0 already installed

おっとっと。早速エラーがでちゃいましたね。調べると、Twilio CLIの問題ではなく、Homebrewの問題のようです。
Homebrew で Error: homebrew-core is a shallow clone が出て brew update が実行できない問題

では、書かれている通りに実行してみます。

git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow

ちなみに、上記コマンドはすごく時間がかかります。反応がないように見えますが、辛抱強く待ってくださいね。
続けて、こちらも実行します。

git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow

こちらはそれほど時間はかかりませんでした。
では、気を取り直してVer.3.xにアップデートします。

% brew upgrade twilio
Updating Homebrew...
(省略)
==> Upgrading twilio/brew/twilio
  2.28.0 -> 3.0.0

どうやらうまくアップデートされたようです。

Profileの移行

まずは現在のプロファイルを確認しておきます。

% twilio profiles:list
ID                Account SID                         Active
SIGNAL2019        ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  false
MainAccount       ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  true
(省略)
VerifyDemo        ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  false

かなりいっぱいプロファイルがあります。
これらを移行するには、注意書きにあったように profiles:port を使えば良いようです。そもそもこのオプションはどんなものかを見てみましょう。

% twilio profiles:port --help
Port API keys from keytar to config file. This command ports ALL keys by default, although to only port a specific key append the profile-id as additional argument.

USAGE
  $ twilio profiles:port [PROFILE]

ARGUMENTS
  PROFILE  Profile-id for porting keys standalone

OPTIONS
OPTIONAL FLAGS
  -l=(debug|info|warn|error|none)  [default: info] Level of logging messages.
  -o=(columns|json|tsv|none)       [default: columns] Format of command output.
  --silent                         Suppress output and logs. This is a shorthand for "-l none -o none".

MORE INFO

MORE INFOの先がないのが気になりますが、とりあえずProfile名を指定しないとすべてのプロファイルが移行されるようです。では最初は特定のプロファイルをデバッグモードで移行してみましょう。

% twilio profiles:port MainAccount -l debug -o json
[DEBUG] Config File: /Users/katsumi/.twilio-cli/config.json
? This command will port API keys from keytar to config file at location /Users/katsumi/.twilio-cli/config.json. Continue? Yes
Porting MainAccount... Done!

途中で移行するかどうか聞かれますが、Yで進めます。
どうやら、ユーザーディレクトリの直下に.twilio-cli/config.jsonに設定情報を移行したようです。
このJSONファイルは、Ver.2までは以下のような情報が書かれていました。

{
  "email": {},
  "prompts": {},
  "projects": [
    {
      "id": "SIGNAL2019",
      "accountSid": "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    },
    {
      "id": "MainAccount",
      "accountSid": "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    },
(省略)
    {
      "id": "VerifyDemo",
      "accountSid": "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
  ],
  "activeProject": "MainAccount"
}

これが先程のコマンドを実行したら以下のように書き換わりました。

{
  "email": {},
  "prompts": {},
  "projects": [
    {
      "id": "SIGNAL2019",
      "accountSid": "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    },
(省略)
    {
      "id": "VerifyDemo",
      "accountSid": "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
  ],
  "profiles": {
    "MainAccount": {
      "accountSid": "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "apiKey": "SKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "apiSecret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
  },
  "activeProject": "MainAccount"
}

結論から言うと、profilesという項目が追加されて、そこにapiKeyとapiSecretが記録されるようになっているようです。
apiKeyとapiSecretは、非常に重要な情報となりますので、このファイルは外部に漏洩しないようにしないといけません。

では、すべてのプロファイルを移行してしまいましょう。

% twilio profiles:port

これですべてのプロファイルが移行できました。
今回のバージョンアップでプロファイルの管理方法は変わりましたが、プロファイル自体の使い方は変わりません。
プロファイルを切り替えるには、以下のように行います。

% twilio profiles:use [切り替えたいプロファイル名]

また、`--profile``オプションも以前と同じように利用できます。

プラグインのバージョンアップ

では次に、プラグインのバージョンアップもしてみます。
バージョンアップする前に、現在のプラグインのリストを確認しておきましょう。

% twilio plugins
@twilio-labs/plugin-rtc 0.1.1
@twilio-labs/plugin-serverless 1.2.0
@twilio-labs/plugin-token 3.0.0
@twilio-labs/plugin-watch 2.0.3

ではバージョンアップしましょう。

% twilio plugins:update
error create-twilio-function@3.3.2: The engine "node" is incompatible with this module. Expected version ">=12.22.1". Got "12.8.0"
error Found incompatible module.
twilio-cli: Updating plugins... !
    Error: yarn add @twilio-labs/plugin-serverless@latest @twilio-labs/plugin-watch@latest @twilio-labs/plugin-token@latest
    @twilio-labs/plugin-rtc@latest --non-interactive --mutex=file:/Users/katsumi/.twilio-cli/yarn.lock
    --preferred-cache-folder=/Users/katsumi/.twilio-cli/yarn --check-files exited with code 1

うおー。エラー出た。
これだけだとちょっとわからないので、確認モードで再実行してみましょう。

% twilio plugins:update -v
(省略)
[2/4] Fetching packages...
error create-twilio-function@3.3.2: The engine "node" is incompatible with this module. Expected version ">=12.22.1". Got "12.8.0"
verbose 8.995022278 Error: Found incompatible module.
(省略)

あー、Node.jsのバージョンが低いようです。
12.22.1以上で実行してくださいとのことなので、Node.jsのバージョンを12.22.9にしてから再実行します。

% node -v
v12.22.9
% twilio plugins:update -v
(省略)
Done in 23.74s.
twilio-cli: Updating plugins... done
 » If you’re using autocomplete, you’ll need to run twilio autocomplete after an update and then open a new terminal window. The CLI needs to re-build its cache.

どうやらうまくいきました。
オートコンプリート機能(自動でコマンドを補完してくれる便利なやつ)を使っているので、そちらも再設定します。

% twilio autocomplete
Building the autocomplete cache... done

Setup Instructions for TWILIO CLI Autocomplete ---

1) Add the autocomplete env var to your zsh profile and source it
$ printf "eval $(twilio 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.:
$ twilio <TAB>                 # Command completion
$ twilio command --<TAB>       # Flag completion

Enjoy!

 » If you are running bash or zsh on macOS or Linux, you can run one of the two commands below (as appropriate for the shell you are using):
 '1) twilio autocomplete bash' or
 '2) twilio autocomplete zsh'

指示に従って以下のコマンドを実行します。

% printf "eval $(twilio autocomplete:script zsh)" >> ~/.zshrc; source ~/.zshrc

さらに以下のコマンドも実行します。

% compaudit -D

これでTABキーでコマンド補完ができます。

では、プラグインの一覧を再度確認してみましょう。

% twilio plugins
@twilio-labs/plugin-rtc 0.8.2
@twilio-labs/plugin-serverless 3.0.0
@twilio-labs/plugin-token 4.0.0
@twilio-labs/plugin-watch 2.0.3

plugin-watch以外はバージョンがあがってますね。

まとめ

チェンジログを見る限り、今回のバージョンアップでは、プロファイル以外にCLIの機能としての大きな差分はないようです。
以前のバージョンだと、Node.jsのバージョンによってCLIのコマンドがうまく動いたり動かなかったりしていたのが、今回のバージョンで解消されていると信じて、これからもCLIを使っていきたいと思います。


Twilio(トゥイリオ)とは

https://cloudapi.kddi-web.com
Twilio は音声通話、メッセージング(SMS /チャット)、ビデオなどの 様々なコミュニケーション手段をアプリケーションやビジネスへ容易に組み込むことのできるクラウド API サービスです。初期費用不要な従量課金制で、各種開発言語に対応しているため、多くのハッカソンイベントやスタートアップなどにも、ご利用いただいております。

自己紹介  
高橋克己(Katsumi Takahashi) 自称「赤い芸人
グローバル・インターネット・ジャパン株式会社 代表取締役
株式会社KDDIウェブコミュニケーションズ Twilio事業部エバンジェリスト

2001年より大手通信事業者の法人サービスの教育に携わり、企業における電話のしくみや重要性を研究。2016年よりTwilio事業部にジョインし、Twilioを使ったスマートコミュニケーションの普及活動を精力的に行っている。
2015 Hall of Doers
2019 Twilio Champions
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2