Skip to content

Instantly share code, notes, and snippets.

@hermanussen
Last active July 17, 2024 07:10
Show Gist options
  • Save hermanussen/36d72f4d216b2caa9ca699aa98cf44b9 to your computer and use it in GitHub Desktop.
Save hermanussen/36d72f4d216b2caa9ca699aa98cf44b9 to your computer and use it in GitHub Desktop.
Get GitHub Copilot for CLI in Powershell by adding the contents here to your $Profile. Ensure that you have installed and authenticated in the GitHub CLI to make it work properly.
function ghcs {
# Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
param(
[Parameter()]
[string]$Hostname,
[ValidateSet('gh', 'git', 'shell')]
[Alias('t')]
[String]$Target = 'shell',
[Parameter(Position=0, ValueFromRemainingArguments)]
[string]$Prompt
)
begin {
# Create temporary file to store potential command user wants to execute when exiting
$executeCommandFile = New-TemporaryFile
# Store original value of GH_* environment variable
$envGhDebug = $Env:GH_DEBUG
$envGhHost = $Env:GH_HOST
}
process {
if ($PSBoundParameters['Debug']) {
$Env:GH_DEBUG = 'api'
}
$Env:GH_HOST = $Hostname
gh copilot suggest -t $Target -s "$executeCommandFile" $Prompt
}
end {
# Execute command contained within temporary file if it is not empty
if ($executeCommandFile.Length -gt 0) {
# Extract command to execute from temporary file
$executeCommand = (Get-Content -Path $executeCommandFile -Raw).Trim()
# Insert command into PowerShell up/down arrow key history
[Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($executeCommand)
# Insert command into PowerShell history
$now = Get-Date
$executeCommandHistoryItem = [PSCustomObject]@{
CommandLine = $executeCommand
ExecutionStatus = [Management.Automation.Runspaces.PipelineState]::NotStarted
StartExecutionTime = $now
EndExecutionTime = $now.AddSeconds(1)
}
Add-History -InputObject $executeCommandHistoryItem
# Execute command
Write-Host "`n"
Invoke-Expression $executeCommand
}
}
clean {
# Clean up temporary file used to store potential command user wants to execute when exiting
Remove-Item -Path $executeCommandFile
# Restore GH_* environment variables to their original value
$Env:GH_DEBUG = $envGhDebug
}
}
function ghce {
# Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
param(
[Parameter()]
[string]$Hostname,
[Parameter(Position=0, ValueFromRemainingArguments)]
[string[]]$Prompt
)
begin {
# Store original value of GH_* environment variables
$envGhDebug = $Env:GH_DEBUG
$envGhHost = $Env:GH_HOST
}
process {
if ($PSBoundParameters['Debug']) {
$Env:GH_DEBUG = 'api'
}
$Env:GH_HOST = $Hostname
gh copilot explain $Prompt
}
clean {
# Restore GH_* environment variables to their original value
$Env:GH_DEBUG = $envGhDebug
$Env:GH_HOST = $envGhHost
}
}
Function GhCopilotSuggestShell {
gh copilot suggest -t shell "$args"
}
Function GhCopilotSuggestPowerShell {
gh copilot suggest -t shell "$args using Windows Powershell"
}
Function GhCopilotSuggestPowerShellCore {
gh copilot suggest -t shell "$args using Powershell Core (pswh)"
}
Function GhCopilotSuggestGit {
gh copilot suggest -t git "$args"
}
Function GhCopilotSuggestGitHub {
gh copilot suggest -t gh "$args"
}
Function GhCopilotOops {
gh copilot suggest -t shell "I am using Powershell Core (pswh) and I tried running the following command but it failed to do what I wanted or gave an error. Please correct any typos or errors. This is the command: $(Get-History -Count 2 | Select-Object -ExpandProperty CommandLine -Last 1)"
}
# By default use Powershell Core
Set-Alias ?? GhCopilotSuggestPowerShellCore
# Explicit calls for Powershell related stuff
Set-Alias pwsh? GhCopilotSuggestPowerShellCore
Set-Alias powershell? GhCopilotSuggestPowerShell
# Regular supported Copilot targets ("gh copilot suggest --help" for more info)
Set-Alias shell? GhCopilotSuggestShell
Set-Alias git? GhCopilotSuggestGit
Set-Alias gh? GhCopilotSuggestGitHub
# Register the oops command to fix a previous command (inspired by https://github.com/nvbn/thefuck)
Set-Alias oops GhCopilotOops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment