|
| 1 | +using McMaster.Extensions.CommandLineUtils; |
| 2 | +using Spectre.Console; |
| 3 | +using System.Text.Json; |
| 4 | + |
| 5 | +namespace Resend.Cli.Email; |
| 6 | + |
| 7 | +/// <summary /> |
| 8 | +[Command( "batch", Description = "Sends a batch of emails" )] |
| 9 | +public class EmailBatchCommand |
| 10 | +{ |
| 11 | + private readonly IResend _resend; |
| 12 | + |
| 13 | + |
| 14 | + /// <summary /> |
| 15 | + [Argument( 0, Description = "Input JSON file" )] |
| 16 | + [FileExists] |
| 17 | + public string? InputFile { get; set; } |
| 18 | + |
| 19 | + /// <summary /> |
| 20 | + [Option( "-k|--key", CommandOptionType.SingleValue, Description = "Idempotency key" )] |
| 21 | + public string? IdempotencyKey { get; set; } |
| 22 | + |
| 23 | + /// <summary /> |
| 24 | + [Option( "-m|--mode", CommandOptionType.SingleValue, Description = "Validation mode" )] |
| 25 | + public EmailBatchValidationMode ValidationMode { get; set; } = EmailBatchValidationMode.Strict; |
| 26 | + |
| 27 | + /// <summary /> |
| 28 | + [Option( "-j|--json", CommandOptionType.NoValue, Description = "Emit output as JSON array" )] |
| 29 | + public bool InJson { get; set; } |
| 30 | + |
| 31 | + |
| 32 | + /// <summary /> |
| 33 | + public EmailBatchCommand( IResend resend ) |
| 34 | + { |
| 35 | + _resend = resend; |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + /// <summary /> |
| 40 | + public async Task<int> OnExecuteAsync() |
| 41 | + { |
| 42 | + /* |
| 43 | + * |
| 44 | + */ |
| 45 | + string json; |
| 46 | + |
| 47 | + if ( Console.IsInputRedirected == true && this.InputFile == null ) |
| 48 | + { |
| 49 | + json = await Console.In.ReadToEndAsync(); |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + var ifile = this.InputFile ?? "emails.json"; |
| 54 | + |
| 55 | + if ( File.Exists( ifile ) == false ) |
| 56 | + { |
| 57 | + var of = Console.ForegroundColor; |
| 58 | + Console.ForegroundColor = ConsoleColor.Red; |
| 59 | + Console.WriteLine( "The file '{0}' does not exist.", ifile ); |
| 60 | + Console.ForegroundColor = of; |
| 61 | + |
| 62 | + return 1; |
| 63 | + } |
| 64 | + |
| 65 | + json = File.ReadAllText( ifile ); |
| 66 | + } |
| 67 | + |
| 68 | + List<EmailMessage> messages = JsonSerializer.Deserialize<List<EmailMessage>>( json )!; |
| 69 | + |
| 70 | + |
| 71 | + /* |
| 72 | + * |
| 73 | + */ |
| 74 | + ResendResponse<EmailBatchResponse> res; |
| 75 | + |
| 76 | + if ( this.IdempotencyKey != null ) |
| 77 | + res = await _resend.EmailBatchAsync( this.IdempotencyKey, messages, this.ValidationMode ); |
| 78 | + else |
| 79 | + res = await _resend.EmailBatchAsync( messages, this.ValidationMode ); |
| 80 | + |
| 81 | + |
| 82 | + /* |
| 83 | + * |
| 84 | + */ |
| 85 | + if ( this.InJson == true ) |
| 86 | + { |
| 87 | + var jso = new JsonSerializerOptions() { WriteIndented = true }; |
| 88 | + var ojson = JsonSerializer.Serialize( res.Content, jso ); |
| 89 | + |
| 90 | + Console.WriteLine( ojson ); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + if ( res.Content.Data.Count > 0 ) |
| 95 | + { |
| 96 | + var table = new Table(); |
| 97 | + table.Border = TableBorder.SimpleHeavy; |
| 98 | + table.AddColumn( "Id" ); |
| 99 | + |
| 100 | + foreach ( var d in res.Content.Data ) |
| 101 | + { |
| 102 | + table.AddRow( |
| 103 | + new Markup( d.Id.ToString() ) |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + AnsiConsole.Write( table ); |
| 108 | + } |
| 109 | + |
| 110 | + if ( res.Content.Errors != null ) |
| 111 | + { |
| 112 | + var table = new Table(); |
| 113 | + table.Border = TableBorder.SimpleHeavy; |
| 114 | + table.AddColumn( "Ix" ); |
| 115 | + table.AddColumn( "Error" ); |
| 116 | + |
| 117 | + foreach ( var d in res.Content.Errors ) |
| 118 | + { |
| 119 | + table.AddRow( |
| 120 | + new Markup( d.Index.ToString() ), |
| 121 | + new Markup( d.Message ) |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + AnsiConsole.Write( table ); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return 0; |
| 130 | + } |
| 131 | +} |
0 commit comments