forked from microsoft/WSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwslinfo.cpp
More file actions
154 lines (120 loc) · 3.59 KB
/
wslinfo.cpp
File metadata and controls
154 lines (120 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
wslinfo.cpp
Abstract:
This file wslpath function definitions.
--*/
#include "common.h"
#include <iostream>
#include <assert.h>
#include "getopt.h"
#include "util.h"
#include "wslpath.h"
#include "wslinfo.h"
#include "lxinitshared.h"
#include "defs.h"
#include "Localization.h"
#include "CommandLine.h"
enum class WslInfoMode
{
GetNetworkingMode,
MsalProxyPath,
WslVersion
};
int WslInfoEntry(int Argc, char* Argv[])
/*++
Routine Description:
This routine is the entrypoint for the wslinfo binary.
Arguments:
Argc - Supplies the argument count.
Argv - Supplies the command line arguments.
Return Value:
0 on success, 1 on failure.
--*/
{
using namespace wsl::shared;
constexpr auto Usage = std::bind(Localization::MessageWslInfoUsage, Localization::Options::Default);
std::optional<WslInfoMode> Mode;
bool noNewLine = false;
ArgumentParser parser(Argc, Argv);
parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::GetNetworkingMode>{Mode, Usage}, WSLINFO_NETWORKING_MODE);
parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::MsalProxyPath>{Mode, Usage}, WSLINFO_MSAL_PROXY_PATH);
parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::WslVersion>{Mode, Usage}, WSLINFO_WSL_VERSION);
parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::WslVersion>{Mode, Usage}, WSLINFO_WSL_VERSION_LEGACY);
parser.AddArgument(NoOp{}, WSLINFO_WSL_HELP);
parser.AddArgument(noNewLine, nullptr, WSLINFO_NO_NEWLINE);
try
{
parser.Parse();
}
catch (const wil::ExceptionWithUserMessage& e)
{
std::cerr << e.what() << "\n";
return 1;
}
if (!Mode.has_value())
{
std::cerr << Usage() << "\n";
return 1;
}
else if (Mode.value() == WslInfoMode::GetNetworkingMode)
{
if (UtilIsUtilityVm())
{
auto NetworkingMode = UtilGetNetworkingMode();
if (!NetworkingMode)
{
std::cerr << Localization::MessageFailedToQueryNetworkingMode() << "\n";
return 1;
}
switch (NetworkingMode.value())
{
case LxMiniInitNetworkingModeNat:
std::cout << "nat";
break;
case LxMiniInitNetworkingModeBridged:
std::cout << "bridged";
break;
case LxMiniInitNetworkingModeMirrored:
std::cout << "mirrored";
break;
case LxMiniInitNetworkingModeVirtioProxy:
std::cout << "virtioproxy";
break;
default:
std::cout << "none";
break;
}
}
else
{
std::cout << "wsl1";
}
}
else if (Mode.value() == WslInfoMode::MsalProxyPath)
{
auto value = UtilGetEnvironmentVariable(LX_WSL2_INSTALL_PATH);
if (value.empty())
{
std::cerr << Localization::MessageNoValueFound() << "\n";
return 1;
}
auto translatedPath = WslPathTranslate(value.data(), TRANSLATE_FLAG_ABSOLUTE, TRANSLATE_MODE_UNIX);
if (translatedPath.empty())
{
std::cerr << Localization::MessageFailedToTranslatePath() << "\n";
return 1;
}
std::cout << translatedPath << "/msal.wsl.proxy.exe";
}
else if (Mode.value() == WslInfoMode::WslVersion)
{
std::cout << WSL_PACKAGE_VERSION;
}
if (!noNewLine)
{
std::cout << '\n';
}
return 0;
}