Skip to content

Commit

Permalink
Prepare for PowerShell Gallery
Browse files Browse the repository at this point in the history
* Extend manifest
* Apply ScriptAnalyzer results

Signed-off-by: Tobias Kasper <[email protected]>
  • Loading branch information
kaspertng committed Apr 8, 2023
1 parent bbd08ae commit af62a76
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
19 changes: 14 additions & 5 deletions PleasePwsh/PleasePwsh.psd1
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
@{
RootModule = 'PleasePwsh.psm1'
ModuleVersion = '0.1'
Author = 'T. Kasper'
Description = 'Translates a prompt into a PowerShell command using OpenAI GPT.'
RootModule = 'PleasePwsh.psm1'
ModuleVersion = '0.1'
GUID = 'd430aac3-8363-4321-a610-ff33a366a50b'
Author = 'Tobias Kasper'
CompanyName = 'TNG Technology Consulting GmbH'
Copyright = '2023 TNG Technology Consulting GmbH. All rights reserved.'
Description = 'Translates a prompt into a PowerShell command using OpenAI GPT.'
PrivateData = @{
PSData = @{
Tags = @('PowerShell', 'GPT', 'OpenAI')
ProjectUri = 'https://github.com/TNG/please-pwsh'
}
}
PowerShellVersion = '7.0'
FunctionsToExport = 'Please'
AliasesToExport = '*'
AliasesToExport = '*'
}
38 changes: 19 additions & 19 deletions PleasePwsh/PleasePwsh.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If this switch is provided, the script will return a brief explanation of the sp
.EXAMPLE
PS> Please "Get all files in current directory that are larger than 1 MB"
PS> Please -Explain "Get-ChildItem -Path 'C:\'"
PS> Please -Explain "Get-ChildItem -Path 'C:\'"
#>
function Please {
[CmdletBinding()]
Expand All @@ -33,18 +33,18 @@ function Please {
[Alias("e")][switch]$Explain

)

Test-ApiKey

if ($Explain) {
$Explanation = Get-CommandExplanation $Prompt
Write-Host "`u{261D} $Explanation"
Write-Output "`u{261D} $Explanation"
$Command = $Prompt
}
else {
$Command = Get-Command $Prompt
$Command = Get-PwshCommand $Prompt
if ($Command.contains("I do not know")) {
Write-Host $Command
Write-Output $Command
Return
}
}
Expand All @@ -54,7 +54,7 @@ function Please {

function Test-ApiKey {
if ($null -eq $env:OPENAI_API_KEY) {
Write-Host "`u{1F50E} Api key missing. See https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key"
Write-Output "`u{1F50E} Api key missing. See https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key"
$Key = Read-Host "Please provide the api key"

if ([string]::IsNullOrWhiteSpace($Key)) {
Expand All @@ -64,7 +64,7 @@ function Test-ApiKey {
}
}

function Get-Command([string]$Prompt) {
function Get-PwshCommand([string]$Prompt) {
$Role = "You translate the input given into PowerShell command. You may not use natural language, but only a PowerShell commands as answer. Do not use markdown. Do not quote the whole output. If you do not know the answer, answer only with 'I do not know'"

$Payload = @{
Expand All @@ -80,9 +80,9 @@ function Get-Command([string]$Prompt) {

function Show-Menu($Command) {
$Title = "`u{1F523} Command:`n $Command"

$Question = "`u{2753} What should I do?"

$OptionAbort = [ChoiceDescription]::new('&Abort')
$OptionCopy = [ChoiceDescription]::new('&Copy')
$OptionInvoke = [ChoiceDescription]::new('&Invoke')
Expand All @@ -93,21 +93,21 @@ function Show-Menu($Command) {

function Invoke-Action ($Action) {
switch ($Action) {
0 {
Write-Host "`u{274C} Aborting"
0 {
Write-Output "`u{274C} Aborting"
}
1 {
Write-Host "`u{00A9} Copying to clipboard"
Write-Output "`u{00A9} Copying to clipboard"
Set-Clipboard -Value $Command
}
2 {
Write-Host "`u{25B6} Invoking command"
Invoke-Expression $Command
Write-Output "`u{25B6} Invoking command"
Invoke-Expression $Command
}
Default {
Write-Host "Invalid action"
Write-Output "Invalid action"
}
}
}
}

function Get-CommandExplanation([string]$Command) {
Expand All @@ -121,7 +121,7 @@ function Get-CommandExplanation([string]$Command) {
)
}

Return Invoke-OpenAIRequest $Payload
Return Invoke-OpenAIRequest $Payload
}

function Invoke-OpenAIRequest($Payload) {
Expand All @@ -133,12 +133,12 @@ function Invoke-OpenAIRequest($Payload) {
}

try {
$Response = Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -Body ($Payload | ConvertTo-Json)
$Response = Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -Body ($Payload | ConvertTo-Json)
}
catch {
Write-Error "Received $($_.Exception.Response.ReasonPhrase): $($_.Exception.Response.Content | ConvertTo-Json)"
}

Return $Response.choices[0].message.content
}

Expand Down

0 comments on commit af62a76

Please sign in to comment.