Skip to content

Commit bbb3aaa

Browse files
authored
Merge pull request #316 from TraGicCode/chore/implement-message-body-in-a-file
--message-body now can be passed a @filename to pull json from
2 parents c1a1b71 + 2cb8b02 commit bbb3aaa

12 files changed

Lines changed: 161 additions & 14 deletions

File tree

src/BuslyCLI.Console/Commands/CommonMessageSettings.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel;
2+
using BuslyCLI.TypeConverters;
23
using Spectre.Console;
34
using Spectre.Console.Cli;
45

@@ -25,15 +26,24 @@ public abstract class CommonMessageSettings : GlobalCommandSettings
2526
public required string EnclosedMessageType { get; set; }
2627

2728
[CommandOption("-m|--message-body <message-body>")]
28-
[Description("The content of the message body")]
29-
public required string MessageBody { get; set; }
29+
[Description("The content of the message body. Accepts a raw JSON string or a path to a file using curl-style @file syntax (e.g. @payload.json).")]
30+
public required MessageBodyValue MessageBody { get; set; }
3031

3132
public override ValidationResult Validate()
3233
{
3334
if (string.IsNullOrWhiteSpace(ContentType)) return ValidationResult.Error("must specify a 'content-type'.");
3435
if (string.IsNullOrWhiteSpace(EnclosedMessageType))
3536
return ValidationResult.Error("must specify an 'enclosed-message-type'.");
36-
if (string.IsNullOrWhiteSpace(MessageBody)) return ValidationResult.Error("must specify a 'message-body'.");
37+
38+
if (MessageBody is null) return ValidationResult.Error("must specify a 'message-body'.");
39+
if (Path.IsPathFullyQualified(MessageBody.Value))
40+
{
41+
if (!File.Exists(MessageBody.Value))
42+
return ValidationResult.Error($"File not found: {MessageBody.Value}");
43+
44+
MessageBody.Value = File.ReadAllText(MessageBody.Value);
45+
}
46+
3747
return base.Validate();
3848
}
3949

