DEV Community

Cover image for ใช้ GitHub Copilot สั่งงานผ่านคำสั่งในเทอร์มินัล
Anuchit Prasertsang
Anuchit Prasertsang

Posted on

ใช้ GitHub Copilot สั่งงานผ่านคำสั่งในเทอร์มินัล

ใช้ GitHub Copilot สั่งงานผ่านคำสั่งในเทอร์มินัล

เราสามารถถาม GitHub Copilot ได้ผ่านคำสั่ง gh

เช่น gh copilot suggest "<question>" โดยที่ คือคำถามที่เราต้องการถาม หรือ อยากได้คำแนะนำ

gh copilot suggest "how to show size of folder in linux?"
Enter fullscreen mode Exit fullscreen mode

ผลลัพธ์ที่ได้

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

Suggestion:

  du -sh /path/to/folder

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

เราสามารถ copy คำสั่งได้เลย หรือ จะเลือกให้ ทำการ อธิบาย, รัน, ปรับปรุง คำสั่งได้เลย

สิ่งที่ต้องเตรียม

เราต้องทำการติดตั้งโปรแกรม gh และโปรแกรมเสริม gh-copilot ก่อนใช้งานคำสั่งด้านบน

  1. ติดตั้งโปรแกรม gh GitHub CLI

    1. MacOS โดยใช้คำสั่ง brew install gh
    2. Linux โดยใช้คำสั่ง
    (type -p wget >/dev/null || (sudo apt update && sudo apt-get install wget -y)) \
    && sudo mkdir -p -m 755 /etc/apt/keyrings \
    && wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
    && sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
    && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
    && sudo apt update \
    && sudo apt install gh -y
    
  2. ติดตั้งโปรแกรมเสริม gh extension install github/gh-copilot

เพียงเท่านี้เราก็สามารถใช้งาน Github Copilot ผ่าน Command line ได้แล้ว

ตัวอย่างการใช้งานอื่นๆ

นอกจากจะใช้ GitHub Copilot ในการอธิบาย (explain) คำถามเกี่ยวกับโค้ดแล้ว
เรายังสามารถใช้ Github Copilot ในการอธิบาย คำสั่งที่ยากต่อการเข้าใจ โดยใช้คำสั่ง gh copilot explain
ตัวอย่างเช่น

gh copilot explain "find . -name "node_modules" -type d -prune -exec rm -rf '{}' +"
Enter fullscreen mode Exit fullscreen mode

ผลลัพธ์ที่ได้

Explanation:

  • find . searches for files and directories under the current directory.
    • -name node_modules searches for directories named "node_modules".-type d specifies that we are only interested in directories.
    • -prune stops find from descending into the matched directories.
    • -exec rm -rf '{}' + executes the rm -rf command on each matched directory.
      • rm -rf removes directories and their contents recursively and forcefully.
      • '{}' is a placeholder for the matched directory.
      • + indicates that multiple directories can be passed as arguments to a single rm command.
Enter fullscreen mode Exit fullscreen mode

อ้างอิง:

Top comments (0)