From d36e27387eabf88845d364b9d3a2da54a583247f Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Mon, 15 Jun 2026 12:54:44 +1000 Subject: v0.18a --- AddImplicitTag.cs | 21 ++++-- AddMediaTag.cs | 19 +++-- ConnectSession.cs | 2 + DisconnectSession.cs | 18 +++++ GetIngestStatistics.cs | 11 +++ GetMedia.cs | 10 +-- GetMediaTag.cs | 8 +- GetTagDefinition.cs | 8 +- GetUploadedFile.cs | 8 +- GetUser.cs | 8 +- HyperBooru.format.ps1xml | 187 +++++++++++++++++++++++++++++++++++++++++++++++ HyperBooru.psd1 | 3 +- NewTagDefinition.cs | 8 +- NewUser.cs | 8 +- PowerShell.csproj | 5 +- PublishMedia.cs | 8 +- RemoveImplicitTag.cs | 22 ++++-- RemoveMediaTag.cs | 19 +++-- RemoveTagDefinition.cs | 13 ++-- RemoveUser.cs | 10 +-- SessionCmdlet.cs | 16 ++++ SetImplicitTag.cs | 21 ++++-- SetMediaTag.cs | 19 +++-- SetTagDefinition.cs | 8 +- SetUser.cs | 8 +- 25 files changed, 351 insertions(+), 117 deletions(-) create mode 100644 DisconnectSession.cs create mode 100644 GetIngestStatistics.cs create mode 100644 HyperBooru.format.ps1xml create mode 100644 SessionCmdlet.cs diff --git a/AddImplicitTag.cs b/AddImplicitTag.cs index 50f6cfc..db2d0bf 100644 --- a/AddImplicitTag.cs +++ b/AddImplicitTag.cs @@ -1,17 +1,24 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("ahbit")] [Cmdlet(VerbsCommon.Add, "HyperBooruImplicitTag")] -public class AddImplicitTagCmdlet : PSCmdlet { - [Parameter(Mandatory = true)] public required HBSession Session { get; set; } - [Parameter(Mandatory = true)] public required Guid TagDefinitionId { get; set; } - [Parameter(Mandatory = true)] public required Guid[] ImplicitTagId { get; set; } +public class AddImplicitTagCmdlet : SessionCmdlet { + [Parameter(Mandatory = true)] public required Guid TagDefinitionId { get; set; } + [Parameter(Mandatory = true)] public required Guid[] ImplicitTagId { get; set; } - protected override void ProcessRecord() => + protected override void ProcessRecord() { Session.Tag.AddImplicitTagsAsync(TagDefinitionId, ImplicitTagId) .GetAwaiter() .GetResult(); + + var id = TagDefinitionId.ToString().ToUpper(); + var impl = ImplicitTagId + .Order() + .Distinct() + .Select(t => t.ToString().ToUpper()); + + WriteVerbose($"Added implicit tags {string.Join(", ", impl)} to tag {id}"); + } } diff --git a/AddMediaTag.cs b/AddMediaTag.cs index 35541e4..2ced119 100644 --- a/AddMediaTag.cs +++ b/AddMediaTag.cs @@ -1,22 +1,27 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("ahbmt")] [Cmdlet(VerbsCommon.Add, "HyperBooruMediaTag")] -public class AddMediaTagCmdlet : PSCmdlet { - [Parameter(Position = 0, Mandatory = true)] - public required HBSession Session { get; set; } - +public class AddMediaTagCmdlet : SessionCmdlet { [Parameter(Position = 1, Mandatory = true)] public Guid MediaId { get; set; } [Parameter(Position = 2, Mandatory = true)] public Guid[] TagId { get; set; } - protected override void ProcessRecord() => + protected override void ProcessRecord() { Session.Media.AddTagsAsync(MediaId, TagId) .GetAwaiter() .GetResult(); + + var id = MediaId.ToString().ToUpper(); + var tags = TagId + .Order() + .Distinct() + .Select(t => t.ToString().ToUpper()); + + WriteVerbose($"Added tags {string.Join(", ", tags)} to media item {id}"); + } } diff --git a/ConnectSession.cs b/ConnectSession.cs index 7590408..e8034fc 100644 --- a/ConnectSession.cs +++ b/ConnectSession.cs @@ -30,6 +30,8 @@ public class ConnectSessionCmdlet : PSCmdlet { targetObject: null)); } + SessionCmdlet.GlobalSession = session; + WriteVerbose($"Successfully logged in to {Host}"); WriteObject(session); } diff --git a/DisconnectSession.cs b/DisconnectSession.cs new file mode 100644 index 0000000..eccd9fe --- /dev/null +++ b/DisconnectSession.cs @@ -0,0 +1,18 @@ +using System.Management.Automation; + +namespace HyperBooru.PowerShell; + +[Alias("dchbs")] +[Cmdlet(VerbsCommunications.Disconnect, "HyperBooruSession")] +public class DisconnectSessionCmdlet : PSCmdlet { + protected override void BeginProcessing() { + var baseUri = SessionCmdlet.GlobalSession?.BaseUri; + + SessionCmdlet.GlobalSession = null; + + if(baseUri is not null) + WriteVerbose($"Successfully disconnected from {baseUri}"); + else + WriteVerbose($"No active sessions - nothing to disconnect"); + } +} diff --git a/GetIngestStatistics.cs b/GetIngestStatistics.cs new file mode 100644 index 0000000..0a61c1f --- /dev/null +++ b/GetIngestStatistics.cs @@ -0,0 +1,11 @@ +using System.Management.Automation; + +namespace HyperBooru.PowerShell; + +[Alias("ghbis")] +[Cmdlet(VerbsCommon.Get, "HyperBooruIngestStatistics")] +public class GetIngestStatisticsCmdlet : SessionCmdlet { + protected override void ProcessRecord() => + WriteObject( + Session.Statistics.GetIngestStatisticsAsync().GetAwaiter().GetResult()); +} diff --git a/GetMedia.cs b/GetMedia.cs index b01f38e..e45ae3a 100644 --- a/GetMedia.cs +++ b/GetMedia.cs @@ -1,15 +1,11 @@ -using HyperBooru.ApiClient; -using HyperBooru.ApiModels; +using HyperBooru.ApiModels; using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("ghbm")] [Cmdlet(VerbsCommon.Get, "HyperBooruMedia")] -public class GetMediaCmdlet : PSCmdlet { - [Parameter(Mandatory = true)] - public HBSession Session { get; set; } - +public class GetMediaCmdlet : SessionCmdlet { [Parameter] public SwitchParameter SelectIngest { get; set; } [Parameter] @@ -19,7 +15,7 @@ public class GetMediaCmdlet : PSCmdlet { [Parameter] public int Count { get; set; } = 50; [Parameter] - public ApiModels.SortOrder SortOrder { get; set; } + public SortOrder SortOrder { get; set; } [Parameter(ParameterSetName = "All", Mandatory = true)] public SwitchParameter All { get; set; } [Parameter(ParameterSetName = "TagId", Mandatory = true)] diff --git a/GetMediaTag.cs b/GetMediaTag.cs index 90d9835..638a85c 100644 --- a/GetMediaTag.cs +++ b/GetMediaTag.cs @@ -1,14 +1,10 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("ghbmt")] [Cmdlet(VerbsCommon.Get, "HyperBooruMediaTag")] -public class GetMediaTagCmdlet : PSCmdlet { - [Parameter(Position = 0, Mandatory = true)] - public required HBSession Session { get; set; } - +public class GetMediaTagCmdlet : SessionCmdlet { [Parameter(Position = 1, Mandatory = true)] public Guid MediaId { get; set; } diff --git a/GetTagDefinition.cs b/GetTagDefinition.cs index 9d33366..916f1b7 100644 --- a/GetTagDefinition.cs +++ b/GetTagDefinition.cs @@ -1,15 +1,11 @@ -using HyperBooru.ApiClient; -using HyperBooru.ApiModels; +using HyperBooru.ApiModels; using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("ghbtd")] [Cmdlet(VerbsCommon.Get, "HyperBooruTagDefinition")] -public class GetTagDefinitionCmdlet : PSCmdlet { - [Parameter(Position = 0, Mandatory = true)] - public HBSession Session { get; set; } - +public class GetTagDefinitionCmdlet : SessionCmdlet { [Parameter(Position = 1, Mandatory = true, ParameterSetName = "AllTags")] public SwitchParameter All { get; set; } diff --git a/GetUploadedFile.cs b/GetUploadedFile.cs index ce9c7d9..ef77765 100644 --- a/GetUploadedFile.cs +++ b/GetUploadedFile.cs @@ -1,14 +1,10 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("ghbuf")] [Cmdlet(VerbsCommon.Get, "HyperBooruUploadedFile")] -public class GetUploadedFileCmdlet : PSCmdlet { - [Parameter(Position = 0, Mandatory = true)] - public HBSession Session { get; set; } - +public class GetUploadedFileCmdlet : SessionCmdlet { [Parameter(Position = 1, Mandatory = true)] public Guid MediaId { get; set; } diff --git a/GetUser.cs b/GetUser.cs index 63c5af7..d190f2c 100644 --- a/GetUser.cs +++ b/GetUser.cs @@ -1,14 +1,10 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("ghbu")] [Cmdlet(VerbsCommon.Get, "HyperBooruUser")] -public class GetUserCmdlet : PSCmdlet { - [Parameter(Position = 0, Mandatory = true)] - public HBSession Session { get; set; } - +public class GetUserCmdlet : SessionCmdlet { [Parameter(Position = 1)] public Guid? UserId { get; set; } diff --git a/HyperBooru.format.ps1xml b/HyperBooru.format.ps1xml new file mode 100644 index 0000000..ab46bf5 --- /dev/null +++ b/HyperBooru.format.ps1xml @@ -0,0 +1,187 @@ + + + + + + HBSessionDefinitionDefaultView + + HyperBooru.ApiClient.HBSession + + + + + + + + + + + + BaseUri + + + + + + + + + TagDefinitionDefaultView + + HyperBooru.ApiModels.TagDefinition + + + + + + + + + + + + + + + + + + + + + TagDefinitionId + + + Alias + + + Namespace + + + Name + + + + + + + + + MediaDefaultView + + HyperBooru.ApiModels.Media + + + + + + + + + + + + + + + + + + MediaId + + + ShortDescription + + + LongDescription + + + + + + + + + UserDefaultView + + HyperBooru.ApiModels.User + + + + + + + + + + + + + + + UserId + + + Username + + + + + + + + + UploadedFileDefaultView + + HyperBooru.ApiModels.UploadedFile + + + + + + + + + + + + + + + + + + + + + + + + + + + UploadedFileId + + + MediaId + + + Checksum + + + Path + + + Filename + + + Length + + + + + + + + + diff --git a/HyperBooru.psd1 b/HyperBooru.psd1 index 63ac453..f0f0605 100644 --- a/HyperBooru.psd1 +++ b/HyperBooru.psd1 @@ -1,6 +1,6 @@ @{ RootModule = 'HyperBooru.PowerShell.dll' - ModuleVersion = '0.17' + ModuleVersion = '0.18' CompatiblePSEditions = 'Core' PowershellVersion = '5.1' GUID = '627c3cbf-7fcc-41b3-9716-610b81ef1032' @@ -11,6 +11,7 @@ ProcessorArchitecture = 'MSIL' FunctionsToExport = '*' VariablesToExport = '*' + FormatsToProcess = 'HyperBooru.format.ps1xml' PrivateData = @{ PSData = @{ LicenseUri = 'https://www.gnu.org/licenses/gpl-3.0.en.html' diff --git a/NewTagDefinition.cs b/NewTagDefinition.cs index c1fd971..dade4ba 100644 --- a/NewTagDefinition.cs +++ b/NewTagDefinition.cs @@ -1,15 +1,11 @@ -using HyperBooru.ApiClient; -using HyperBooru.ApiModels; +using HyperBooru.ApiModels; using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("nhbtd")] [Cmdlet(VerbsCommon.New, "HyperBooruTagDefinition")] -public class NewTagDefinitionCmdlet : PSCmdlet { - [Parameter(Mandatory = true)] - public required HBSession Session { get; set; } - +public class NewTagDefinitionCmdlet : SessionCmdlet { [Parameter] public string? Namespace { get; set; } [Parameter(Mandatory = true)] public required string Name { get; set; } [Parameter] public string? Alias { get; set; } diff --git a/NewUser.cs b/NewUser.cs index 7b9ab07..323fdef 100644 --- a/NewUser.cs +++ b/NewUser.cs @@ -1,15 +1,11 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; using System.Security; namespace HyperBooru.PowerShell; [Alias("nhbu")] [Cmdlet(VerbsCommon.New, "HyperBooruUser")] -public class NewUserCmdlet : PSCmdlet { - [Parameter(Position = 0, Mandatory = true)] - public HBSession Session { get; set; } - +public class NewUserCmdlet : SessionCmdlet { [Parameter(Position = 1, Mandatory = true)] public string Username { get; set; } diff --git a/PowerShell.csproj b/PowerShell.csproj index 241f948..4ddcb95 100644 --- a/PowerShell.csproj +++ b/PowerShell.csproj @@ -9,7 +9,7 @@ $(AssemblyVersion) HyperBooru.PowerShell Jake Mannens - 0.17-alpha + 0.18-alpha @@ -25,6 +25,9 @@ + + Always + Always diff --git a/PublishMedia.cs b/PublishMedia.cs index 523b00f..003b48a 100644 --- a/PublishMedia.cs +++ b/PublishMedia.cs @@ -1,14 +1,10 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("pbhbm")] [Cmdlet(VerbsData.Publish, "HyperBooruMedia")] -public class PublishMediaCmdlet : PSCmdlet { - [Parameter(Position = 0, Mandatory = true)] - public HBSession Session { get; set; } - +public class PublishMediaCmdlet : SessionCmdlet { [Parameter(Position = 1, Mandatory = true, ValueFromPipeline = true)] public string Path { get; set; } diff --git a/RemoveImplicitTag.cs b/RemoveImplicitTag.cs index 7d156ab..a76c54f 100644 --- a/RemoveImplicitTag.cs +++ b/RemoveImplicitTag.cs @@ -1,17 +1,25 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("rhbit")] [Cmdlet(VerbsCommon.Remove, "HyperBooruImplicitTag")] -public class RemoveImplicitTagCmdlet : PSCmdlet { - [Parameter(Mandatory = true)] public required HBSession Session { get; set; } - [Parameter(Mandatory = true)] public required Guid TagDefinitionId { get; set; } - [Parameter(Mandatory = true)] public required Guid[] ImplicitTagId { get; set; } +public class RemoveImplicitTagCmdlet : SessionCmdlet { + [Parameter(Mandatory = true)] public required Guid TagDefinitionId { get; set; } + [Parameter(Mandatory = true)] public required Guid[] ImplicitTagId { get; set; } - protected override void ProcessRecord() => + protected override void ProcessRecord() { Session.Tag.DeleteImplicitTagAsync(TagDefinitionId, ImplicitTagId) .GetAwaiter() .GetResult(); + + var id = TagDefinitionId.ToString().ToUpper(); + var impl = ImplicitTagId + .Order() + .Distinct() + .Select(t => t.ToString().ToUpper()); + + WriteVerbose( + $"Removed implicit tags {string.Join(", ", impl)} from tag {id}"); + } } diff --git a/RemoveMediaTag.cs b/RemoveMediaTag.cs index d41980d..de10611 100644 --- a/RemoveMediaTag.cs +++ b/RemoveMediaTag.cs @@ -1,22 +1,27 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("rhbmt")] [Cmdlet(VerbsCommon.Remove, "HyperBooruMediaTag")] -public class RemoveMediaTagCmdlet : PSCmdlet { - [Parameter(Position = 0, Mandatory = true)] - public required HBSession Session { get; set; } - +public class RemoveMediaTagCmdlet : SessionCmdlet { [Parameter(Position = 1, Mandatory = true)] public Guid MediaId { get; set; } [Parameter(Position = 2, Mandatory = true)] public Guid[] TagId { get; set; } - protected override void ProcessRecord() => + protected override void ProcessRecord() { Session.Media.DeleteTagsAsync(MediaId, TagId) .GetAwaiter() .GetResult(); + + var id = MediaId.ToString().ToUpper(); + var tags = TagId + .Order() + .Distinct() + .Select(t => t.ToString().ToUpper()); + + WriteVerbose($"Removed tags {string.Join(", ", tags)} from media item {id}"); + } } diff --git a/RemoveTagDefinition.cs b/RemoveTagDefinition.cs index e28d7f9..b0d4a11 100644 --- a/RemoveTagDefinition.cs +++ b/RemoveTagDefinition.cs @@ -1,16 +1,17 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("rhbtd")] [Cmdlet(VerbsCommon.Remove, "HyperBooruTagDefinition")] -public class RemoveTagDefinitionCmdlet : PSCmdlet { - [Parameter(Mandatory = true)] public required HBSession Session { get; set; } - [Parameter(Mandatory = true)] public required Guid TagDefinitionId { get; set; } +public class RemoveTagDefinitionCmdlet : SessionCmdlet { + [Parameter(Mandatory = true)] public required Guid TagDefinitionId { get; set; } - protected override void ProcessRecord() => + protected override void ProcessRecord() { Session.Tag.DeleteTagDefinitionAsync(TagDefinitionId) .GetAwaiter() .GetResult(); + + WriteVerbose($"Removed tag definition {TagDefinitionId.ToString().ToUpper()}"); + } } diff --git a/RemoveUser.cs b/RemoveUser.cs index 0e28675..5251103 100644 --- a/RemoveUser.cs +++ b/RemoveUser.cs @@ -1,14 +1,10 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("rhbu")] [Cmdlet(VerbsCommon.Remove, "HyperBooruUser")] -public class RemoveUserCmdlet : PSCmdlet { - [Parameter(Position = 0, Mandatory = true)] - public HBSession Session { get; set; } - +public class RemoveUserCmdlet : SessionCmdlet { [Parameter(Position = 1, Mandatory = true)] public Guid UserId { get; set; } @@ -16,5 +12,7 @@ public class RemoveUserCmdlet : PSCmdlet { Session.User.DeleteUserAsync(UserId) .GetAwaiter() .GetResult(); + + WriteVerbose($"Removed user {UserId.ToString().ToUpper()}"); } } diff --git a/SessionCmdlet.cs b/SessionCmdlet.cs new file mode 100644 index 0000000..d823383 --- /dev/null +++ b/SessionCmdlet.cs @@ -0,0 +1,16 @@ +using HyperBooru.ApiClient; +using System.Management.Automation; + +namespace HyperBooru.PowerShell; + +public abstract class SessionCmdlet : PSCmdlet { + [Parameter(Position = 0)] + public HBSession Session { + get => session ?? GlobalSession!; + set => session = value; + } + + internal static HBSession? GlobalSession { get; set; } + + private HBSession? session = null; +} diff --git a/SetImplicitTag.cs b/SetImplicitTag.cs index 8d11f1c..bad10d1 100644 --- a/SetImplicitTag.cs +++ b/SetImplicitTag.cs @@ -1,17 +1,24 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("shbit")] [Cmdlet(VerbsCommon.Set, "HyperBooruImplicitTag")] -public class SetImplicitTagCmdlet : PSCmdlet { - [Parameter(Mandatory = true)] public required HBSession Session { get; set; } - [Parameter(Mandatory = true)] public required Guid TagDefinitionId { get; set; } - [Parameter(Mandatory = true)] public required Guid[] ImplicitTagId { get; set; } +public class SetImplicitTagCmdlet : SessionCmdlet { + [Parameter(Mandatory = true)] public required Guid TagDefinitionId { get; set; } + [Parameter(Mandatory = true)] public required Guid[] ImplicitTagId { get; set; } - protected override void ProcessRecord() => + protected override void ProcessRecord() { Session.Tag.ReplaceImplicitTagsAsync(TagDefinitionId, ImplicitTagId) .GetAwaiter() .GetResult(); + + var id = TagDefinitionId.ToString().ToUpper(); + var impl = ImplicitTagId + .Order() + .Distinct() + .Select(t => t.ToString().ToUpper()); + + WriteVerbose($"Set implicit tags for {id} to {string.Join(", ", impl)}"); + } } diff --git a/SetMediaTag.cs b/SetMediaTag.cs index 8931991..dc2bbd7 100644 --- a/SetMediaTag.cs +++ b/SetMediaTag.cs @@ -1,22 +1,27 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("shbmt")] [Cmdlet(VerbsCommon.Set, "HyperBooruMediaTag")] -public class SetMediaTagCmdlet : PSCmdlet { - [Parameter(Position = 0, Mandatory = true)] - public required HBSession Session { get; set; } - +public class SetMediaTagCmdlet : SessionCmdlet { [Parameter(Position = 1, Mandatory = true)] public Guid MediaId { get; set; } [Parameter(Position = 2, Mandatory = true)] public Guid[] TagId { get; set; } - protected override void ProcessRecord() => + protected override void ProcessRecord() { Session.Media.ReplaceTagsAsync(MediaId, TagId) .GetAwaiter() .GetResult(); + + var id = MediaId.ToString().ToUpper(); + var tags = TagId + .Order() + .Distinct() + .Select(t => t.ToString().ToUpper()); + + WriteVerbose($"Set tags for media item {id} to {string.Join(", ", tags)}"); + } } diff --git a/SetTagDefinition.cs b/SetTagDefinition.cs index 51a18f4..b1ef14b 100644 --- a/SetTagDefinition.cs +++ b/SetTagDefinition.cs @@ -1,15 +1,11 @@ -using HyperBooru.ApiClient; -using HyperBooru.ApiModels; +using HyperBooru.ApiModels; using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("shbtd")] [Cmdlet(VerbsCommon.Set, "HyperBooruTagDefinition")] -public class SetTagDefinitionCmdlet : PSCmdlet { - [Parameter(Mandatory = true)] - public required HBSession Session { get; set; } - +public class SetTagDefinitionCmdlet : SessionCmdlet { [Parameter(Mandatory = true)] public required Guid TagDefinitionId { get; set; } diff --git a/SetUser.cs b/SetUser.cs index 93af581..cf13c7f 100644 --- a/SetUser.cs +++ b/SetUser.cs @@ -1,15 +1,11 @@ -using HyperBooru.ApiClient; -using System.Management.Automation; +using System.Management.Automation; using System.Security; namespace HyperBooru.PowerShell; [Alias("shbu")] [Cmdlet(VerbsCommon.Set, "HyperBooruUser")] -public class SetUserCmdlet : PSCmdlet { - [Parameter(Position = 0, Mandatory = true)] - public HBSession Session { get; set; } - +public class SetUserCmdlet : SessionCmdlet { [Parameter(Position = 1, Mandatory = true)] public Guid UserId { get; set; } -- cgit v1.3