diff options
| author | Dan Engelbrecht <[email protected]> | 2024-06-04 22:21:06 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2024-06-04 22:21:06 +0200 |
| commit | 3a82e1cb3b5a284d2c47e03d541a35b84589314b (patch) | |
| tree | d4b6c3d7c548b2d6f83b105fb0a5668ca2152889 /src/zen/cmds/workspaces_cmd.cpp | |
| parent | add batching of CacheStore requests for GetCacheValues/GetCacheChunks (#90) (diff) | |
| download | archived-zen-3a82e1cb3b5a284d2c47e03d541a35b84589314b.tar.xz archived-zen-3a82e1cb3b5a284d2c47e03d541a35b84589314b.zip | |
workspace share aliases (#91)
- Add `zen workspace-share` `--root-path` option - the root local file path of the workspace - if given it will automatically create the workspace before creating the share. If `--workspace` is omitted, an id will be generated from the `--root-path` parameter
- Add `/ws/share/{alias}/` endpoint - a shortcut to `/ws/{workspace_id}/{share_id}/` based endpoints using the alias for a workspace share
- Add `--alias` option to replace `--workspace` and `--share` options for `workspace-share` zen commands
- Rename `zen workspace create` `folder` option to `root-path`
- Rename `zen workspace create` `folder` option to `share-path`
Diffstat (limited to 'src/zen/cmds/workspaces_cmd.cpp')
| -rw-r--r-- | src/zen/cmds/workspaces_cmd.cpp | 129 |
1 files changed, 89 insertions, 40 deletions
diff --git a/src/zen/cmds/workspaces_cmd.cpp b/src/zen/cmds/workspaces_cmd.cpp index 503bc24cf..afdf5d7f5 100644 --- a/src/zen/cmds/workspaces_cmd.cpp +++ b/src/zen/cmds/workspaces_cmd.cpp @@ -28,9 +28,9 @@ WorkspaceCommand::WorkspaceCommand() m_CreateOptions.add_options()("h,help", "Print help"); m_CreateOptions.add_option("", "w", "workspace", "Workspace identity(id)", cxxopts::value(m_Id), "<workspaceid>"); - m_CreateOptions.add_option("", "r", "folder", "Root file system folder for workspace", cxxopts::value(m_Path), "<folder>"); - m_CreateOptions.parse_positional({"folder", "workspace"}); - m_CreateOptions.positional_help("folder workspace"); + m_CreateOptions.add_option("", "r", "root-path", "Root file system folder for workspace", cxxopts::value(m_Path), "<root-path>"); + m_CreateOptions.parse_positional({"root-path", "workspace"}); + m_CreateOptions.positional_help("root-path workspace"); m_InfoOptions.add_options()("h,help", "Print help"); m_InfoOptions.add_option("", "w", "workspace", "Workspace identity(id)", cxxopts::value(m_Id), "<workspaceid>"); @@ -155,27 +155,40 @@ WorkspaceShareCommand::WorkspaceShareCommand() m_CreateOptions.add_options()("h,help", "Print help"); m_CreateOptions.add_option("", "w", "workspace", "Workspace identity (id)", cxxopts::value(m_WorkspaceId), "<workspaceid>"); + m_CreateOptions.add_option("", + "r", + "root-path", + "Root path for workspace, replaces 'workspace' id", + cxxopts::value(m_WorkspaceRoot), + "<root-path>"); m_CreateOptions.add_option("", "s", "share", "Workspace share identity(id)", cxxopts::value(m_ShareId), "<shareid>"); - m_CreateOptions.add_option("", "r", "folder", "Folder path inside the workspace to share", cxxopts::value(m_SharePath), "<folder>"); - m_CreateOptions.parse_positional({"workspace", "folder", "share"}); - m_CreateOptions.positional_help("workspace folder share"); + m_CreateOptions + .add_option("", "p", "share-path", "Folder path inside the workspace to share", cxxopts::value(m_SharePath), "<share-path>"); + m_CreateOptions.add_option("", "a", "alias", "Named alias for this share", cxxopts::value(m_Alias), "<alias>"); + m_CreateOptions.parse_positional({"workspace", "share-path", "share"}); + m_CreateOptions.positional_help("workspace share-path share"); m_InfoOptions.add_options()("h,help", "Print help"); m_InfoOptions.add_option("", "w", "workspace", "Workspace identity (id)", cxxopts::value(m_WorkspaceId), "<workspaceid>"); m_InfoOptions.add_option("", "s", "share", "Workspace share identity(id)", cxxopts::value(m_ShareId), "<shareid>"); m_InfoOptions.add_option("", "r", "refresh", "Refresh workspace share", cxxopts::value(m_Refresh), "<refresh>"); + m_InfoOptions.add_option("", "a", "alias", "Named alias for this share", cxxopts::value(m_Alias), "<alias>"); m_InfoOptions.parse_positional({"workspace", "share"}); m_InfoOptions.positional_help("workspace share"); m_RemoveOptions.add_options()("h,help", "Print help"); m_RemoveOptions.add_option("", "w", "workspace", "Workspace identity (id)", cxxopts::value(m_WorkspaceId), "<workspaceid>"); m_RemoveOptions.add_option("", "s", "share", "Workspace share identity(id)", cxxopts::value(m_ShareId), "<shareid>"); + m_RemoveOptions + .add_option("", "a", "alias", "Alias for the share, replaces 'workspace' and 'share' options", cxxopts::value(m_Alias), "<alias>"); m_RemoveOptions.parse_positional({"workspace", "share"}); m_RemoveOptions.positional_help("workspace share"); m_FilesOptions.add_options()("h,help", "Print help"); m_FilesOptions.add_option("", "w", "workspace", "Workspace identity (id)", cxxopts::value(m_WorkspaceId), "<workspaceid>"); m_FilesOptions.add_option("", "s", "share", "Workspace share identity(id)", cxxopts::value(m_ShareId), "<shareid>"); + m_FilesOptions + .add_option("", "a", "alias", "Alias for the share, replaces 'workspace' and 'share' options", cxxopts::value(m_Alias), "<alias>"); m_FilesOptions.add_option("", "", "filter", @@ -189,6 +202,8 @@ WorkspaceShareCommand::WorkspaceShareCommand() m_EntriesOptions.add_options()("h,help", "Print help"); m_EntriesOptions.add_option("", "w", "workspace", "Workspace identity (id)", cxxopts::value(m_WorkspaceId), "<workspaceid>"); m_EntriesOptions.add_option("", "s", "share", "Workspace share identity(id)", cxxopts::value(m_ShareId), "<shareid>"); + m_EntriesOptions + .add_option("", "a", "alias", "Alias for the share, replaces 'workspace' and 'share' options", cxxopts::value(m_Alias), "<alias>"); m_EntriesOptions.add_option("", "", "filter", @@ -203,6 +218,8 @@ WorkspaceShareCommand::WorkspaceShareCommand() m_GetChunkOptions.add_options()("h,help", "Print help"); m_GetChunkOptions.add_option("", "w", "workspace", "Workspace identity (id)", cxxopts::value(m_WorkspaceId), "<workspaceid>"); m_GetChunkOptions.add_option("", "s", "share", "Workspace share identity(id)", cxxopts::value(m_ShareId), "<shareid>"); + m_GetChunkOptions + .add_option("", "a", "alias", "Alias for the share, replaces 'workspace' and 'share' options", cxxopts::value(m_Alias), "<alias>"); m_GetChunkOptions.add_option("", "c", "chunk", "Chunk identity (id)", cxxopts::value(m_ChunkId), "<chunkid>"); m_GetChunkOptions.add_option("", "", "offset", "Offset in chunk", cxxopts::value(m_Offset), "<offset>"); m_GetChunkOptions.add_option("", "", "size", "Size of chunk", cxxopts::value(m_Size), "<size>"); @@ -212,6 +229,8 @@ WorkspaceShareCommand::WorkspaceShareCommand() m_GetChunkBatchOptions.add_options()("h,help", "Print help"); m_GetChunkBatchOptions.add_option("", "s", "share", "Workspace share identity(id)", cxxopts::value(m_ShareId), "<shareid>"); m_GetChunkBatchOptions.add_option("", "w", "workspace", "Workspace identity (id)", cxxopts::value(m_WorkspaceId), "<workspaceid>"); + m_GetChunkBatchOptions + .add_option("", "a", "alias", "Alias for the share, replaces 'workspace' and 'share' options", cxxopts::value(m_Alias), "<alias>"); m_GetChunkBatchOptions.add_option("", "", "chunks", "A list of identities (id)", cxxopts::value(m_ChunkIds), "<chunkids>"); m_GetChunkBatchOptions.parse_positional({"workspace", "share", "chunks"}); m_GetChunkBatchOptions.positional_help("workspace share chunks"); @@ -251,15 +270,45 @@ WorkspaceShareCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** return 0; } - if (m_WorkspaceId.empty()) - { - throw zen::OptionParseException("workspace id is required"); - } - HttpClient Http(m_HostName); if (SubOption == &m_CreateOptions) { + if (!m_WorkspaceRoot.empty()) + { + HttpClient::KeyValueMap Params{{"root_path", m_WorkspaceRoot}}; + if (HttpClient::Response Result = + Http.Put(fmt::format("/ws/{}", m_WorkspaceId.empty() ? Oid::Zero.ToString() : m_WorkspaceId), Params)) + { + if (Oid::Zero == Oid::TryFromHexString(Result.AsText())) + { + throw std::runtime_error(fmt::format("failed to create workspace {} with root path '{}'. Reason: {}", + m_WorkspaceId, + m_WorkspaceRoot, + Result.AsText())); + } + m_WorkspaceId = Result.AsText(); + if (Result.StatusCode == HttpResponseCode::Created) + { + ZEN_CONSOLE("Created workspace {} using root path '{}'", m_WorkspaceId, m_WorkspaceRoot); + } + else + { + ZEN_CONSOLE("Using existing workspace {} with root path '{}'", m_WorkspaceId, m_WorkspaceRoot); + } + } + else + { + Result.ThrowError(fmt::format("failed to create workspace {} with root path '{}'", m_WorkspaceId, m_WorkspaceRoot)); + return 1; + } + } + + if (m_WorkspaceId.empty()) + { + throw zen::OptionParseException("workspace id or root path is required"); + } + if (m_ShareId.empty()) { if (m_SharePath.ends_with(std::filesystem::path::preferred_separator)) @@ -272,6 +321,10 @@ WorkspaceShareCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** } HttpClient::KeyValueMap Params{{"share_path", m_SharePath}}; + if (!m_Alias.empty()) + { + Params.Entries.insert_or_assign("alias", m_Alias); + } if (HttpClient::Response Result = Http.Put(fmt::format("/ws/{}/{}", m_WorkspaceId, m_ShareId), Params)) { @@ -285,14 +338,29 @@ WorkspaceShareCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** } } - if (SubOption == &m_InfoOptions) - { - if (m_ShareId.empty()) + auto GetShareIdentityUrl = [&](const cxxopts::Options& Opts) { + if (m_Alias.empty()) { - throw zen::OptionParseException(fmt::format("share id is required", m_InfoOptions.help())); + if (m_WorkspaceId.empty()) + { + throw zen::OptionParseException("workspace id is required"); + } + + if (m_ShareId.empty()) + { + throw zen::OptionParseException(fmt::format("share id is required", Opts.help())); + } + return fmt::format("{}/{}", m_WorkspaceId, m_ShareId); } + else + { + return fmt::format("share/{}", m_Alias); + } + }; - if (HttpClient::Response Result = Http.Get(fmt::format("/ws/{}/{}", m_WorkspaceId, m_ShareId))) + if (SubOption == &m_InfoOptions) + { + if (HttpClient::Response Result = Http.Get(fmt::format("/ws/{}", GetShareIdentityUrl(m_InfoOptions)))) { ZEN_CONSOLE("{}", Result.ToText()); return 0; @@ -306,11 +374,7 @@ WorkspaceShareCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** if (SubOption == &m_RemoveOptions) { - if (m_ShareId.empty()) - { - throw zen::OptionParseException(fmt::format("share id is required", m_InfoOptions.help())); - } - if (HttpClient::Response Result = Http.Delete(fmt::format("/ws/{}/{}", m_WorkspaceId, m_ShareId))) + if (HttpClient::Response Result = Http.Delete(fmt::format("/ws/{}", GetShareIdentityUrl(m_RemoveOptions)))) { ZEN_CONSOLE("{}", Result); return 0; @@ -324,11 +388,6 @@ WorkspaceShareCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** if (SubOption == &m_FilesOptions) { - if (m_ShareId.empty()) - { - throw zen::OptionParseException(fmt::format("share id is required", m_InfoOptions.help())); - } - HttpClient::KeyValueMap Params; if (!m_FieldFilter.empty()) { @@ -339,7 +398,7 @@ WorkspaceShareCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** Params.Entries.insert_or_assign("refresh", ToString(m_Refresh)); } - if (HttpClient::Response Result = Http.Get(fmt::format("/ws/{}/{}/files", m_WorkspaceId, m_ShareId), {}, Params)) + if (HttpClient::Response Result = Http.Get(fmt::format("/ws/{}/files", GetShareIdentityUrl(m_FilesOptions)), {}, Params)) { ZEN_CONSOLE("{}: {}", Result, Result.ToText()); return 0; @@ -353,11 +412,6 @@ WorkspaceShareCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** if (SubOption == &m_EntriesOptions) { - if (m_ShareId.empty()) - { - throw zen::OptionParseException(fmt::format("share id is required", m_InfoOptions.help())); - } - HttpClient::KeyValueMap Params; if (!m_ChunkId.empty()) { @@ -372,7 +426,7 @@ WorkspaceShareCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** Params.Entries.insert_or_assign("refresh", ToString(m_Refresh)); } - if (HttpClient::Response Result = Http.Get(fmt::format("/ws/{}/{}/entries", m_WorkspaceId, m_ShareId), {}, Params)) + if (HttpClient::Response Result = Http.Get(fmt::format("/ws/{}/entries", GetShareIdentityUrl(m_EntriesOptions)), {}, Params)) { ZEN_CONSOLE("{}: {}", Result, Result.ToText()); return 0; @@ -434,11 +488,6 @@ WorkspaceShareCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** if (SubOption == &m_GetChunkOptions) { - if (m_ShareId.empty()) - { - throw zen::OptionParseException(fmt::format("share id is required", m_InfoOptions.help())); - } - if (m_ChunkId.empty()) { throw zen::OptionParseException("chunk id is required"); @@ -456,7 +505,7 @@ WorkspaceShareCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** Params.Entries.insert_or_assign("size", fmt::format("{}", m_Size)); } - if (HttpClient::Response Result = Http.Get(fmt::format("/ws/{}/{}/{}", m_WorkspaceId, m_ShareId, m_ChunkId), {}, Params)) + if (HttpClient::Response Result = Http.Get(fmt::format("/ws/{}/{}", GetShareIdentityUrl(m_GetChunkOptions), m_ChunkId), {}, Params)) { ZEN_CONSOLE("{}: Bytes: {}", Result, NiceBytes(Result.ResponsePayload.GetSize())); return 0; @@ -493,7 +542,7 @@ WorkspaceShareCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** } IoBuffer Payload = BuildChunkBatchRequest(ChunkRequests); - if (HttpClient::Response Result = Http.Post(fmt::format("/ws/{}/{}/batch", m_WorkspaceId, m_ShareId), Payload)) + if (HttpClient::Response Result = Http.Post(fmt::format("/ws/{}/batch", GetShareIdentityUrl(m_GetChunkBatchOptions)), Payload)) { ZEN_CONSOLE("{}: Bytes: {}", Result, NiceBytes(Result.ResponsePayload.GetSize())); std::vector<IoBuffer> Results = ParseChunkBatchResponse(Result.ResponsePayload); |