Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/windows/wslc/commands/RootCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ Module Name:
#include "SettingsCommand.h"
#include "VersionCommand.h"

#include "wslutil.h"
#include "VersionService.h"

using namespace wsl::windows::wslc::execution;
using namespace wsl::shared;
using namespace wsl::windows::common;
using namespace wsl::windows::wslc::services;

namespace wsl::windows::wslc {
std::vector<std::unique_ptr<Command>> RootCommand::GetCommands() const
Expand Down Expand Up @@ -79,7 +84,7 @@ void RootCommand::ExecuteInternal(CLIExecutionContext& context) const
{
if (context.Args.Contains(ArgType::Version))
{
VersionCommand::PrintVersion();
wslutil::PrintMessage(VersionService::GetVersionString());
return;
}

Expand Down
18 changes: 11 additions & 7 deletions src/windows/wslc/commands/VersionCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ Module Name:

--*/
#include "VersionCommand.h"
#include "VersionTasks.h"

using namespace wsl::shared;
using namespace wsl::windows::wslc::execution;
using namespace wsl::windows::wslc::task;

namespace wsl::windows::wslc {
std::vector<Argument> VersionCommand::GetArguments() const
{
return {
Argument::Create(ArgType::Format),
};
}

std::wstring VersionCommand::ShortDescription() const
{
return Localization::WSLCCLI_VersionDesc();
Expand All @@ -27,14 +36,9 @@ std::wstring VersionCommand::LongDescription() const
return Localization::WSLCCLI_VersionLongDesc();
}

void VersionCommand::PrintVersion()
{
wsl::windows::common::wslutil::PrintMessage(std::format(L"{} {}", s_ExecutableName, WSL_PACKAGE_VERSION));
}

void VersionCommand::ExecuteInternal(CLIExecutionContext& context) const
{
UNREFERENCED_PARAMETER(context);
PrintVersion();
context << GetVersionInfo //
<< ListVersionInfo;
Comment thread
AmelBawa-msft marked this conversation as resolved.
}
} // namespace wsl::windows::wslc
2 changes: 1 addition & 1 deletion src/windows/wslc/commands/VersionCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct VersionCommand final : public Command
VersionCommand(const std::wstring& parent) : Command(CommandName, parent)
{
}
static void PrintVersion();
std::vector<Argument> GetArguments() const override;
std::wstring ShortDescription() const override;
std::wstring LongDescription() const override;

Expand Down
3 changes: 3 additions & 0 deletions src/windows/wslc/core/ExecutionContextData.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Module Name:
#include "EnumVariantMap.h"
#include "ContainerModel.h"
#include "ImageModel.h"
#include "VersionModel.h"
#include "SessionModel.h"

#include <string>
Expand All @@ -36,6 +37,7 @@ enum class Data : size_t
Containers,
ContainerOptions,
Images,
Version,

Max
};
Expand All @@ -50,6 +52,7 @@ namespace details {
DEFINE_DATA_MAPPING(Containers, std::vector<wsl::windows::wslc::models::ContainerInformation>);
DEFINE_DATA_MAPPING(ContainerOptions, wsl::windows::wslc::models::ContainerOptions);
DEFINE_DATA_MAPPING(Images, std::vector<wsl::windows::wslc::models::ImageInformation>);
DEFINE_DATA_MAPPING(Version, wsl::windows::wslc::models::VersionInfo);
} // namespace details

struct DataMap : wsl::windows::wslc::EnumBasedVariantMap<Data, wsl::windows::wslc::execution::details::DataMapping>
Expand Down
90 changes: 90 additions & 0 deletions src/windows/wslc/services/VersionModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*++

Copyright (c) Microsoft. All rights reserved.

Module Name:

VersionModel.h

Abstract:

This file contains the VersionModel definitions
--*/

#pragma once

namespace wsl::windows::wslc::models {

#define WSLC_VERSION_ROW(key, value) std::format("{:<20}{}\n", key ":", value)

struct ClientVersion
{
std::string Version = WSL_PACKAGE_VERSION;
std::string GitCommit = COMMIT_HASH;
std::string Built = __DATE__ " " __TIME__;
std::string Os = "windows";

#if defined(_AMD64_)
std::string Arch = "amd64";
#elif defined(_ARM64_)
std::string Arch = "arm64";
#else
std::string Arch = "unknown";
#endif

NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(ClientVersion, Version, GitCommit, Built, Os, Arch);

Comment thread
AmelBawa-msft marked this conversation as resolved.
std::string ToString() const
{
std::stringstream ss;
ss << "Client:\n";
ss << WSLC_VERSION_ROW(" Version", Version);
ss << WSLC_VERSION_ROW(" Git commit", GitCommit);
ss << WSLC_VERSION_ROW(" Built", Built);
ss << WSLC_VERSION_ROW(" OS/Arch", Os + "/" + Arch);
return ss.str();
}
};

struct ServerVersion
{
std::string Kernel = KERNEL_VERSION;
std::string WSLg = WSLG_VERSION;
std::string MSRDC = MSRDC_VERSION;
std::string Direct3D = DIRECT3D_VERSION;
std::string DXCore = DXCORE_VERSION;
std::string Windows = wsl::windows::common::helpers::GetWindowsVersionString();

NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(ServerVersion, Kernel, WSLg, MSRDC, Direct3D, DXCore, Windows);

Comment thread
AmelBawa-msft marked this conversation as resolved.
std::string ToString() const
{
std::stringstream ss;
ss << "Server:\n";
ss << WSLC_VERSION_ROW(" Linux kernel", Kernel);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These strings should be loc'd for table output in the CLI.

ss << WSLC_VERSION_ROW(" WSLg", WSLg);
ss << WSLC_VERSION_ROW(" MSRDC", MSRDC);
ss << WSLC_VERSION_ROW(" Direct3D", Direct3D);
ss << WSLC_VERSION_ROW(" DXCore", DXCore);
ss << WSLC_VERSION_ROW(" Windows", Windows);
return ss.str();
}
};

struct VersionInfo
{
ClientVersion Client{};
ServerVersion Server{};

NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(VersionInfo, Client, Server);

std::string ToString() const
{
std::stringstream ss;
ss << Client.ToString() << "\n";
ss << Server.ToString();
return ss.str();
}
};
#undef WSLC_VERSION_ROW
} // namespace wsl::windows::wslc::models
33 changes: 33 additions & 0 deletions src/windows/wslc/services/VersionService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*++

Copyright (c) Microsoft. All rights reserved.

Module Name:

VersionService.cpp

Abstract:

This file contains the VersionService implementation
--*/

#include "precomp.h"
#include "VersionService.h"
#include "Command.h"

namespace wsl::windows::wslc::services {

using namespace wsl::shared::string;

std::wstring VersionService::GetVersionString()
{
return std::format(L"{} {}", s_ExecutableName, MultiByteToWide(VersionInfo().Client.Version));
Comment thread
AmelBawa-msft marked this conversation as resolved.
}

const wsl::windows::wslc::models::VersionInfo& VersionService::VersionInfo()
{
static const wsl::windows::wslc::models::VersionInfo s_versionInfo{};
return s_versionInfo;
}

} // namespace wsl::windows::wslc::services
24 changes: 24 additions & 0 deletions src/windows/wslc/services/VersionService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*++

Copyright (c) Microsoft. All rights reserved.

Module Name:

VersionService.h

Abstract:

This file contains the VersionService definition
--*/
#pragma once

#include "VersionModel.h"
#include <wslc.h>

namespace wsl::windows::wslc::services {
struct VersionService
{
static std::wstring GetVersionString();
static const wsl::windows::wslc::models::VersionInfo& VersionInfo();
};
} // namespace wsl::windows::wslc::services
66 changes: 66 additions & 0 deletions src/windows/wslc/tasks/VersionTasks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*++

Copyright (c) Microsoft. All rights reserved.

Module Name:

VersionTasks.cpp

Abstract:

Implementation of version command related execution logic.

--*/
#include "precomp.h"
#include "Argument.h"
#include "ArgumentValidation.h"
#include "ContainerModel.h"
#include "VersionTasks.h"
#include "VersionService.h"
#include "CLIExecutionContext.h"
#include <wil/result_macros.h>
#include <wslc_schema.h>

using namespace wsl::shared;
using namespace wsl::shared::string;
using namespace wsl::windows::wslc::execution;
using namespace wsl::windows::wslc::models;
using namespace wsl::windows::wslc::services;
using namespace wsl::windows::common::wslutil;

namespace wsl::windows::wslc::task {

void GetVersionInfo(CLIExecutionContext& context)
{
context.Data.Add<Data::Version>(VersionService::VersionInfo());
}

void ListVersionInfo(CLIExecutionContext& context)
{
WI_ASSERT(context.Data.Contains(Data::Version));
auto& versionInfo = context.Data.Get<Data::Version>();

FormatType format = FormatType::Table; // Default is table
if (context.Args.Contains(ArgType::Format))
{
format = validation::GetFormatTypeFromString(context.Args.Get<ArgType::Format>());
}

switch (format)
{
case FormatType::Json:
{
auto json = ToJson(versionInfo, c_jsonPrettyPrintIndent);
PrintMessage(MultiByteToWide(json));
break;
}
case FormatType::Table:
{
PrintMessage(MultiByteToWide(versionInfo.ToString()));
Comment thread
AmelBawa-msft marked this conversation as resolved.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use the table output format all the other tables use.

break;
}
default:
THROW_HR(E_UNEXPECTED);
}
}
} // namespace wsl::windows::wslc::task
22 changes: 22 additions & 0 deletions src/windows/wslc/tasks/VersionTasks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*++

Copyright (c) Microsoft. All rights reserved.

Module Name:

VersionTasks.h

Abstract:

Declaration of version command execution tasks.
--*/
#pragma once
#include "CLIExecutionContext.h"
#include "Task.h"

using wsl::windows::wslc::execution::CLIExecutionContext;

namespace wsl::windows::wslc::task {
void GetVersionInfo(CLIExecutionContext& context);
void ListVersionInfo(CLIExecutionContext& context);
} // namespace wsl::windows::wslc::task
10 changes: 5 additions & 5 deletions test/windows/wslc/WSLCCLICommandUnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ class WSLCCLICommandUnitTests
VERIFY_ARE_EQUAL(0u, cmd.GetCommands().size());
}