src/BuslyCLI.Console/Commands/NsbCommand/SendCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected override async Task<int> ExecuteAsync(CommandContext context, SendComm
2727
var message = new OutgoingMessage(
2828
Guid.NewGuid().ToString(),
2929
headers,
30-
Encoding.ASCII.GetBytes(settings.MessageBody)
30+
Encoding.ASCII.GetBytes(settings.MessageBody.Value)
3131
);
3232

3333
var transportOperation = new TransportOperation(

src/BuslyCLI.Console/Commands/NsbEvent/PublishCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected override async Task<int> ExecuteAsync(CommandContext context, PublishC
3030
var message = new OutgoingMessage(
3131
Guid.NewGuid().ToString(),
3232
headers,
33-
Encoding.ASCII.GetBytes(settings.MessageBody)
33+
Encoding.ASCII.GetBytes(settings.MessageBody.Value)
3434
);
3535

3636
var type = CreateTypeFromString(settings.EnclosedMessageType);

src/BuslyCLI.Console/Commands/NsbTimeout/SendTimeout.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected override async Task<int> ExecuteAsync(CommandContext context, SendTime
4646
var message = new OutgoingMessage(
4747
Guid.NewGuid().ToString(),
4848
headers,
49-
Encoding.ASCII.GetBytes(settings.MessageBody)
49+
Encoding.ASCII.GetBytes(settings.MessageBody.Value)
5050
);
5151

5252
var dispatchProperties = new DispatchProperties();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.ComponentModel;
2+
3+
namespace BuslyCLI.TypeConverters;
4+
5+
public class MessageBodyTypeConverter : TypeConverter
6+
{
7+
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
8+
{
9+
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
10+
}
11+
12+
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
13+
{
14+
15+
if (value is string str)
16+
{
17+
// Curl-style @file reference: parse only, no I/O
18+
if (str.StartsWith("@"))
19+
{
20+
var expandedFilePath = new ExpandedPath(str[1..]);
21+
return new MessageBodyValue(expandedFilePath.Path);
22+
}
23+
}
24+
25+
return base.ConvertFrom(context, culture, value);
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.ComponentModel;
2+
3+
namespace BuslyCLI.TypeConverters;
4+
5+
[TypeConverter(typeof(MessageBodyTypeConverter))]
6+
public class MessageBodyValue
7+
{
8+
public string Value { get; set; }
9+
10+
11+
public MessageBodyValue(string value)
12+
{
13+
Value = value;
14+
}
15+
}

tests/BuslyCLI.Console.Tests/Commands/CommonCommandSettingsTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BuslyCLI.Commands.NsbCommand;
2+
using BuslyCLI.TypeConverters;
23

34
namespace BuslyCLI.Console.Tests.Commands;
45

@@ -12,7 +13,7 @@ public void ShouldFailWhenDestinationEndpointIsMissing()
1213
{
1314
ContentType = "application/json",
1415
EnclosedMessageType = "MessageContracts.Commands.CreateOrder",
15-
MessageBody = "{}",
16+
MessageBody = new MessageBodyValue("{}"),
1617
DestinationEndpoint = null!
1718
};
1819

tests/BuslyCLI.Console.Tests/Commands/CommonMessageSettingsTests.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BuslyCLI.Commands.NsbEvent;
2+
using BuslyCLI.TypeConverters;
23

34
namespace BuslyCLI.Console.Tests.Commands;
45

@@ -12,7 +13,7 @@ public void ShouldFailWhenContentTypeIsMissing()
1213
{
1314
ContentType = null!,
1415
EnclosedMessageType = "MessageContracts.Commands.CreateOrder",
15-
MessageBody = "{}"
16+
MessageBody = new MessageBodyValue("{}")
1617
};
1718

1819
var result = settings.Validate();
@@ -28,7 +29,7 @@ public void ShouldFailWhenEnclosedMessageTypeIsMissing()
2829
{
2930
ContentType = "application/json",
3031
EnclosedMessageType = null!,
31-
MessageBody = "{}"
32+
MessageBody = new MessageBodyValue("{}")
3233
};
3334

3435
var result = settings.Validate();
@@ -52,4 +53,21 @@ public void ShouldFailWhenMessageBodyIsMissing()
5253
Assert.That(result.Successful, Is.False);
5354
Assert.That(result.Message, Does.Contain("must specify a 'message-body'."));
5455
}
56+
57+
[Test]
58+
public void ShouldFailWhenMessageBodyFileDoesNotExist()
59+
{
60+
var nonExistentFilePath = Path.GetFullPath("non-existent-payload.json");
61+
var settings = new PublishCommandSettings
62+
{
63+
ContentType = "application/json",
64+
EnclosedMessageType = "MessageContracts.Commands.CreateOrder",
65+
MessageBody = new MessageBodyValue(nonExistentFilePath)
66+
};
67+
68+
var result = settings.Validate();
69+
70+
Assert.That(result.Successful, Is.False);
71+
Assert.That(result.Message, Does.Contain($"File not found: {nonExistentFilePath}"));
72+
}
5573
}

website/docs/cli-reference/command/send.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ busly send command
1414
| ------------------------------- | ----------------------------------------------------------------------------------------------- |
1515
| `-c`, `--content-type` | The fully qualified .NET type name of the enclosed message (ex: Ordering.Commands.CreateOrder ) |
1616
| `-e`, `--enclosed-message-type` | The type of serialization used for the message |
17-
| `-m`, `--message-body` | The content of the message body |
18-
| `-d`, `--destination-endpoint` | The destination endpoint to send a message to |
17+
| `-m`, `--message-body` | The content of the message body. Accepts a raw JSON string or a path to a file using curl-style `@` syntax (e.g. `@payload.json`). |
1918

2019
## Examples
2120

website/docs/cli-reference/event/publish.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ busly event publish
1414
| ------------------------------- | ----------------------------------------------------------------------------------------------- |
1515
| `-c`, `--content-type` | The fully qualified .NET type name of the enclosed message (ex: Ordering.Commands.CreateOrder ) |
1616
| `-e`, `--enclosed-message-type` | The type of serialization used for the message |
17-
| `-m`, `--message-body` | The content of the message body |
17+
| `-m`, `--message-body` | The content of the message body. Accepts a raw JSON string or a path to a file using curl-style `@` syntax (e.g. `@payload.json`). |
1818

1919
## Examples
2020

0 commit comments

Comments
 (0)