Files
ClaudeMailbox/src/ClaudeMailbox/Cli/ClientCommands.cs
Mika Kuns ee0b72f43b
All checks were successful
CI (Node) / build-test (push) Successful in 10s
CI (.NET) / build (push) Successful in 11s
feat: change default port from 47822 to 37849 (v1.2.0)
47822 collided with ClaudeDo.Worker.exe on at least one user's machine.
37849 is high, registered to nobody, and avoids the prior conflict.
Both the Node port and the .NET port move together (still
wire-compatible). Defaults change only — if a user has a custom port
in mailbox.json, that stays.
2026-05-19 14:07:56 +02:00

105 lines
3.2 KiB
C#

using System.Net.Http.Json;
using System.Text.Json;
namespace ClaudeMailbox.Cli;
public static class ClientCommands
{
private const string DefaultUrl = "http://127.0.0.1:37849";
public static async Task<int> RunAsync(string[] args)
{
var command = args[0];
var url = GetOption(args, "--url") ?? DefaultUrl;
using var client = new HttpClient { BaseAddress = new Uri(url) };
try
{
return command switch
{
"send" => await Send(args, client),
"peek" => await Peek(args, client),
"check" => await Check(args, client),
"list" => await List(client),
_ => PrintError($"Unknown command: {command}"),
};
}
catch (HttpRequestException ex)
{
Console.Error.WriteLine($"Could not reach daemon at {url}: {ex.Message}");
Console.Error.WriteLine("Is 'claude-mailbox serve' running?");
return 2;
}
}
private static async Task<int> Send(string[] args, HttpClient client)
{
var to = Required(args, "--to");
var from = Required(args, "--from");
var body = Required(args, "--body");
var req = new HttpRequestMessage(HttpMethod.Post, "/v1/send")
{
Content = JsonContent.Create(new { to, body }),
};
req.Headers.Add("X-Mailbox", from);
var res = await client.SendAsync(req);
res.EnsureSuccessStatusCode();
Console.WriteLine(await res.Content.ReadAsStringAsync());
return 0;
}
private static async Task<int> Peek(string[] args, HttpClient client)
{
var name = Required(args, "--name");
var res = await client.GetAsync($"/v1/peek?name={Uri.EscapeDataString(name)}");
res.EnsureSuccessStatusCode();
Console.WriteLine(await res.Content.ReadAsStringAsync());
return 0;
}
private static async Task<int> Check(string[] args, HttpClient client)
{
var name = Required(args, "--name");
var req = new HttpRequestMessage(HttpMethod.Post, $"/v1/check-inbox?name={Uri.EscapeDataString(name)}");
req.Headers.Add("X-Mailbox", name);
var res = await client.SendAsync(req);
res.EnsureSuccessStatusCode();
Console.WriteLine(await res.Content.ReadAsStringAsync());
return 0;
}
private static async Task<int> List(HttpClient client)
{
var res = await client.GetAsync("/v1/list");
res.EnsureSuccessStatusCode();
Console.WriteLine(await res.Content.ReadAsStringAsync());
return 0;
}
public static string? GetOption(string[] args, string name)
{
for (var i = 0; i < args.Length - 1; i++)
if (args[i] == name) return args[i + 1];
return null;
}
private static string Required(string[] args, string name)
{
var v = GetOption(args, name);
if (string.IsNullOrWhiteSpace(v))
throw new ArgumentException($"Missing required option {name}");
return v;
}
private static int PrintError(string msg)
{
Console.Error.WriteLine(msg);
return 1;
}
}