-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathConfigCommand.cs
More file actions
203 lines (166 loc) · 7.63 KB
/
ConfigCommand.cs
File metadata and controls
203 lines (166 loc) · 7.63 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.CommandLine;
using System.CommandLine.Help;
using System.Threading.Tasks;
using NuGet.CommandLine.XPlat.Commands;
using NuGet.Common;
namespace NuGet.CommandLine.XPlat
{
internal class ConfigCommand
{
private static HelpOption HelpOption = new HelpOption()
{
Arity = ArgumentArity.Zero
};
private static Argument<string> SetConfigKeyArgument = new Argument<string>(name: "config-key")
{
Arity = ArgumentArity.ExactlyOne,
Description = Strings.ConfigSetConfigKeyDescription,
};
private static Argument<string> UnsetConfigKeyArgument = new Argument<string>(name: "config-key")
{
Arity = ArgumentArity.ExactlyOne,
Description = Strings.ConfigUnsetConfigKeyDescription,
};
private static Argument<string> ConfigValueArgument = new Argument<string>(name: "config-value")
{
Arity = ArgumentArity.ExactlyOne,
Description = Strings.ConfigSetConfigValueDescription,
};
private static Argument<string> AllOrConfigKeyArgument = new Argument<string>(name: "all-or-config-key")
{
Arity = ArgumentArity.ExactlyOne,
Description = Strings.ConfigGetAllOrConfigKeyDescription
};
private static Option<string> WorkingDirectory = new Option<string>(name: "--working-directory")
{
Arity = ArgumentArity.ZeroOrOne,
Description = Strings.ConfigPathsWorkingDirectoryDescription
};
private static Option<bool> ShowPathOption = new Option<bool>(name: "--show-path")
{
Arity = ArgumentArity.Zero,
Description = Strings.ConfigGetShowPathDescription,
};
private static Option<string> ConfigFileOption = new Option<string>(name: "--configfile")
{
Arity = ArgumentArity.ZeroOrOne,
Description = Strings.Option_ConfigFile,
};
internal static void LogException(Exception e, ILogger log)
{
// Log the error
if (ExceptionLogger.Instance.ShowStack)
{
log.LogError(e.ToString());
}
else
{
log.LogError(ExceptionUtilities.DisplayMessage(e));
}
// Log the stack trace as verbose output.
log.LogVerbose(e.ToString());
}
internal static Command Register(Command app, Func<ILoggerWithColor> getLogger)
{
var ConfigCmd = new DocumentedCommand(name: "config", description: Strings.Config_Description, "https://aka.ms/dotnet/nuget/config");
ConfigCmd.Options.Add(HelpOption);
// Options directly under the verb 'config'
// noun sub-command: config paths
var PathsCmd = new DocumentedCommand(name: "paths", description: Strings.ConfigPathsCommandDescription, "https://aka.ms/dotnet/nuget/config/paths");
// Options under sub-command: config paths
RegisterOptionsForCommandConfigPaths(PathsCmd, getLogger);
ConfigCmd.Subcommands.Add(PathsCmd);
// noun sub-command: config get
var GetCmd = new DocumentedCommand(name: "get", description: Strings.ConfigGetCommandDescription, "https://aka.ms/dotnet/nuget/config/get");
// Options under sub-command: config get
RegisterOptionsForCommandConfigGet(GetCmd, getLogger);
ConfigCmd.Subcommands.Add(GetCmd);
// noun sub-command: config set
var SetCmd = new DocumentedCommand(name: "set", description: Strings.ConfigSetCommandDescription, "https://aka.ms/dotnet/nuget/config/set");
// Options under sub-command: config set
RegisterOptionsForCommandConfigSet(SetCmd, getLogger);
ConfigCmd.Subcommands.Add(SetCmd);
// noun sub-command: config unset
var UnsetCmd = new DocumentedCommand(name: "unset", description: Strings.ConfigUnsetCommandDescription, "https://aka.ms/dotnet/nuget/config/unset");
// Options under sub-command: config unset
RegisterOptionsForCommandConfigUnset(UnsetCmd, getLogger);
ConfigCmd.Subcommands.Add(UnsetCmd);
app.Subcommands.Add(ConfigCmd);
return ConfigCmd;
} // End noun method
private static void RegisterOptionsForCommandConfigPaths(Command cmd, Func<ILogger> getLogger)
{
cmd.Options.Add(WorkingDirectory);
cmd.Options.Add(HelpOption);
// Create handler delegate handler for cmd
cmd.SetAction((parseResult, cancellationToken) =>
{
var args = new ConfigPathsArgs()
{
WorkingDirectory = parseResult.GetValue(WorkingDirectory),
};
int exitCode = ConfigPathsRunner.Run(args, getLogger);
return Task.FromResult(exitCode);
});
}
private static void RegisterOptionsForCommandConfigGet(Command cmd, Func<ILogger> getLogger)
{
cmd.Arguments.Add(AllOrConfigKeyArgument);
cmd.Options.Add(WorkingDirectory);
cmd.Options.Add(ShowPathOption);
cmd.Options.Add(HelpOption);
// Create handler delegate handler for cmd
cmd.SetAction((parseResult, cancellationToken) =>
{
var args = new ConfigGetArgs()
{
AllOrConfigKey = parseResult.GetValue(AllOrConfigKeyArgument),
WorkingDirectory = parseResult.GetValue(WorkingDirectory),
ShowPath = parseResult.GetValue(ShowPathOption),
};
int exitCode = ConfigGetRunner.Run(args, getLogger);
return Task.FromResult(exitCode);
});
}
private static void RegisterOptionsForCommandConfigSet(Command cmd, Func<ILogger> getLogger)
{
cmd.Arguments.Add(SetConfigKeyArgument);
cmd.Arguments.Add(ConfigValueArgument);
cmd.Options.Add(ConfigFileOption);
cmd.Options.Add(HelpOption);
// Create handler delegate handler for cmd
cmd.SetAction((parseResult, cancellationToken) =>
{
var args = new ConfigSetArgs()
{
ConfigKey = parseResult.GetValue(SetConfigKeyArgument),
ConfigValue = parseResult.GetValue(ConfigValueArgument),
ConfigFile = parseResult.GetValue(ConfigFileOption),
};
int exitCode = ConfigSetRunner.Run(args, getLogger);
return Task.FromResult(exitCode);
});
}
private static void RegisterOptionsForCommandConfigUnset(Command cmd, Func<ILogger> getLogger)
{
cmd.Arguments.Add(UnsetConfigKeyArgument);
cmd.Options.Add(ConfigFileOption);
cmd.Options.Add(HelpOption);
// Create handler delegate handler for cmd
cmd.SetAction((parseResult, cancellationToken) =>
{
var args = new ConfigUnsetArgs()
{
ConfigKey = parseResult.GetValue(UnsetConfigKeyArgument),
ConfigFile = parseResult.GetValue(ConfigFileOption),
};
int exitCode = ConfigUnsetRunner.Run(args, getLogger);
return Task.FromResult(exitCode);
});
}
}
}