Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Documentation/arc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cratis arc commands list [--url <URL>] [-o <FORMAT>]

| Option | Description |
|---|---|
| `--url <URL>` | Base URL of the Arc application (default: `https://localhost:5001`). |
| `--url <URL>` | Base URL of the Arc application (default: `http://localhost:5000`). |
| `-o, --output <FORMAT>` | Output format: `table`, `plain`, `json`, `json-compact`. |

## Output
Expand Down
5 changes: 3 additions & 2 deletions Documentation/arc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ All `cratis arc` commands accept the following connection flag:

| Flag | Description |
|---|---|
| `--url <URL>` | Base URL of the Arc application. Overrides the `ARC_URL` environment variable. Defaults to `https://localhost:5001`. |
| `--url <URL>` | Base URL of the Arc application. Overrides the `ARC_URL` environment variable. Defaults to `http://localhost:5000`. |

## Connection Resolution Order

The CLI resolves the Arc application URL in this order:

1. `--url` flag
2. `ARC_URL` environment variable
3. Default: `https://localhost:5001`
3. `Properties/launchSettings.json` or `properties/launchSettings.json` `applicationUrl`
4. Default: `http://localhost:5000`

## Sub-Commands

Expand Down
2 changes: 1 addition & 1 deletion Documentation/arc/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cratis arc queries list [--url <URL>] [-o <FORMAT>]

| Option | Description |
|---|---|
| `--url <URL>` | Base URL of the Arc application (default: `https://localhost:5001`). |
| `--url <URL>` | Base URL of the Arc application (default: `http://localhost:5000`). |
| `-o, --output <FORMAT>` | Output format: `table`, `plain`, `json`, `json-compact`. |

## Output
Expand Down
1 change: 1 addition & 0 deletions Source/Cli.Specs/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

global using Cratis.Cli.Commands.Arc;
global using Cratis.Cli.Commands.Chronicle;
global using Cratis.Cli.Commands.Chronicle.Diagnose;
global using Cratis.Cli.Commands.Chronicle.Observers;
Expand Down
66 changes: 66 additions & 0 deletions Source/Cli.Specs/for_ArcSettings/given/a_temp_arc_directory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Cratis.Cli.for_ArcSettings.given;

public class a_temp_arc_directory : Specification, IDisposable
{
protected string _tempDirectory = string.Empty;
string? _previousCurrentDirectory;
string? _previousArcUrl;

void Establish()
{
_tempDirectory = Path.Combine(Path.GetTempPath(), $"cratis-cli-arc-specs-{Guid.NewGuid():N}");
Directory.CreateDirectory(_tempDirectory);

_previousCurrentDirectory = Directory.GetCurrentDirectory();
_previousArcUrl = Environment.GetEnvironmentVariable(ArcDefaults.UrlEnvVar);

Environment.SetEnvironmentVariable(ArcDefaults.UrlEnvVar, null);
Directory.SetCurrentDirectory(_tempDirectory);
}

protected void WriteLaunchSettings(string folderName, string applicationUrl)
{
var propertiesPath = Path.Combine(_tempDirectory, folderName);
Directory.CreateDirectory(propertiesPath);

var content =
"{\n" +
" \"profiles\": {\n" +
" \"Core\": {\n" +
" \"commandName\": \"Project\",\n" +
$" \"applicationUrl\": \"{applicationUrl}\"\n" +
" }\n" +
" }\n" +
"}\n";
File.WriteAllText(Path.Combine(propertiesPath, "launchSettings.json"), content);
}

/// <inheritdoc/>
#pragma warning disable CA1033
void IDisposable.Dispose()
#pragma warning restore CA1033
{
CleanUp();
}

protected virtual void CleanUp()
{
Directory.SetCurrentDirectory(_previousCurrentDirectory ?? Environment.CurrentDirectory);
Environment.SetEnvironmentVariable(ArcDefaults.UrlEnvVar, _previousArcUrl);

try
{
if (Directory.Exists(_tempDirectory))
{
Directory.Delete(_tempDirectory, true);
}
}
catch
{
// Best-effort cleanup.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Cratis.Cli.for_ArcSettings.when_resolving_url;

[Collection(CliSpecsCollection.Name)]
public class and_environment_variable_is_set : given.a_temp_arc_directory
{
ArcSettings _settings = null!;
string _result = string.Empty;

void Establish()
{
WriteLaunchSettings("Properties", "http://localhost:5100/");
Environment.SetEnvironmentVariable(ArcDefaults.UrlEnvVar, "http://localhost:5300/");
_settings = new ArcSettings();
}

void Because() => _result = _settings.ResolveUrl();

[Fact] void should_return_environment_variable() => _result.ShouldEqual("http://localhost:5300");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Cratis.Cli.for_ArcSettings.when_resolving_url;

[Collection(CliSpecsCollection.Name)]
public class and_flag_is_set : given.a_temp_arc_directory
{
ArcSettings _settings = null!;
string _result = string.Empty;

void Establish() => _settings = new ArcSettings { Url = "http://localhost:6001/" };

void Because() => _result = _settings.ResolveUrl();

[Fact] void should_return_the_flag_value() => _result.ShouldEqual("http://localhost:6001");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Cratis.Cli.for_ArcSettings.when_resolving_url;

[Collection(CliSpecsCollection.Name)]
public class and_launch_settings_exists_in_lowercase_properties_folder : given.a_temp_arc_directory
{
ArcSettings _settings = null!;
string _result = string.Empty;

void Establish()
{
WriteLaunchSettings("properties", "http://localhost:5200/");
_settings = new ArcSettings();
}

void Because() => _result = _settings.ResolveUrl();

[Fact] void should_return_the_application_url() => _result.ShouldEqual("http://localhost:5200");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Cratis.Cli.for_ArcSettings.when_resolving_url;

[Collection(CliSpecsCollection.Name)]
public class and_launch_settings_exists_in_properties_folder : given.a_temp_arc_directory
{
ArcSettings _settings = null!;
string _result = string.Empty;

void Establish()
{
WriteLaunchSettings("Properties", "http://localhost:5100/");
_settings = new ArcSettings();
}

void Because() => _result = _settings.ResolveUrl();

[Fact] void should_return_the_application_url() => _result.ShouldEqual("http://localhost:5100");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Cratis.Cli.for_ArcSettings.when_resolving_url;

[Collection(CliSpecsCollection.Name)]
public class and_no_override_is_configured : given.a_temp_arc_directory
{
ArcSettings _settings = null!;
string _result = string.Empty;

void Establish() => _settings = new ArcSettings();

void Because() => _result = _settings.ResolveUrl();

[Fact] void should_return_default_url() => _result.ShouldEqual(ArcDefaults.DefaultUrl);
}
2 changes: 1 addition & 1 deletion Source/Cli/Commands/Arc/ArcDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class ArcDefaults
/// <summary>
/// The default URL for an Arc application.
/// </summary>
public const string DefaultUrl = "https://localhost:5001";
public const string DefaultUrl = "http://localhost:5000";

/// <summary>
/// Environment variable name for the Arc application URL.
Expand Down
88 changes: 86 additions & 2 deletions Source/Cli/Commands/Arc/ArcSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Text.Json;

namespace Cratis.Cli.Commands.Arc;

/// <summary>
Expand All @@ -12,11 +14,11 @@ public class ArcSettings : GlobalSettings
/// Gets or sets the base URL of the Arc application.
/// </summary>
[CommandOption("--url <URL>")]
[Description("Base URL of the Arc application (e.g. https://localhost:5001)")]
[Description("Base URL of the Arc application (e.g. http://localhost:5000)")]
public string? Url { get; set; }

/// <summary>
/// Resolves the effective base URL by checking the flag, environment variable, then default.
/// Resolves the effective base URL by checking the flag, environment variable, launch settings, then default.
/// </summary>
/// <returns>The resolved base URL string.</returns>
#pragma warning disable CA1055 // URI string return type — string is intentional here for consistency with connection helpers
Expand All @@ -34,6 +36,88 @@ public string ResolveUrl()
return envVar.TrimEnd('/');
}

var launchSettingsUrl = TryGetApplicationUrlFromLaunchSettings();
if (!string.IsNullOrWhiteSpace(launchSettingsUrl))
{
return launchSettingsUrl;
}

return ArcDefaults.DefaultUrl;
}

static string? TryGetApplicationUrlFromLaunchSettings()
{
var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
while (currentDirectory is not null)
{
var upperCasePath = Path.Combine(currentDirectory.FullName, "Properties", "launchSettings.json");
var upperCaseUrl = TryReadApplicationUrl(upperCasePath);
if (!string.IsNullOrWhiteSpace(upperCaseUrl))
{
return upperCaseUrl;
}

var lowerCasePath = Path.Combine(currentDirectory.FullName, "properties", "launchSettings.json");
var lowerCaseUrl = TryReadApplicationUrl(lowerCasePath);
if (!string.IsNullOrWhiteSpace(lowerCaseUrl))
{
return lowerCaseUrl;
}

currentDirectory = currentDirectory.Parent;
}

return null;
}

static string? TryReadApplicationUrl(string path)
{
if (!File.Exists(path))
{
return null;
}

try
{
using var stream = File.OpenRead(path);
using var document = JsonDocument.Parse(stream);
if (!document.RootElement.TryGetProperty("profiles", out var profiles) || profiles.ValueKind != JsonValueKind.Object)
{
return null;
}

foreach (var profile in profiles.EnumerateObject())
{
if (profile.Value.ValueKind != JsonValueKind.Object ||
!profile.Value.TryGetProperty("applicationUrl", out var applicationUrlProperty) ||
applicationUrlProperty.ValueKind != JsonValueKind.String)
{
continue;
}

var applicationUrl = applicationUrlProperty.GetString();
if (string.IsNullOrWhiteSpace(applicationUrl))
{
continue;
}

var firstUrl = applicationUrl.Split(';', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
if (!string.IsNullOrWhiteSpace(firstUrl))
{
return firstUrl.TrimEnd('/');
}
}
}
catch (IOException)
{
}
catch (UnauthorizedAccessException)
{
}
catch (JsonException)
{
}

return null;
}
}
2 changes: 1 addition & 1 deletion Source/Cli/Commands/Arc/Commands/ListCommandsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Cratis.Cli.Commands.Arc.Commands;
[LlmDescription("Lists all registered command endpoints in the Arc application. Returns name, namespace, HTTP route, type, and documentation summary for each command.")]
[CliCommand("list", "List registered command endpoints", Branch = typeof(ArcBranch.Commands))]
[CliExample("arc", "commands", "list")]
[CliExample("arc", "commands", "list", "--url", "https://localhost:5001")]
[CliExample("arc", "commands", "list", "--url", "http://localhost:5000")]
[CliExample("arc", "commands", "list", "-o", "json")]
[LlmOutputAdvice("plain", "plain is significantly smaller than JSON for large command lists.")]
public class ListCommandsCommand : ArcCommand<ArcSettings>
Expand Down
2 changes: 1 addition & 1 deletion Source/Cli/Commands/Arc/Queries/ListQueriesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Cratis.Cli.Commands.Arc.Queries;
[LlmDescription("Lists all registered query endpoints in the Arc application. Returns name, namespace, HTTP route, fully qualified type, and documentation summary for each query.")]
[CliCommand("list", "List registered query endpoints", Branch = typeof(ArcBranch.Queries))]
[CliExample("arc", "queries", "list")]
[CliExample("arc", "queries", "list", "--url", "https://localhost:5001")]
[CliExample("arc", "queries", "list", "--url", "http://localhost:5000")]
[CliExample("arc", "queries", "list", "-o", "json")]
[LlmOutputAdvice("plain", "plain is significantly smaller than JSON for large query lists.")]
public class ListQueriesCommand : ArcCommand<ArcSettings>
Expand Down
Loading