// Test: Verify VersionCommand has no arguments (only the auto-added --help)
TEST_METHOD(VersionCommand_HasNoArguments)
// Test: Verify VersionCommand has the expected arguments (--format, plus the auto-added --help)
TEST_METHOD(VersionCommand_HasFormatArgument)
{
auto cmd = VersionCommand(L"wslc");
VERIFY_ARE_EQUAL(0u, cmd.GetArguments().size());
// Test out that auto added help command is the only one
VERIFY_ARE_EQUAL(1u, cmd.GetAllArguments().size());
VERIFY_ARE_EQUAL(1u, cmd.GetArguments().size());
// Test out that --format and auto-added --help are the only arguments
VERIFY_ARE_EQUAL(2u, cmd.GetAllArguments().size());
}

// Test: Verify RootCommand contains VersionCommand as a subcommand
Expand Down
6 changes: 6 additions & 0 deletions test/windows/wslc/WSLCCLIExecutionUnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ class WSLCCLIExecutionUnitTests
dataMap.Add<Data::Images>(std::move(images));
handled = true;
}
else if (dataType == Data::Version)
{
wsl::windows::wslc::models::VersionInfo versionInfo;
dataMap.Add<Data::Version>(std::move(versionInfo));
handled = true;
}

if (!handled)
{
Expand Down
5 changes: 0 additions & 5 deletions test/windows/wslc/e2e/WSLCE2EGlobalTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ class WSLCE2EGlobalTests
RunWslcAndVerify(L"INVALID_CMD", {.Stdout = GetHelpMessage(), .Stderr = L"Unrecognized command: 'INVALID_CMD'\r\n", .ExitCode = 1});
}

WSLC_TEST_METHOD(WSLCE2E_VersionCommand)
{
RunWslcAndVerify(L"version", {.Stdout = GetVersionMessage(), .Stderr = L"", .ExitCode = 0});
}

WSLC_TEST_METHOD(WSLCE2E_VersionFlag)
{
RunWslcAndVerify(L"--version", {.Stdout = GetVersionMessage(), .Stderr = L"", .ExitCode = 0});
Expand Down
Loading