Skip to content

Commit 505ee7d

Browse files
committed
Add -InputObject parameter for smart pipelilne processing.
1 parent 362efa2 commit 505ee7d

1 file changed

Lines changed: 56 additions & 18 deletions

File tree

PSql.Deploy/Commands/GetSqlMigrationsCommand.cs

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,33 @@ namespace PSql.Deploy.Commands;
1414
/// <remarks>
1515
/// Lists database schema migrations.
1616
/// </remarks>
17-
[Cmdlet(VerbsCommon.Get, "SqlMigrations", DefaultParameterSetName = "Path")]
17+
[Cmdlet(VerbsCommon.Get, "SqlMigrations", DefaultParameterSetName = nameof(InputObject))]
1818
[OutputType(typeof(Migration))]
1919
public sealed class GetSqlMigrationsCommand : AsyncPSCmdlet
2020
{
2121
/// <summary>
22-
/// <b>-Path:</b> Path to a directory containing database source code in
23-
/// the layout expected by PSql.Deploy. The default is the current
24-
/// directory.
22+
/// <b>-InputObject:</b> Any object accepted by the <c>-Path</c> or the
23+
/// <c>-Target</c> parameter. The cmdlet determines which parameter to
24+
/// use.
2525
/// </summary>
26+
/// <remarks>
27+
/// string | SqlContext | SqlTargetDatabase
28+
/// </remarks>
2629
[Parameter(
27-
ParameterSetName = "Path",
28-
Mandatory = false,
30+
ParameterSetName = nameof(InputObject),
2931
Position = 0,
32+
Mandatory = false,
3033
ValueFromPipeline = true
3134
)]
35+
[ValidateNotNullOrEmpty]
36+
public object? InputObject { get; set; }
37+
38+
/// <summary>
39+
/// <b>-Path:</b> Path to a directory containing database source code in
40+
/// the layout expected by PSql.Deploy. The default is the current
41+
/// directory.
42+
/// </summary>
43+
[Parameter(ParameterSetName = nameof(Path), Mandatory = true)]
3244
[Alias("PSPath")]
3345
[ValidateNotNullOrEmpty]
3446
public string? Path { get; set; }
@@ -47,12 +59,7 @@ public sealed class GetSqlMigrationsCommand : AsyncPSCmdlet
4759
/// <remarks>
4860
/// string | SqlContext | SqlTargetDatabase
4961
/// </remarks>
50-
[Parameter(
51-
ParameterSetName = "Target",
52-
Mandatory = true,
53-
Position = 0,
54-
ValueFromPipeline = true
55-
)]
62+
[Parameter(ParameterSetName = nameof(Target), Mandatory = true)]
5663
[ValidateNotNullOrEmpty]
5764
public SqlTargetDatabase? Target { get; set; }
5865

@@ -61,7 +68,8 @@ public sealed class GetSqlMigrationsCommand : AsyncPSCmdlet
6168
/// <see langword="null"/> to to return all migrations registered on the
6269
/// target database. The default is <see langword="null"/>.
6370
/// </summary>
64-
[Parameter(ParameterSetName = "Target")]
71+
[Parameter(ParameterSetName = nameof(InputObject))]
72+
[Parameter(ParameterSetName = nameof(Target))]
6573
[AllowNull, AllowEmptyString]
6674
public string? MinimumName { get; set; }
6775

@@ -73,11 +81,9 @@ protected override void ProcessRecord()
7381

7482
private async Task ProcessRecordAsync()
7583
{
76-
var migrations = ParameterSetName switch
77-
{
78-
"Path" => GetMigrations(Path),
79-
_ => await GetMigrationsAsync(Target!),
80-
};
84+
var migrations = IsForTarget()
85+
? await GetMigrationsAsync(Target)
86+
: GetMigrations(Path);
8187

8288
#if INCLUDE_CONTENT
8389
if (IncludeContent.IsPresent)
@@ -88,6 +94,38 @@ private async Task ProcessRecordAsync()
8894
WriteObject(new Migration(migration));
8995
}
9096

97+
[MemberNotNullWhen(true, nameof(Target))]
98+
private bool IsForTarget()
99+
{
100+
while (InputObject is PSObject psObject)
101+
InputObject = psObject.BaseObject;
102+
103+
switch (ParameterSetName)
104+
{
105+
case nameof(Path):
106+
return false;
107+
108+
case nameof(Target):
109+
Assume.NotNull(Target);
110+
return true;
111+
112+
case nameof(InputObject) when InputObject is null:
113+
return false;
114+
115+
case nameof(InputObject) when InputObject is string path && !path.Contains(';'):
116+
Path = path;
117+
return false;
118+
119+
case nameof(InputObject) when InputObject is SqlTargetDatabase target:
120+
Target = target;
121+
return true;
122+
123+
default:
124+
Target = new SqlTargetDatabase(InputObject!);
125+
return true;
126+
}
127+
}
128+
91129
private IReadOnlyList<M.Migration> GetMigrations(string? path)
92130
{
93131
path = this.GetFullPath(path);

0 commit comments

Comments
 (0)