Skip to content

Instantly share code, notes, and snippets.

@gsusI
Last active May 20, 2023 11:56
Show Gist options
  • Save gsusI/ad3ea21fda265b7a31d27a2246393880 to your computer and use it in GitHub Desktop.
Save gsusI/ad3ea21fda265b7a31d27a2246393880 to your computer and use it in GitHub Desktop.
Powershell script to save prompt and resulting command to history using GitHub Copilot CLI
function Invoke-CopilotWhatTheShell {
$TMPFILE = New-TemporaryFile;
try {
$wts_args = "(using powershell) $args)"
github-copilot-cli what-the-shell $wts_args --shellout $TMPFILE
if ($LASTEXITCODE -eq 0) {
if (Test-Path $TMPFILE) {
$FIXED_CMD = Get-Content $TMPFILE;
Add-Content -Value "$FIXED_CMD" -Path (Get-PSReadLineOption).HistorySavePath;
Invoke-Expression $FIXED_CMD;
}
else {
Write-Host "Apologies! Extracting command failed";
}
}
else {
Write-Error "Apologies! Copilot failed to generate a command";
}
}
finally {
Remove-Item $TMPFILE;
}
}
Set-Alias '??' 'Invoke-CopilotWhatTheShell';
Set-Alias 'wts' 'Invoke-CopilotWhatTheShell';
function Invoke-CopilotGitAssist {
$TMPFILE = New-TemporaryFile;
try {
github-copilot-cli git-assist $args --shellout $TMPFILE
if ($LASTEXITCODE -eq 0) {
if (Test-Path $TMPFILE) {
$FIXED_CMD = Get-Content $TMPFILE;
Add-Content -Value "$FIXED_CMD" -Path (Get-PSReadLineOption).HistorySavePath;
Invoke-Expression $FIXED_CMD;
}
else {
Write-Host "Apologies! Extracting command failed";
}
}
else {
Write-Error "Apologies! Copilot failed to generate a command";
}
}
finally {
Remove-Item $TMPFILE;
}
}
Set-Alias 'git?' 'Invoke-CopilotGitAssist';
function Invoke-CopilotGitHubAssist {
$TMPFILE = New-TemporaryFile;
try {
github-copilot-cli gh-assist $args --shellout $TMPFILE
if ($LASTEXITCODE -eq 0) {
if (Test-Path $TMPFILE) {
$FIXED_CMD = Get-Content $TMPFILE;
Add-Content -Value "$FIXED_CMD" -Path (Get-PSReadLineOption).HistorySavePath;
Invoke-Expression $FIXED_CMD;
}
else {
Write-Host "Apologies! Extracting command failed";
}
}
else {
Write-Error "Apologies! Copilot failed to generate a command";
}
}
finally {
Remove-Item $TMPFILE;
}
}
Set-Alias 'gh?' 'Invoke-CopilotGitHubAssist';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment