Skip to content
This repository has been archived by the owner on Dec 25, 2024. It is now read-only.

Commit

Permalink
Added TryGetWorkspaceId() and TryGetLiveMode()
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Jun 2, 2024
1 parent cecc4b9 commit ae9b30e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/FaluCli/Client/FaluCliClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
if (string.IsNullOrWhiteSpace(key))
{
// (1) Override the X-Workspace-Id header if CLI contains the option
var workspaceId = context.GetWorkspaceId();
if (!string.IsNullOrWhiteSpace(workspaceId))
if (context.TryGetWorkspaceId(out var workspaceId))
{
request.Headers.Replace("X-Workspace-Id", workspaceId);
}

// (2) Override the X-Live-Mode header if CLI contains the option
var live = context.GetLiveMode();
if (live is not null)
if (context.TryGetLiveMode(out var live))
{
request.Headers.Replace("X-Live-Mode", live.Value.ToString().ToLowerInvariant());
}
Expand Down
22 changes: 13 additions & 9 deletions src/FaluCli/Extensions/InvocationContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
namespace System.CommandLine;
using System.Diagnostics.CodeAnalysis;

namespace System.CommandLine;

/// <summary>
/// Extensions for <see cref="InvocationContext"/>
/// </summary>
internal static class InvocationContextExtensions
{
public static bool IsVerboseEnabled(this InvocationContext context)
{
return context.ParseResult.ValueForOption<bool>("--verbose");
}
public static bool IsVerboseEnabled(this InvocationContext context) => context.ParseResult.ValueForOption<bool>("--verbose");
public static bool IsTelemetryDisabled(this InvocationContext context) => context.ParseResult.ValueForOption<bool>("--no-telemetry");
public static string? GetWorkspaceId(this InvocationContext context) => context.ParseResult.ValueForOption<string>("--workspace");
public static bool? GetLiveMode(this InvocationContext context) => context.ParseResult.ValueForOption<bool?>("--live");

public static string? GetWorkspaceId(this InvocationContext context)
public static bool TryGetWorkspaceId(this InvocationContext context, [NotNullWhen(true)] out string? workspaceId)
{
return context.ParseResult.ValueForOption<string>("--workspace");
workspaceId = context.GetWorkspaceId();
return !string.IsNullOrWhiteSpace(workspaceId);
}

public static bool? GetLiveMode(this InvocationContext context)
public static bool TryGetLiveMode(this InvocationContext context, [NotNullWhen(true)] out bool? liveMode)
{
return context.ParseResult.ValueForOption<bool?>("--live");
liveMode = context.GetLiveMode();
return liveMode is not null;
}
}

0 comments on commit ae9b30e

Please sign in to comment.