Skip to content

Commit f5e1b45

Browse files
committed
Init volume command
1 parent 01706c9 commit f5e1b45

19 files changed

Lines changed: 863 additions & 0 deletions

localization/strings/en-US/Resources.resw

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,4 +2505,41 @@ On first run, creates the file with all settings commented out at their defaults
25052505
<data name="WSLCCLI_ImageLoadNoInputError" xml:space="preserve">
25062506
<value>Requested load but no input provided.</value>
25072507
</data>
2508+
<data name="WSLCCLI_VolumeCommandDesc" xml:space="preserve">
2509+
<value>Manage volumes.</value>
2510+
</data>
2511+
<data name="WSLCCLI_VolumeCommandLongDesc" xml:space="preserve">
2512+
<value>Manage the lifecycle of WSL volumes, including creating, inspecting, listing, and deleting them.</value>
2513+
<comment>{Locked="WSL"}Product names should not be translated</comment>
2514+
</data>
2515+
<data name="WSLCCLI_VolumeCreateDesc" xml:space="preserve">
2516+
<value>Create a volume.</value>
2517+
</data>
2518+
<data name="WSLCCLI_VolumeCreateLongDesc" xml:space="preserve">
2519+
<value>Creates a named volume that can be attached to containers.</value>
2520+
</data>
2521+
<data name="WSLCCLI_VolumeDeleteDesc" xml:space="preserve">
2522+
<value>Remove one or more volumes.</value>
2523+
</data>
2524+
<data name="WSLCCLI_VolumeDeleteLongDesc" xml:space="preserve">
2525+
<value>Removes one or more volumes. A volume cannot be removed if it is in use by a container.</value>
2526+
</data>
2527+
<data name="WSLCCLI_VolumeInspectDesc" xml:space="preserve">
2528+
<value>Display detailed information on one or more volumes.</value>
2529+
</data>
2530+
<data name="WSLCCLI_VolumeInspectLongDesc" xml:space="preserve">
2531+
<value>Display detailed information on one or more volumes.</value>
2532+
</data>
2533+
<data name="WSLCCLI_VolumeListDesc" xml:space="preserve">
2534+
<value>List volumes.</value>
2535+
</data>
2536+
<data name="WSLCCLI_VolumeListLongDesc" xml:space="preserve">
2537+
<value>Lists all volumes in the session.</value>
2538+
</data>
2539+
<data name="WSLCCLI_VolumeNameArgDescription" xml:space="preserve">
2540+
<value>Volume name</value>
2541+
</data>
2542+
<data name="WSLCCLI_VolumeDriverArgDescription" xml:space="preserve">
2543+
<value>Specify volume driver name</value>
2544+
</data>
25082545
</root>

src/windows/wslc/arguments/ArgumentDefinitions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,7 @@ _(Verbose, "verbose", NO_ALIAS, Kind::Flag, L
8383
_(Version, "version", L"v", Kind::Flag, Localization::WSLCCLI_VersionArgDescription()) \
8484
/*_(Virtual, "virtualization", NO_ALIAS, Kind::Value, Localization::WSLCCLI_VirtualArgDescription())*/ \
8585
_(Volume, "volume", L"v", Kind::Value, Localization::WSLCCLI_VolumeArgDescription()) \
86+
_(VolumeDriver, "driver", L"d", Kind::Value, Localization::WSLCCLI_VolumeDriverArgDescription()) \
87+
_(VolumeName, "volume-name", NO_ALIAS, Kind::Positional, Localization::WSLCCLI_VolumeNameArgDescription()) \
8688
_(WorkDir, "workdir", L"w", Kind::Value, Localization::WSLCCLI_WorkingDirArgDescription()) \
8789
// clang-format on

src/windows/wslc/arguments/ArgumentValidation.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ void Argument::Validate(const ArgMap& execArgs) const
4747
validation::ValidateVolumeMount(execArgs.GetAll<ArgType::Volume>());
4848
break;
4949

50+
case ArgType::VolumeDriver:
51+
validation::ValidateVolumeDriver(execArgs.GetAll<ArgType::VolumeDriver>(), m_name);
52+
break;
53+
5054
case ArgType::WorkDir:
5155
{
5256
const auto& value = execArgs.Get<ArgType::WorkDir>();
@@ -97,6 +101,29 @@ void ValidateVolumeMount(const std::vector<std::wstring>& values)
97101
}
98102
}
99103

