Outsider's Dev Story

Stay Hungry. Stay Foolish. Don't Be Satisfied.
RetroTech 팟캐스트 44BITS 팟캐스트

GitHub Copilot을 CLI 명령어에서 도움받을 수 있는 GitHub Copilot in the CLI

GitHub Copilot을 터미널에서 사용할 수 있는 GitHub Copilot in the CLI가 지난달부터 GA, 즉 누구나 이용할 수 있게 공개되었다. 누구나라고는 하지만 GitHub Copilot이 유료 기능이기 때문에 가입자만 유료이긴 하다.

GitHub Copilot in the CLI

GitHub Copilot in the CLI는 말 그대로 터미널에서 GitHub Copilot의 AI 기능을 이용해서 필요한 터미널 명령어의 안내를 받거나 터미널 명령어의 의미를 이해하는 데 도움을 받을 수 있다.

설치

GitHub Copilot CLI를 사용하려면 GitHub의 공식 CLI인 gh를 사용해야 한다. gh에 대한 내용과 설정 방법은 이전 글에서 확인할 수 있다.

gh extension을 이용해서 gh-copilot을 설치할 수 있다.

$ gh extension install github/gh-copilot
✓ Installed extension github/gh-copilot
$ gh extension list
NAME        REPO               VERSION
gh copilot  github/gh-copilot  v1.0.1


github/gh-copilot

익스텐션을 설치했으므로 이제 gh copilot 명령어를 사용할 수 있다.

$ gh copilot
Your AI command line copilot.

Usage:
  copilot [command]

Examples:

$ gh copilot suggest "Install git"
$ gh copilot explain "traceroute github.com"


Available Commands:
  alias       Generate shell-specific aliases for convenience
  config      Configure options
  explain     Explain a command
  suggest     Suggest a command

Flags:
  -h, --help      help for copilot
  -v, --version   version for copilot

Use "copilot [command] --help" for more information about a command.

위에서 보듯이 명령어는 크게 gh copilot suggestgh copilot explain 두 가지로 나눌 수 있다.

gh copilot suggest

gh copilot suggest는 이름 그대로 원하는 작업을 입력하면 명령어를 알려주는 기능이다. 이젠 많은 AI를 써봐서 다들 알다시피 할 때마다 항상 똑같은 명령어를 주는 것도 아니고 항상 올바른 명령어를 알려주는 것은 아니라서 써보면서 잘해주는 걸 파악해서 쓰면서 점점 발전하길 기다려야 하는 것 같다.

이 명령어는 인터렉티브 형식으로 순차적으로 진행되므로 차례대로 보겠다.

$ gh copilot suggest

Welcome to GitHub Copilot in the CLI!
version 1.0.1 (2024-03-22)

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. For more information, see https://gh.io/gh-copilot-transparency

? What kind of command can I help you with?
> generic shell command
  gh command
  git command

이 정도는 자동으로 해주면 좋을 것 같은데 제안할 명령어를 일반적인 쉘 명령어로 알려줄지 ghgit의 명령어로 알려줄지를 먼저 물어본다. 아직 어떤 질문인지 적지도 않았는데 이걸 묻는 건 좀 어색하게 느껴진다.

$ gh copilot suggest

Welcome to GitHub Copilot in the CLI!
version 1.0.1 (2024-03-22)

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. For more information, see https://gh.io/gh-copilot-transparency

? What kind of command can I help you with?
> generic shell command

? What would you like the shell command to do?
> get pending pod list in kubernetes cluster

Suggestion:

  kubectl get pods --field-selector=status.phase==Pending

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

여기서는 Kubernetes에서 pending 팟의 목록을 가져오는 명령어(get pending pod list in kubernetes cluster)를 요구하자 kubectl get pods --field-selector=status.phase==Pending라며 얼추 제대로 된 답변을 준다. CLI 명령어의 경우는 자주 쓰는 명령어나 옵션어가 아니면 항상 헷갈리기 마련이라서 검색해보지 않고 터미널에서 바로 찾을 수 있는 점은 좋아 보인다.

명령어를 제안하고 나면 이를 바로 실행할지 클립보드에 복사할지 등을 선택할 수 있다. AI이기 때문에 한국어도 되긴 하지만 터미널에서는 개인적으로 한국어 전환이 귀찮아서 비영어권 사람으로서는 이런 부분은 참 아쉽다.

gh copilot explain

gh copilot explain은 CLI 명령어를 주면 설명해 주는 명령어다. 특정 명령어가 뭘 하는지 모를 때 사용할 수 있다.

$ gh copilot explain

Welcome to GitHub Copilot in the CLI!
version 1.0.1 (2024-03-22)

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. For more information, see https://gh.io/gh-copilot-transparency

? Which command would you like to explain?
> kubectl get pods --field-selector=status.phase==Pending

Explanation:

  • kubectl get pods is used to retrieve information about pods in a Kubernetes cluster.
    • --field-selector=status.phase==Pending is a field selector flag used to filter 
      the pods based on their status phase. In this case, it filters the pods 
      that are in the "Pending" phase.

앞에서 받은 명령어를 다시 넣어봤는데 꽤 잘 설명해 준다.

$ gh copilot explain

Welcome to GitHub Copilot in the CLI!
version 1.0.1 (2024-03-22)

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. For more information, see https://gh.io/gh-copilot-transparency

? Which command would you like to explain?
> jq '.[] | .name, .city' file.json

Explanation:

  • jq is a command-line tool for processing JSON data.
    • '.[]' selects each element of the JSON array.
    • | .name, .city outputs the value of the name and city fields of each element.
      • .name selects the value of the name field.
      • .city selects the value of the city field.

jq 옵션도 꽤 잘 설명해 준다.

별칭

앞에서 본 것처럼 명령어가 꽤 길어서 사용하기가 좀 귀찮기 때문에 alias를 만들 수 있는 명령어도 같이 제공한다.

$ gh copilot alias

? Which shell to generate aliases for?
> Zsh

gh copilot alias를 입력하고 사용하는 Shell을 선택하면 별칭을 만들 수 있는 쉘 스크립트를 제공해 준다. 이를 쉘 설정의 별칭 설정에 추가해 주면 gh copilot suggestgh copilot explain를 각각 ghcs, ghce로 축약해서 사용할 수 있다.

2024/04/20 22:34 2024/04/20 22:34