Github Copilot in the CLI を使ってみる

はじめに

先日Githubからメールが届きました。

1
You have been granted access to GitHub Copilot in the CLI.

ようやく Github Copilot in the CLI が利用できるようになりました。

今までは Vim 環境でだけGithub Copilot を使うことができましたが、コマンドライン環境からCopilotの力を利用できることになるのでしょうか?と思い早速試してみました。

検証環境

1
2
3
4
5
$ uname -moi
arm64 unknown Darwin

$ bash -version | head -n 1
GNU bash, バージョン 5.2.15(1)-release (aarch64-apple-darwin22.1.0)

作業の流れ ( 作業ログ )

まずはメールのリンクに書かれていたドキュメントページから概要や使い方を学んでいくことにしました。

Github Copilot in the CLI の概要

Github Copilot in the CLI を使うと、ターミナル操作中にコマンドの候補を提示してくれたり、コマンドの説明を提示してくれたりするそうです。

_2023-12-19現在はパブリックベータとして提供されているサービスですので、まだまだ基本機能や使い方が変わっていくかもしれません。 _

以降、Copilot CLI と省略します。

Copilot CLI はターミナル上にチャットのようなインターフェイスを提供してくれます。 このインターフェイスを通して質問することができます。

Copilot CLI は質問に対する回答としてコマンド候補や説明を提示してくれます。

早速使ってみる

Copilot CLI は Github CLI ( gh コマンド ) の拡張という位置づけのようです

gh copilot suggest コマンドを実行することで候補を表示できるとのことなので早速試してみます。

ちなみに gh コマンドのインストール方法や使い方については過去に別エントリで紹介しています

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$ gh copilot suggest
unknown command "copilot" for "gh"

Usage:  gh <command> <subcommand> [flags]

Available commands:
  alias
  api
  auth
  browse
  cache
  co
  codespace
  completion
  config
  extension
  gist
  gpg-key
  issue
  label
  org
  pr
  project
  release
  repo
  ruleset
  run
  search
  secret
  ssh-key
  status
  variable
  workflow

実行に失敗しました。 拡張をインストールする必要があるようです。

インストール

_事前に gh auth login コマンドを実行してGithubへの認証を完了しておきましょう。 _

拡張をインストールするには以下のコマンドを実行します。

1
2
$ gh extension install github/gh-copilot
✓ Installed extension github/gh-copilot

アップグレードしたい場合は以下のコマンドを実行するそうです。

1
2
3
$ gh extension upgrade gh-copilot
[copilot]: already up to date
✓ Successfully upgraded extension

gh copilot suggest

改めて先程失敗した gh copilot suggest コマンドを実行してみます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ gh copilot suggest

? Allow GitHub to collect optional usage data to help us improve? This data does not include your queries.  [Use arrows to move, type to filter]
> Yes
  No

? What kind of command can I help you with?  [Use arrows to move, type to filter]
> generic shell command
  gh command
  git command

確かにチャットライクなインタラクティブ操作で質問に答えていくインターフェイスとなっています。 実行したいコマンドの種類を聞かれましたので、 ghgit 以外のコマンドを提案してもらうことにします。

続けます。

1
2
? What would you like the shell command to do?
> I want to connect redis server in the terminal.

実際に最近困った操作について質問してみます。 具体的には Redis サーバにターミナルから接続したかったのですがコマンドが分からなかったという場面を想定してみました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Suggestion:

  redis-cli

? Select an option  [Use arrows to move, type to filter]
> Copy command to clipboard
  Explain command
  Revise command
  Rate response
  Exit

redis-cli を使って接続できるよ、と教えてくれました。 このコマンドについて引き続きどうするかを聞かれています。 クリップボードにコピーすることができるようですね。 説明の表示はどの程度まで説明してくれるのでしょう?

1
2
3
4
5
6
7
8
? Select an option
> Explain command

Explanation:

  • redis-cli is the command-line interface for Redis, an in-memory data structure store.
    • Use it to interact with Redis server and execute commands.
    • It provides an interactive shell where you can run Redis commands and manage data.

なるほど。 。 MANページをサマリーしたような内容ということなのかな。 思ったよりもリッチな情報が表示されるわけではなかった。

gh copilot explain

Copilot CLI に対してコマンドの説明を依頼するためのコマンドとして gh copilot explain があるそうなので試してみます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ gh copilot explain redis-cli

Welcome to GitHub Copilot in the CLI!
version 0.5.3-beta (2023-11-09)

I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve.

Explanation:

  • redis-cli is a command-line interface for interacting with Redis, an in-memory data structure store.
    • It is used to connect to a Redis server and perform various operations like retrieving and setting values, executing commands, managing keys, and more.

先程 sugguest 実行後に対話的に選択した結果は gh copilot explain と同じのようです。

もう少し複雑なコマンドを引数として与えた場合はどうなるでしょう?

gh コマンドを使ってプルリクエストのIDを一覧表示するワンライナーについて説明してもらいます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$ gh copilot explain 'gh pr list | awk "{ print \$1 }"'

Welcome to GitHub Copilot in the CLI!
version 0.5.3-beta (2023-11-09)

I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve.

Explanation:

  • gh pr list uses the gh command-line tool to list pull requests.
| pipes the output of the previous command to the next command.
  • awk is a text processing tool.
"{ print \$1 }" is an awk script that prints the first field of each line, which corresponds to the first column in this case.
\$1 represents the first field/column of each line. The backslash is used to escape the dollar sign since it has a special meaning in the shell.

もちろん説明は全く間違っていないのですが、「それは知ってる」というような内容ですね。 コードを自然言語に変換しただけでした。

ひとこと

今のところはあまり必要なさそうです。