master
cpp 142 lines 3.74 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 RegistryCommand.cpp
8
9 Abstract:
10
11 Implementation of the registry command tree (login, logout).
12
13 --*/
14
15 #include "CLIExecutionContext.h"
16 #include "RegistryCommand.h"
17 #include "RegistryTasks.h"
18 #include "SessionTasks.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
27 // Registry Root Command
28 std::vector<std::unique_ptr<Command>> RegistryCommand::GetCommands() const
29 {
30 std::vector<std::unique_ptr<Command>> commands;
31 commands.push_back(std::make_unique<RegistryLoginCommand>(FullName()));
32 commands.push_back(std::make_unique<RegistryLogoutCommand>(FullName()));
33 return commands;
34 }
35
36 std::vector<Argument> RegistryCommand::GetArguments() const
37 {
38 return {};
39 }
40
41 std::wstring RegistryCommand::ShortDescription() const
42 {
43 return Localization::WSLCCLI_RegistryCommandDesc();
44 }
45
46 std::wstring RegistryCommand::LongDescription() const
47 {
48 return Localization::WSLCCLI_RegistryCommandLongDesc();
49 }
50
51 void RegistryCommand::ExecuteInternal(CLIExecutionContext& context) const
52 {
53 OutputHelp(context.Terminal);
54 }
55
56 // Registry Login Command
57 std::vector<Argument> RegistryLoginCommand::GetArguments() const
58 {
59 return {
60 Argument::Create(ArgType::Password),
61 Argument::Create(ArgType::PasswordStdin),
62 Argument::Create(ArgType::Username),
63 Argument::Create(ArgType::Server),
64 };
65 }
66
67 std::wstring RegistryLoginCommand::ShortDescription() const
68 {
69 return Localization::WSLCCLI_LoginDesc();
70 }
71
72 std::wstring RegistryLoginCommand::LongDescription() const
73 {
74 return Localization::WSLCCLI_LoginLongDesc();
75 }
76
77 void RegistryLoginCommand::ValidateArgumentsInternal(ArgMap& execArgs) const
78 {
79 if (execArgs.Contains(ArgType::Password) && execArgs.GetValue<ArgType::PasswordStdin>())
80 {
81 throw ArgumentException(
82 Localization::WSLCCLI_LoginPasswordAndStdinMutuallyExclusive(), GetArgumentsForHelp({ArgType::Password, ArgType::PasswordStdin}));
83 }
84
85 if (execArgs.GetValue<ArgType::PasswordStdin>() && !execArgs.Contains(ArgType::Username))
86 {
87 throw ArgumentException(
88 Localization::WSLCCLI_LoginPasswordStdinRequiresUsername(), GetArgumentsForHelp({ArgType::PasswordStdin, ArgType::Username}));
89 }
90 }
91
92 void RegistryLoginCommand::ExecuteInternal(CLIExecutionContext& context) const
93 {
94 if (!context.Args.Contains(ArgType::Username))
95 {
96 auto username = context.Terminal.PromptForLine(Localization::WSLCCLI_LoginUsernamePrompt());
97 context.Args.Add(ArgType::Username, std::move(username));
98 }
99
100 // Resolve password: --password, --password-stdin, or interactive prompt.
101 if (!context.Args.Contains(ArgType::Password))
102 {
103 if (context.Args.GetValue<ArgType::PasswordStdin>())
104 {
105 context.Args.Add(ArgType::Password, context.Terminal.ReadLine().value_or(std::wstring{}));
106 }
107 else
108 {
109 auto password = context.Terminal.PromptForLine(Localization::WSLCCLI_LoginPasswordPrompt(), true);
110 context.Args.Add(ArgType::Password, std::move(password));
111 }
112 }
113
114 context //
115 << ResolveSession << Login;
116 }
117
118 // Registry Logout Command
119 std::vector<Argument> RegistryLogoutCommand::GetArguments() const
120 {
121 return {
122 Argument::Create(ArgType::Server),
123 };
124 }
125
126 std::wstring RegistryLogoutCommand::ShortDescription() const
127 {
128 return Localization::WSLCCLI_LogoutDesc();
129 }
130
131 std::wstring RegistryLogoutCommand::LongDescription() const
132 {
133 return Localization::WSLCCLI_LogoutLongDesc();
134 }
135
136 void RegistryLogoutCommand::ExecuteInternal(CLIExecutionContext& context) const
137 {
138 context //
139 << Logout;
140 }
141
142 } // namespace wsl::windows::wslc