using System.Management.Automation; namespace HyperBooru.PowerShell; [Alias("pbhbm")] [Cmdlet(VerbsData.Publish, "HyperBooruMedia")] public class PublishMediaCmdlet : SessionCmdlet { [Parameter(Position = 1, Mandatory = true, ValueFromPipeline = true)] public string Path { get; set; } [Parameter(Position = 2)] public SwitchParameter Delete { get; set; } [Parameter(Position = 3)] public Guid[] TagId { get; set; } = Array.Empty(); protected override void ProcessRecord() { Path = System.IO.Path.GetFullPath( Path, SessionState.Path.CurrentLocation.Path); ApiModels.Media media; try { media = Session.Media.UploadAsync(Path, TagId, !TagId.Any()) .GetAwaiter() .GetResult(); } catch(Exception e) { ThrowTerminatingError( new ErrorRecord( e, "UploadFile", ErrorCategory.WriteError, Path)); return; } WriteVerbose($"Uploaded file: {Path}"); WriteObject(media); if(!Delete) return; try { File.Delete(Path); WriteVerbose($"Deleted file: {Path}"); } catch(Exception e) { ThrowTerminatingError( new ErrorRecord( e, "DeleteFile", ErrorCategory.InvalidResult, Path)); } } }