Skip to content

Commit 17e2ec1

Browse files
committed
feat: scaffold postgresql toolkit cli
1 parent 1c2272b commit 17e2ec1

6 files changed

Lines changed: 188 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PGHOST=127.0.0.1
2+
PGPORT=5432
3+
PGUSER=postgres
4+
PGPASSWORD=change-me
5+
PGDATABASE=app
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# PostgreSQL Toolkit Source
2+
3+
这个目录存放 PostgreSQL PowerShell CLI 的源码、帮助文档源文件和构建脚本。
4+
5+
## Build
6+
7+
```powershell
8+
pwsh -NoProfile -File ./scripts/pwsh/devops/postgresql/build/Build-PostgresToolkit.ps1
9+
```
10+
11+
## Commands
12+
13+
- `backup`
14+
- `restore`
15+
- `import-csv`
16+
- `install-tools`
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Set-StrictMode -Version Latest
2+
$ErrorActionPreference = 'Stop'
3+
4+
<#
5+
.SYNOPSIS
6+
返回 PostgreSQL Toolkit 的帮助文本。
7+
8+
.DESCRIPTION
9+
统一生成 CLI 使用说明,后续子命令级帮助也从这里扩展,
10+
让控制台帮助与独立帮助文档尽量保持一致。
11+
12+
.PARAMETER CommandName
13+
可选的子命令名称;当前最小实现统一返回总览帮助。
14+
15+
.OUTPUTS
16+
string
17+
返回可直接打印到控制台的帮助文本。
18+
#>
19+
function Get-PostgresToolkitHelpText {
20+
[CmdletBinding()]
21+
param(
22+
[string]$CommandName
23+
)
24+
25+
$defaultHelp = @'
26+
Usage:
27+
./Postgres-Toolkit.ps1 <command> [options]
28+
29+
Commands:
30+
backup
31+
restore
32+
import-csv
33+
install-tools
34+
help
35+
36+
Examples:
37+
./Postgres-Toolkit.ps1 backup --database app --output ./app.dump --format custom
38+
./Postgres-Toolkit.ps1 restore --input ./app.dump --target-database app_restore --clean
39+
./Postgres-Toolkit.ps1 import-csv --input ./users.csv --table users --header
40+
./Postgres-Toolkit.ps1 install-tools --apply
41+
'@
42+
43+
return $defaultHelp.Trim()
44+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Postgres Toolkit Help
2+
3+
## Commands
4+
5+
- `backup`
6+
- `restore`
7+
- `import-csv`
8+
- `install-tools`
9+
10+
## Examples
11+
12+
```powershell
13+
./Postgres-Toolkit.ps1 backup --database app --output ./app.dump --format custom
14+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env pwsh
2+
<#
3+
.SYNOPSIS
4+
PostgreSQL 常用备份、恢复、CSV 导入与工具安装命令行工具。
5+
6+
.DESCRIPTION
7+
当前入口先负责子命令分发与帮助输出,后续命令实现会逐步挂到这里。
8+
9+
.PARAMETER CommandName
10+
要执行的子命令名称,例如 `backup`、`restore`、`help`。
11+
12+
.PARAMETER RawArguments
13+
透传给子命令解析器的剩余参数数组。
14+
#>
15+
[CmdletBinding(PositionalBinding = $false)]
16+
param(
17+
[Parameter(Position = 0)]
18+
[string]$CommandName,
19+
20+
[Parameter(ValueFromRemainingArguments = $true)]
21+
[string[]]$RawArguments
22+
)
23+
24+
Set-StrictMode -Version Latest
25+
$ErrorActionPreference = 'Stop'
26+
27+
<#
28+
.SYNOPSIS
29+
执行 PostgreSQL Toolkit 的顶层命令分发。
30+
31+
.DESCRIPTION
32+
当前最小实现只负责帮助命令与空命令路径,后续再接入具体子命令。
33+
34+
.PARAMETER CommandName
35+
顶层命令名。
36+
37+
.PARAMETER RawArguments
38+
透传的剩余参数。
39+
40+
.OUTPUTS
41+
PSCustomObject
42+
返回包含 `ExitCode` 与 `Output` 的标准执行结果。
43+
#>
44+
function Invoke-PostgresToolkitCommand {
45+
[CmdletBinding()]
46+
param(
47+
[string]$CommandName,
48+
[string[]]$RawArguments
49+
)
50+
51+
if ([string]::IsNullOrWhiteSpace($CommandName) -or $CommandName -eq 'help') {
52+
return [PSCustomObject]@{
53+
ExitCode = 0
54+
Output = Get-PostgresToolkitHelpText
55+
}
56+
}
57+
58+
throw "未知命令: $CommandName"
59+
}
60+
61+
if ($env:PWSH_TEST_SKIP_POSTGRES_TOOLKIT_MAIN -ne '1') {
62+
$result = Invoke-PostgresToolkitCommand -CommandName $CommandName -RawArguments $RawArguments
63+
if (-not [string]::IsNullOrWhiteSpace($result.Output)) {
64+
Write-Output $result.Output
65+
}
66+
67+
exit $result.ExitCode
68+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Set-StrictMode -Version Latest
2+
3+
BeforeAll {
4+
$script:RepoRoot = Join-Path $PSScriptRoot '..'
5+
$env:PWSH_TEST_SKIP_POSTGRES_TOOLKIT_MAIN = '1'
6+
7+
foreach ($relativePath in @(
8+
'scripts/pwsh/devops/postgresql/core/logging.ps1'
9+
'scripts/pwsh/devops/postgresql/core/process.ps1'
10+
'scripts/pwsh/devops/postgresql/core/arguments.ps1'
11+
'scripts/pwsh/devops/postgresql/core/connection.ps1'
12+
'scripts/pwsh/devops/postgresql/core/context.ps1'
13+
'scripts/pwsh/devops/postgresql/core/formats.ps1'
14+
'scripts/pwsh/devops/postgresql/core/validation.ps1'
15+
'scripts/pwsh/devops/postgresql/commands/help.ps1'
16+
'scripts/pwsh/devops/postgresql/main.ps1'
17+
)) {
18+
. (Join-Path $script:RepoRoot $relativePath)
19+
}
20+
}
21+
22+
Describe 'Get-PostgresToolkitHelpText' {
23+
It '输出四个核心命令和示例' {
24+
$helpText = Get-PostgresToolkitHelpText
25+
26+
$helpText | Should -Match 'backup'
27+
$helpText | Should -Match 'restore'
28+
$helpText | Should -Match 'import-csv'
29+
$helpText | Should -Match 'install-tools'
30+
$helpText | Should -Match 'Postgres-Toolkit.ps1 backup'
31+
}
32+
}
33+
34+
Describe 'Invoke-PostgresToolkitCommand' {
35+
It '未传命令时返回帮助文本而不是抛错' {
36+
$result = Invoke-PostgresToolkitCommand -CommandName '' -RawArguments @()
37+
38+
$result.ExitCode | Should -Be 0
39+
$result.Output | Should -Match 'Usage'
40+
}
41+
}

0 commit comments

Comments
 (0)