Skip to content

Commit 6b5920b

Browse files
Fusion CLI DevEx improvements (#8607)
1 parent cb1dcd3 commit 6b5920b

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

src/Nitro/CommandLine/src/CommandLine.Cloud/Commands/Api/CreateApiCommand.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ private static async Task<int> ExecuteAsync(
4242

4343
var name = await context.OptionOrAskAsync("Name", Opt<ApiNameOption>.Instance, ct);
4444
var pathResult = await context
45-
.OptionOrAskAsync("Path [dim](e.g. /foo/bar)[/]", Opt<ApiPathOption>.Instance, ct);
45+
.OptionOrAskAsync(
46+
"Path [dim](e.g. /foo/bar)[/]",
47+
Opt<ApiPathOption>.Instance,
48+
defaultValue: "/",
49+
ct);
4650

47-
var path = pathResult.Split("/", TrimEntries | RemoveEmptyEntries).ToArray();
51+
var path = pathResult.Split("/", TrimEntries | RemoveEmptyEntries);
4852

4953
var kind = context.GetApiKind();
5054

src/Nitro/CommandLine/src/CommandLine.Cloud/Commands/Login/LoginCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ private static async Task<int> ExecuteAsync(
4444
$"Logged in as [bold]{session.Email}[/] ({session.Tenant} on {session.IdentityServer})");
4545

4646
return await SetDefaultWorkspaceCommand
47-
.ExecuteAsync(console, client, sessionService, cancellationToken);
47+
.ExecuteAsync(forceSelection: false, console, client, sessionService, cancellationToken);
4848
}
4949
}

src/Nitro/CommandLine/src/CommandLine.Cloud/Commands/Workspace/SetDefaultWorkspaceCommand.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ public SetDefaultWorkspaceCommand() : base(Command)
1414
Description =
1515
"Use this command to select a workspace and set it as your default workspace";
1616

17-
this.SetHandler(
18-
ExecuteAsync,
19-
Bind.FromServiceProvider<IAnsiConsole>(),
20-
Bind.FromServiceProvider<IApiClient>(),
21-
Bind.FromServiceProvider<ISessionService>(),
22-
Bind.FromServiceProvider<CancellationToken>());
17+
this.SetHandler(context => ExecuteAsync(
18+
true,
19+
context.BindingContext.GetRequiredService<IAnsiConsole>(),
20+
context.BindingContext.GetRequiredService<IApiClient>(),
21+
context.BindingContext.GetRequiredService<ISessionService>(),
22+
context.BindingContext.GetRequiredService<CancellationToken>()));
2323
}
2424

2525
public static async Task<int> ExecuteAsync(
26+
bool forceSelection,
2627
IAnsiConsole console,
2728
IApiClient client,
2829
ISessionService sessionService,
@@ -42,22 +43,38 @@ public static async Task<int> ExecuteAsync(
4243
$"You do not have any workspaces. Run {"nitro launch".AsCommand()} and create one.");
4344
}
4445

45-
var selectedWorkspace = await PagedSelectionPrompt
46-
.New(paginationContainer)
47-
.Title(message.AsQuestion())
48-
.UseConverter(x => x.Node.Name)
49-
.RenderAsync(console, cancellationToken);
46+
Workspace? workspace;
47+
var wasPrompted = false;
5048

51-
if (selectedWorkspace is null)
49+
if (current.Count == 1 && !forceSelection)
5250
{
53-
throw Exit("No workspaces was selected as default");
51+
var firstWorkspace = current[0].Node;
52+
workspace = new Workspace(firstWorkspace.Id, firstWorkspace.Name);
5453
}
54+
else
55+
{
56+
var selectedWorkspace = await PagedSelectionPrompt
57+
.New(paginationContainer)
58+
.Title(message.AsQuestion())
59+
.UseConverter(x => x.Node.Name)
60+
.RenderAsync(console, cancellationToken);
61+
62+
if (selectedWorkspace is null)
63+
{
64+
throw Exit("No workspaces was selected as default");
65+
}
66+
67+
workspace = new Workspace(selectedWorkspace.Node.Id, selectedWorkspace.Node.Name);
5568

56-
var workspace = new Workspace(selectedWorkspace.Node.Id, selectedWorkspace.Node.Name);
69+
wasPrompted = true;
70+
}
5771

5872
await sessionService.SelectWorkspaceAsync(workspace, cancellationToken);
5973

60-
console.OkQuestion(message, workspace.Name);
74+
if (wasPrompted)
75+
{
76+
console.OkQuestion(message, workspace.Name);
77+
}
6178

6279
return ExitCodes.Success;
6380
}

src/Nitro/CommandLine/src/CommandLine.Core/Helpers/ConsoleHelpers.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static async Task<string> OptionOrAskAsync(
4747
this InvocationContext context,
4848
string question,
4949
Option<string> option,
50+
string? defaultValue,
5051
CancellationToken cancellationToken)
5152
{
5253
var value = context.ParseResult.GetValueForOption(option);
@@ -58,10 +59,23 @@ public static async Task<string> OptionOrAskAsync(
5859

5960
var console = context.BindingContext.GetRequiredService<IAnsiConsole>();
6061

61-
return await new TextPrompt<string>(question.AsQuestion())
62-
.ShowAsync(console, cancellationToken);
62+
var prompt = new TextPrompt<string>(question.AsQuestion());
63+
64+
if (defaultValue is not null)
65+
{
66+
prompt = prompt.DefaultValue(defaultValue);
67+
}
68+
69+
return await prompt.ShowAsync(console, cancellationToken);
6370
}
6471

72+
public static Task<string> OptionOrAskAsync(
73+
this InvocationContext context,
74+
string question,
75+
Option<string> option,
76+
CancellationToken cancellationToken)
77+
=> OptionOrAskAsync(context, question, option, defaultValue: null, cancellationToken);
78+
6579
public static async Task<string> AskAsync(
6680
this IAnsiConsole console,
6781
string question,

0 commit comments

Comments
 (0)