-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathDeleteCommand.cs
More file actions
122 lines (104 loc) · 4.78 KB
/
DeleteCommand.cs
File metadata and controls
122 lines (104 loc) · 4.78 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
// 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 disable
using System;
using System.CommandLine;
using System.Globalization;
using NuGet.Commands;
using NuGet.Configuration;
using NuGet.Credentials;
namespace NuGet.CommandLine.XPlat
{
internal static class DeleteCommand
{
private static readonly Option<string> SourceOption = new Option<string>("--source", "-s")
{
Arity = ArgumentArity.ZeroOrOne,
Description = Strings.Source_Description,
};
private static readonly Option<bool> NonInteractiveOption = new Option<bool>("--non-interactive")
{
Arity = ArgumentArity.Zero,
Description = Strings.NonInteractive_Description,
};
private static readonly Option<string> ApiKeyOption = new Option<string>("--api-key", "-k")
{
Arity = ArgumentArity.ZeroOrOne,
Description = Strings.ApiKey_Description,
};
private static readonly Option<bool> NoServiceEndpointOption = new Option<bool>("--no-service-endpoint")
{
Arity = ArgumentArity.Zero,
Description = Strings.NoServiceEndpoint_Description,
};
private static readonly Option<bool> InteractiveOption = new Option<bool>("--interactive")
{
Arity = ArgumentArity.Zero,
Description = Strings.NuGetXplatCommand_Interactive,
};
private static readonly Argument<string> PackageIdArgument = new Argument<string>("PackageId")
{
Arity = ArgumentArity.ExactlyOne,
Description = Strings.Delete_PackageIdAndVersion_Description,
};
private static readonly Argument<string> PackageVersionArgument = new Argument<string>("PackageVersion")
{
Arity = ArgumentArity.ExactlyOne,
Description = Strings.Delete_PackageIdAndVersion_Description,
};
internal static void Register(Command parent, Func<ILoggerWithColor> getLogger)
{
var deleteCmd = new Command("delete", Strings.Delete_Description);
deleteCmd.Options.Add(SourceOption);
deleteCmd.Options.Add(NonInteractiveOption);
deleteCmd.Options.Add(ApiKeyOption);
deleteCmd.Options.Add(NoServiceEndpointOption);
deleteCmd.Options.Add(InteractiveOption);
deleteCmd.Arguments.Add(PackageIdArgument);
deleteCmd.Arguments.Add(PackageVersionArgument);
deleteCmd.SetAction(async (parseResult, cancellationToken) =>
{
string packageId = parseResult.GetValue(PackageIdArgument);
string packageVersion = parseResult.GetValue(PackageVersionArgument);
string sourcePath = parseResult.GetValue(SourceOption);
string apiKeyValue = parseResult.GetValue(ApiKeyOption);
bool nonInteractiveValue = parseResult.GetValue(NonInteractiveOption);
bool noServiceEndpoint = parseResult.GetValue(NoServiceEndpointOption);
bool interactiveValue = parseResult.GetValue(InteractiveOption);
DefaultCredentialServiceUtility.SetupDefaultCredentialService(getLogger(), !interactiveValue);
#pragma warning disable CS0618 // Type or member is obsolete
PackageSourceProvider sourceProvider = new PackageSourceProvider(XPlatUtility.GetSettingsForCurrentWorkingDirectory(), enablePackageSourcesChangedEvent: false);
#pragma warning restore CS0618 // Type or member is obsolete
await DeleteRunner.Run(
sourceProvider.Settings,
sourceProvider,
packageId,
packageVersion,
sourcePath,
apiKeyValue,
nonInteractiveValue,
noServiceEndpoint,
Confirm,
getLogger());
return 0;
});
parent.Subcommands.Add(deleteCmd);
}
private static bool Confirm(string description)
{
ConsoleColor currentColor = ConsoleColor.Gray;
try
{
currentColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.ConsoleConfirmMessage, description));
var result = Console.ReadLine();
return result != null && result.StartsWith(Strings.ConsoleConfirmMessageAccept, StringComparison.OrdinalIgnoreCase);
}
finally
{
Console.ForegroundColor = currentColor;
}
}
}
}