This repository has been archived by the owner on Dec 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This paves way for using the workspace name as an alternative to its identifier. It also allows us to validate the workspace before sending the request hence failing early. The workspaces can be synced using new commands: - `falu workspaces list [--refresh] [--all]` - `falu workspaces show --name <name-or-id>` After login, the workspaces are also synced which should ensure we have valid data every few (approximately 7) days.
- Loading branch information
1 parent
6437cbd
commit c8c7cfd
Showing
12 changed files
with
255 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Falu.Core; | ||
|
||
namespace Falu.Client.Workspaces; | ||
|
||
internal class Workspace : IHasId | ||
{ | ||
/// <inheritdoc/> | ||
public string? Id { get; set; } | ||
|
||
public string? Name { get; set; } | ||
|
||
public string? Status { get; set; } | ||
|
||
public string? Role { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using Falu.Core; | ||
|
||
namespace Falu.Client.Workspaces; | ||
|
||
/// <summary>Options for filtering and pagination of workspaces.</summary> | ||
public record WorkspacesListOptions : BasicListOptions { } // intentionally left blank |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Falu.Core; | ||
using SC = Falu.FaluCliJsonSerializerContext; | ||
|
||
namespace Falu.Client.Workspaces; | ||
|
||
internal class WorkspacesServiceClient(HttpClient backChannel, FaluClientOptions options) : BaseServiceClient<Workspace>(backChannel, options, SC.Default.Workspace, SC.Default.ListWorkspace), | ||
ISupportsListing<Workspace, WorkspacesListOptions> | ||
{ | ||
/// <inheritdoc/> | ||
protected override string BasePath => "/v1/workspaces"; | ||
|
||
|
||
/// <summary>Retrieve workspaces.</summary> | ||
/// <inheritdoc/> | ||
public virtual Task<ResourceResponse<List<Workspace>>> ListAsync(WorkspacesListOptions? options = null, | ||
RequestOptions? requestOptions = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return ListResourcesAsync(options, requestOptions, cancellationToken); | ||
} | ||
|
||
/// <summary>Retrieve workspaces recursively.</summary> | ||
/// <inheritdoc/> | ||
public virtual IAsyncEnumerable<Workspace> ListRecursivelyAsync(WorkspacesListOptions? options = null, | ||
RequestOptions? requestOptions = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return ListResourcesRecursivelyAsync(options, requestOptions, cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using Falu.Config; | ||
using Spectre.Console; | ||
|
||
namespace Falu.Commands; | ||
|
||
internal class WorkspacesCommand : CliCommand | ||
{ | ||
public WorkspacesCommand() : base("workspaces", "Manage workspaces") | ||
{ | ||
Add(new WorkspacesListCommand()); | ||
Add(new WorkspacesShowCommand()); | ||
} | ||
} | ||
|
||
internal class WorkspacesListCommand : FaluCliCommand | ||
{ | ||
public WorkspacesListCommand() : base("list", "Get a list of workspaces for the logged in account.\r\nBy default, 'Terminated' workspaces are not shown.") | ||
{ | ||
this.AddOption(aliases: ["--all"], | ||
description: "List all workspaces, rather than skipping 'Terminated' ones.", | ||
defaultValue: false); | ||
|
||
this.AddOption(aliases: ["--refresh"], | ||
description: "Retrieve up-to-date workspaces from server.", | ||
defaultValue: false); | ||
} | ||
|
||
public override async Task<int> ExecuteAsync(CliCommandExecutionContext context, CancellationToken cancellationToken) | ||
{ | ||
var all = context.ParseResult.ValueForOption<bool>("--all"); | ||
var refresh = context.ParseResult.ValueForOption<bool>("--refresh"); | ||
|
||
var defaultWorkspaceId = context.ConfigValues.DefaultWorkspaceId; | ||
|
||
var workspaces = context.ConfigValues.Workspaces.ToList(); | ||
if (refresh) | ||
{ | ||
var response = await context.Client.Workspaces.ListAsync(cancellationToken: cancellationToken); | ||
response.EnsureSuccess(); | ||
workspaces = response.Resource!.Select(w => new ConfigValuesWorkspace(w)).ToList(); | ||
context.ConfigValues.Workspaces = workspaces; // update them so that they are saved | ||
|
||
// update default workspace | ||
if (workspaces.Count > 0) | ||
{ | ||
if (defaultWorkspaceId is not null) | ||
{ | ||
var workspace = context.ConfigValues.GetWorkspace(defaultWorkspaceId); | ||
if (workspace is null) | ||
{ | ||
context.Logger.LogInformation("Default workspace '{DefaultWorkspaceId}' not found. Resetting to null.", defaultWorkspaceId); | ||
defaultWorkspaceId = context.ConfigValues.DefaultWorkspaceId = null; | ||
} | ||
} | ||
} | ||
} | ||
|
||
workspaces = all ? workspaces : workspaces.Where(w => !string.Equals(w.Status, "terminated", StringComparison.OrdinalIgnoreCase)).ToList(); | ||
|
||
var table = new Table().AddColumn("Name") | ||
.AddColumn("Id") | ||
.AddColumn("Status") | ||
.AddColumn(new TableColumn("Default").Centered()); | ||
|
||
foreach (var workspace in workspaces) | ||
{ | ||
var isDefault = string.Equals(workspace.Id, defaultWorkspaceId, StringComparison.OrdinalIgnoreCase); | ||
table.AddRow(new Markup(workspace.Name), | ||
new Markup(workspace.Id), | ||
new Markup(workspace.Status), | ||
new Markup(isDefault ? SpectreFormatter.ColouredGreen("✔ YES") : "")); | ||
} | ||
|
||
AnsiConsole.Write(table); | ||
|
||
return 0; | ||
} | ||
} | ||
|
||
internal class WorkspacesShowCommand : FaluCliCommand | ||
{ | ||
public WorkspacesShowCommand() : base("show", " Get the details of a workspace.") | ||
{ | ||
this.AddOption<string>(aliases: ["--name", "-n"], | ||
description: "Name or ID of workspace.", | ||
configure: o => o.Required = true); | ||
} | ||
|
||
public override Task<int> ExecuteAsync(CliCommandExecutionContext context, CancellationToken cancellationToken) | ||
{ | ||
var name = context.ParseResult.ValueForOption<string>("--name")!; | ||
|
||
var workspace = context.ConfigValues.GetRequiredWorkspace(name); | ||
|
||
var table = new Table().AddColumn("Name") | ||
.AddColumn("Id") | ||
.AddColumn("Status") | ||
.AddColumn(new TableColumn("Default").Centered()); | ||
|
||
var isDefault = string.Equals(workspace.Id, context.ConfigValues.DefaultWorkspaceId, StringComparison.OrdinalIgnoreCase); | ||
table.AddRow(new Markup(workspace.Name), | ||
new Markup(workspace.Id), | ||
new Markup(workspace.Status), | ||
new Markup(isDefault ? SpectreFormatter.ColouredGreen("✔ YES") : "")); | ||
|
||
AnsiConsole.Write(table); | ||
|
||
return Task.FromResult(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters