-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
159 lines (145 loc) · 6.35 KB
/
Program.cs
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
using System;
using System.Reflection;
using System.Diagnostics;
using System.IO;
using CommandLine;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Frameworks;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
namespace NugetBulkDoer
{
public class Program
{
public static async Task Main(string[] args)
{
CommandLine.Parser.Default.ParseArguments<Options>(args)
.WithParsed(async (options) => await Execute(options));
}
public static async Task Execute(Options options)
{
Console.Write($"Provide the API key associated with this package. \n" +
"Make sure you have created the key with unlisting privileges. \n >");
string ApiKey = Console.ReadLine();
Console.Write($"Provide the ID of the package you wish to unlist from. \n >");
string PackageID = Console.ReadLine();
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
FindPackageByIdResource resource = await repository.GetResourceAsync<FindPackageByIdResource>();
IEnumerable<NuGetVersion> versions = await resource.GetAllVersionsAsync(
PackageID,
cache,
logger,
cancellationToken);
Console.Write($"Which versions would you like to unlist? (Options: some/all/previews) >");
string mode = Console.ReadLine();
if (mode.Equals("all"))
{
UnlistSome(PackageID, ApiKey, versions, ".");
}
else if (mode.Equals("previews"))
{
UnlistSome(PackageID, ApiKey, versions, "-");
} else if (mode.Equals("some"))
{
Console.Write($"Please enter the character or substring to search for. \n >");
string InputText = Console.ReadLine();
UnlistSome(PackageID, ApiKey, versions, InputText);
} else {
Console.Write("Input not valid.");
}
}
/// <summary>
/// Prints to console all package versions to be unlisted
/// Returns a boolean based on whether user confirms or not
/// </summary>
/// <param name="SelectedPackages">Package versions to be unlisted</param>
/// <returns></returns>
public static Boolean Confirm(List<NuGetVersion> toUnlist)
{
if (!toUnlist.Any())
{
Console.WriteLine("We did not find any versions of the package that matched that description.");
return false;
}
foreach (NuGetVersion version in toUnlist)
{
Console.WriteLine($"{version}");
}
Console.Write($"Please confirm to unlist the above versions (y/n)");
string userConfirmation = Console.ReadLine();
return userConfirmation.Equals("y");
}
/// <summary>
/// Finds all package versions that are currently not unlisted
/// Confirms the selection with the user
/// Unlists all package versions
/// Output confirmation
/// </summary>
/// <param name="PackageID">Package to be modified</param>
/// <param name="ApiKey">Credentials for package version modification</param>
public static void Unlist(string PackageID, string ApiKey, IEnumerable<NuGetVersion> versions)
{
/* */ foreach (NuGetVersion version in versions)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.FileName = "CMD.exe";
process.StartInfo = startInfo;
//process.ErrorDataReceived += cmd_Error;
process.OutputDataReceived += cmd_DataReceived;
process.EnableRaisingEvents = true;
//startInfo.Arguments = $"/C dotnet nuget delete {PackageID} {version} -k {ApiKey} -s https://www.nuget.org";
process.Start();
process.BeginOutputReadLine();
//process.BeginErrorReadLine();
process.StandardInput.WriteLine($"dotnet nuget delete {PackageID} {version} -k {ApiKey} -s https://www.nuget.org && exit");
process.StandardInput.WriteLine("y");
Console.WriteLine($"Version {version} has been unlisted.");
process.WaitForExit();
}
static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("Output from other process");
Console.WriteLine(e.Data);
}
}
/// <summary>
/// Finds all package versions that are currently not unlisted and contains the character '-'
/// Denotes a pre-release version
/// Confirms the selection with the user
/// Unlists all pre-release package versions
/// Output confirmation
/// </summary>
/// <param name="PackageID">Package to be modified</param>
/// <param name="ApiKey">Credentials for package version modification</param>
public static void UnlistSome(string PackageID, string ApiKey, IEnumerable<NuGetVersion> versions, string Keyword)
{
List<NuGetVersion> previews = new List<NuGetVersion>();
foreach (NuGetVersion version in versions)
{
if (version.ToString().Contains(Keyword))
{
previews.Add(version);
}
}
if (Confirm(previews))
{
Unlist(PackageID, ApiKey, previews);
}
}
}
}