104+
void ValidateVolumeDriver(const std::vector<std::wstring>& values, const std::wstring& argName)
105+
{
106+
static const std::vector<std::wstring> supportedDrivers = {L"vhd"};
107+
for (const auto& value : values)
108+
{
109+
bool found = false;
110+
for (const auto& driver : supportedDrivers)
111+
{
112+
if (IsEqual(value, driver))
113+
{
114+
found = true;
115+
break;
116+
}
117+
}
118+
119+
if (!found)
120+
{
121+
throw ArgumentException(std::format(
122+
L"Invalid {} value: {} is not a recognized volume driver. Supported drivers are: vhd.", argName, value));
123+
}
124+
}
125+
}
126+
100127
// Convert string to WSLCSignal enum - accepts either signal name (e.g., "SIGKILL") or number (e.g., "9")
101128
WSLCSignal GetWSLCSignalFromString(const std::wstring& input, const std::wstring& argName)
102129
{

src/windows/wslc/arguments/ArgumentValidation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@ void ValidateFormatTypeFromString(const std::vector<std::wstring>& values, const
6161
FormatType GetFormatTypeFromString(const std::wstring& input, const std::wstring& argName = {});
6262

6363
void ValidateVolumeMount(const std::vector<std::wstring>& values);
64+
void ValidateVolumeDriver(const std::vector<std::wstring>& values, const std::wstring& argName);
6465

6566
} // namespace wsl::windows::wslc::validation

src/windows/wslc/commands/RootCommand.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Module Name:
1919
#include "SessionCommand.h"
2020
#include "SettingsCommand.h"
2121
#include "VersionCommand.h"
22+
#include "VolumeCommand.h"
2223

2324
using namespace wsl::windows::wslc::execution;
2425
using namespace wsl::shared;
@@ -31,6 +32,7 @@ std::vector<std::unique_ptr<Command>> RootCommand::GetCommands() const
3132
commands.push_back(std::make_unique<ImageCommand>(FullName()));
3233
commands.push_back(std::make_unique<SessionCommand>(FullName()));
3334
commands.push_back(std::make_unique<SettingsCommand>(FullName()));
35+
commands.push_back(std::make_unique<VolumeCommand>(FullName()));
3436
commands.push_back(std::make_unique<ContainerAttachCommand>(FullName()));
3537
commands.push_back(std::make_unique<ImageBuildCommand>(FullName()));
3638
commands.push_back(std::make_unique<ContainerCreateCommand>(FullName()));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*++
2+
3+
Copyright (c) Microsoft. All rights reserved.
4+
5+
Module Name:
6+
7+
VolumeCommand.cpp
8+
9+
Abstract:
10+
11+
Implementation of command execution logic.
12+
13+
--*/
14+
#include "CLIExecutionContext.h"
15+
#include "VolumeCommand.h"
16+
17+
using namespace wsl::windows::wslc::execution;
18+
using namespace wsl::shared;
19+
20+
namespace wsl::windows::wslc {
21+
// Volume Root Command
22+
std::vector<std::unique_ptr<Command>> VolumeCommand::GetCommands() const
23+
{
24+
std::vector<std::unique_ptr<Command>> commands;
25+
commands.push_back(std::make_unique<VolumeCreateCommand>(FullName()));
26+
commands.push_back(std::make_unique<VolumeDeleteCommand>(FullName()));
27+
commands.push_back(std::make_unique<VolumeInspectCommand>(FullName()));
28+
commands.push_back(std::make_unique<VolumeListCommand>(FullName()));
29+
return commands;
30+
}
31+
32+
std::vector<Argument> VolumeCommand::GetArguments() const
33+
{
34+
return {};
35+
}
36+
37+
std::wstring VolumeCommand::ShortDescription() const
38+
{
39+
return Localization::WSLCCLI_VolumeCommandDesc();
40+
}
41+
42+
std::wstring VolumeCommand::LongDescription() const
43+
{
44+
return Localization::WSLCCLI_VolumeCommandLongDesc();
45+
}
46+
47+
void VolumeCommand::ExecuteInternal(CLIExecutionContext& context) const
48+
{
49+
OutputHelp();
50+
}
51+
} // namespace wsl::windows::wslc
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*++
2+
3+
Copyright (c) Microsoft. All rights reserved.
4+
5+
Module Name:
6+
7+
VolumeCommand.h
8+
9+
Abstract:
10+
11+
Declaration of command classes and interfaces.
12+
13+
--*/
14+
#pragma once
15+
#include "Command.h"
16+
17+
namespace wsl::windows::wslc {
18+
// Root Volume Command
19+
struct VolumeCommand final : public Command
20+
{
21+
constexpr static std::wstring_view CommandName = L"volume";
22+
VolumeCommand(const std::wstring& parent) : Command(CommandName, parent)
23+
{
24+
}
25+
std::vector<Argument> GetArguments() const override;
26+
std::wstring ShortDescription() const override;
27+
std::wstring LongDescription() const override;
28+
29+
std::vector<std::unique_ptr<Command>> GetCommands() const override;
30+
31+
protected:
32+
void ExecuteInternal(CLIExecutionContext& context) const override;
33+
};
34+
35+
// Create Command
36+
struct VolumeCreateCommand final : public Command
37+
{
38+
constexpr static std::wstring_view CommandName = L"create";
39+
VolumeCreateCommand(const std::wstring& parent) : Command(CommandName, parent)
40+
{
41+
}
42+
std::vector<Argument> GetArguments() const override;
43+
std::wstring ShortDescription() const override;
44+
std::wstring LongDescription() const override;
45+
46+
protected:
47+
void ExecuteInternal(CLIExecutionContext& context) const override;
48+
};
49+
50+
// Delete Command
51+
struct VolumeDeleteCommand final : public Command
52+
{
53+
constexpr static std::wstring_view CommandName = L"remove";
54+
VolumeDeleteCommand(const std::wstring& parent) : Command(CommandName, {L"delete", L"rm"}, parent)
55+
{
56+
}
57+
std::vector<Argument> GetArguments() const override;
58+
std::wstring ShortDescription() const override;
59+
std::wstring LongDescription() const override;
60+
61+
protected:
62+
void ExecuteInternal(CLIExecutionContext& context) const override;
63+
};
64+
65+
// Inspect Command
66+
struct VolumeInspectCommand final : public Command
67+
{
68+
constexpr static std::wstring_view CommandName = L"inspect";
69+
VolumeInspectCommand(const std::wstring& parent) : Command(CommandName, parent)
70+
{
71+
}
72+
std::vector<Argument> GetArguments() const override;
73+
std::wstring ShortDescription() const override;
74+
std::wstring LongDescription() const override;
75+
76+
protected:
77+
void ExecuteInternal(CLIExecutionContext& context) const override;
78+
};
79+
80+
// List Command
81+
struct VolumeListCommand final : public Command
82+
{
83+
constexpr static std::wstring_view CommandName = L"list";
84+
VolumeListCommand(const std::wstring& parent) : Command(CommandName, {L"ls"}, parent)
85+
{
86+
}
87+
std::vector<Argument> GetArguments() const override;
88+
std::wstring ShortDescription() const override;
89+
std::wstring LongDescription() const override;
90+
91+
protected:
92+
void ValidateArgumentsInternal(const ArgMap& execArgs) const override;
93+
void ExecuteInternal(CLIExecutionContext& context) const override;
94+
};
95+
} // namespace wsl::windows::wslc
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*++
2+
3+
Copyright (c) Microsoft. All rights reserved.
4+
5+
Module Name:
6+
7+
VolumeCreateCommand.cpp
8+
9+
Abstract:
10+
11+
Implementation of command execution logic.
12+
13+
--*/
14+
15+
#include "VolumeCommand.h"
16+
#include "CLIExecutionContext.h"
17+
#include "SessionTasks.h"
18+
#include "VolumeTasks.h"
19+
#include "Task.h"
20+
21+
using namespace wsl::windows::wslc::execution;
22+
using namespace wsl::windows::wslc::task;
23+
using namespace wsl::shared;
24+
25+
namespace wsl::windows::wslc {
26+
// Volume Create Command
27+
std::vector<Argument> VolumeCreateCommand::GetArguments() const
28+
{
29+
return {
30+
Argument::Create(ArgType::VolumeName, true),
31+
Argument::Create(ArgType::VolumeDriver),
32+
Argument::Create(ArgType::Session),
33+
};
34+
}
35+
36+
std::wstring VolumeCreateCommand::ShortDescription() const
37+
{
38+
return Localization::WSLCCLI_VolumeCreateDesc();
39+
}
40+
41+
std::wstring VolumeCreateCommand::LongDescription() const
42+
{
43+
return Localization::WSLCCLI_VolumeCreateLongDesc();
44+
}
45+
46+
// clang-format off
47+
void VolumeCreateCommand::ExecuteInternal(CLIExecutionContext& context) const
48+
{
49+
context
50+
<< CreateSession
51+
<< CreateVolume;
52+
}
53+
// clang-format on
54+
} // namespace wsl::windows::wslc
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*++
2+
3+
Copyright (c) Microsoft. All rights reserved.
4+
5+
Module Name:
6+
7+
VolumeDeleteCommand.cpp
8+
9+
Abstract:
10+
11+
Implementation of command execution logic.
12+
13+
--*/
14+
15+
#include "VolumeCommand.h"
16+
#include "CLIExecutionContext.h"
17+
#include "SessionTasks.h"
18+
#include "VolumeTasks.h"
19+
#include "Task.h"
20+
21+
using namespace wsl::windows::wslc::execution;
22+
using namespace wsl::windows::wslc::task;
23+
using namespace wsl::shared;
24+
25+
namespace wsl::windows::wslc {
26+
// Volume Delete Command
27+
std::vector<Argument> VolumeDeleteCommand::GetArguments() const
28+
{
29+
return {
30+
Argument::Create(ArgType::VolumeName, true, NO_LIMIT),
31+
Argument::Create(ArgType::Session),
32+
};
33+
}
34+
35+
std::wstring VolumeDeleteCommand::ShortDescription() const
36+
{
37+
return Localization::WSLCCLI_VolumeDeleteDesc();
38+
}
39+
40+
std::wstring VolumeDeleteCommand::LongDescription() const
41+
{
42+
return Localization::WSLCCLI_VolumeDeleteLongDesc();
43+
}
44+
45+
// clang-format off
46+
void VolumeDeleteCommand::ExecuteInternal(CLIExecutionContext& context) const
47+
{
48+
context
49+
<< CreateSession
50+
<< DeleteVolumes;
51+
}
52+
// clang-format on
53+
} // namespace wsl::windows::wslc

0 commit comments

Comments
 (0)