Skip to content

Commit a233ac6

Browse files
authored
Merge pull request #1 from TNG/feature/prepare-pwsh-gallery
Feature/prepare pwsh gallery
2 parents bbd08ae + f879a6a commit a233ac6

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

PleasePwsh/PleasePwsh.psd1

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
@{
2-
RootModule = 'PleasePwsh.psm1'
3-
ModuleVersion = '0.1'
4-
Author = 'T. Kasper'
5-
Description = 'Translates a prompt into a PowerShell command using OpenAI GPT.'
2+
RootModule = 'PleasePwsh.psm1'
3+
ModuleVersion = '0.1'
4+
GUID = 'd430aac3-8363-4321-a610-ff33a366a50b'
5+
Author = 'Tobias Kasper'
6+
CompanyName = 'TNG Technology Consulting GmbH'
7+
Copyright = '2023 TNG Technology Consulting GmbH. All rights reserved.'
8+
Description = 'Translates a prompt into a PowerShell command using OpenAI GPT.'
9+
PrivateData = @{
10+
PSData = @{
11+
Tags = @('PowerShell', 'GPT', 'OpenAI')
12+
ProjectUri = 'https://github.com/TNG/please-pwsh'
13+
}
14+
}
615
PowerShellVersion = '7.0'
716
FunctionsToExport = 'Please'
8-
AliasesToExport = '*'
17+
AliasesToExport = '*'
918
}

PleasePwsh/PleasePwsh.psm1

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ If this switch is provided, the script will return a brief explanation of the sp
2424
.EXAMPLE
2525
PS> Please "Get all files in current directory that are larger than 1 MB"
2626
27-
PS> Please -Explain "Get-ChildItem -Path 'C:\'"
27+
PS> Please -Explain "Get-ChildItem -Path 'C:\'"
2828
#>
2929
function Please {
3030
[CmdletBinding()]
@@ -33,18 +33,18 @@ function Please {
3333
[Alias("e")][switch]$Explain
3434

3535
)
36-
36+
3737
Test-ApiKey
3838

3939
if ($Explain) {
4040
$Explanation = Get-CommandExplanation $Prompt
41-
Write-Host "`u{261D} $Explanation"
41+
Write-Output "`u{261D} $Explanation"
4242
$Command = $Prompt
4343
}
4444
else {
45-
$Command = Get-Command $Prompt
45+
$Command = Get-PwshCommand $Prompt
4646
if ($Command.contains("I do not know")) {
47-
Write-Host $Command
47+
Write-Output $Command
4848
Return
4949
}
5050
}
@@ -54,7 +54,7 @@ function Please {
5454

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

6060
if ([string]::IsNullOrWhiteSpace($Key)) {
@@ -64,7 +64,7 @@ function Test-ApiKey {
6464
}
6565
}
6666

67-
function Get-Command([string]$Prompt) {
67+
function Get-PwshCommand([string]$Prompt) {
6868
$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'"
6969

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

8181
function Show-Menu($Command) {
8282
$Title = "`u{1F523} Command:`n $Command"
83-
83+
8484
$Question = "`u{2753} What should I do?"
85-
85+
8686
$OptionAbort = [ChoiceDescription]::new('&Abort')
8787
$OptionCopy = [ChoiceDescription]::new('&Copy')
8888
$OptionInvoke = [ChoiceDescription]::new('&Invoke')
@@ -93,21 +93,21 @@ function Show-Menu($Command) {
9393

9494
function Invoke-Action ($Action) {
9595
switch ($Action) {
96-
0 {
97-
Write-Host "`u{274C} Aborting"
96+
0 {
97+
Write-Output "`u{274C} Aborting"
9898
}
9999
1 {
100-
Write-Host "`u{00A9} Copying to clipboard"
100+
Write-Output "`u{00A9} Copying to clipboard"
101101
Set-Clipboard -Value $Command
102102
}
103103
2 {
104-
Write-Host "`u{25B6} Invoking command"
105-
Invoke-Expression $Command
104+
Write-Output "`u{25B6} Invoking command"
105+
Invoke-Expression $Command
106106
}
107107
Default {
108-
Write-Host "Invalid action"
108+
Write-Output "Invalid action"
109109
}
110-
}
110+
}
111111
}
112112

113113
function Get-CommandExplanation([string]$Command) {
@@ -121,7 +121,7 @@ function Get-CommandExplanation([string]$Command) {
121121
)
122122
}
123123

124-
Return Invoke-OpenAIRequest $Payload
124+
Return Invoke-OpenAIRequest $Payload
125125
}
126126

127127
function Invoke-OpenAIRequest($Payload) {
@@ -133,12 +133,12 @@ function Invoke-OpenAIRequest($Payload) {
133133
}
134134

135135
try {
136-
$Response = Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -Body ($Payload | ConvertTo-Json)
136+
$Response = Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -Body ($Payload | ConvertTo-Json)
137137
}
138138
catch {
139139
Write-Error "Received $($_.Exception.Response.ReasonPhrase): $($_.Exception.Response.Content | ConvertTo-Json)"
140140
}
141-
141+
142142
Return $Response.choices[0].message.content
143143
}
144144

Readme.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Please CLI for PowerShell Core
22

3-
An AI helper script to create PowerShell commands. It is a copy of [Please CLI](https://github.com/TNG/please-cli/).
3+
An AI helper script to create PowerShell commands. It is a copy of [Please CLI](https://github.com/TNG/please-cli/).
44

55
The initial approach to `ChatGPT please convert the bash script to pwsh` did not work. So its a rewrite that still contains trace amounts of AI.
66

@@ -38,8 +38,11 @@ You need an [OpenAI API key](https://platform.openai.com/account/api-keys) and a
3838

3939
## Installation
4040

41-
For now copy the folder `PleasePwsh` in one of your module folders and set the OpenAI API key as environment variable.
42-
- Find module folders with `$ENV:PSModulePath -Split ';'`
43-
- Set environment variable permanently in PowerShell profile
44-
- Open profile `Code $PROFILE`
45-
- Add a line `$ENV:OPENAI_API_KEY = <YOUR_API_KEY>`
41+
From the [PowerShell Gallery](https://www.powershellgallery.com/packages/PleasePwsh) with
42+
```
43+
Install-Module -Name PleasePwsh
44+
```
45+
46+
Set the OpenAI API key as environment variable.
47+
- Open PowerShell profile: `Code $PROFILE`
48+
- Add a line with your API key: `$ENV:OPENAI_API_KEY = <YOUR_API_KEY>`

0 commit comments

Comments
 (0)