Example #1
0
            private static async Task <int> DropBefore(string package, string version, string apiKey, string src,
                                                       bool pre)
            {
                if (package == null)
                {
                    throw new ExitCodeException(1, nameof(package));
                }
                if (version == null)
                {
                    throw new ExitCodeException(2, nameof(version));
                }
                if (apiKey == null)
                {
                    throw new ExitCodeException(3, nameof(apiKey));
                }

                try
                {
                    var matches = await PackageHelper.GetPackagesAsync(package);

                    var filtered = PackageHelper.FilterBefore(matches, version, pre).ToArray();
                    Console.WriteLine($"Found {filtered.Length} packages below version {version}.");
                    foreach (var match in filtered)
                    {
                        try
                        {
                            using (var process = new Process())
                            {
                                process.StartInfo.FileName = "nuget.exe";
                                string arguments;
                                string logArguments;
                                if (src == null)
                                {
                                    arguments =
                                        $"delete {package} {match.input} -NonInteractive -Verbosity n -ApiKey {apiKey}";
                                    logArguments =
                                        $"delete {package} {match.input} -NonInteractive -Verbosity n -ApiKey ***";
                                }
                                else
                                {
                                    arguments =
                                        $"delete {package} {match.input} -NonInteractive -Verbosity n -ApiKey {apiKey} -Source {src}";
                                    logArguments =
                                        $"delete {package} {match.input} -NonInteractive -Verbosity n -ApiKey *** -Source {src}";
                                }

                                process.StartInfo.Arguments = arguments;
                                Console.WriteLine($"Executing nuget {logArguments}");
                                process.Start();
                                process.WaitForExit();
                                if (process.ExitCode != 0)
                                {
                                    Console.WriteLine($"nuget process exited with exitCode: {process.ExitCode}.");
                                    return(process.ExitCode);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                            return(5);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return(4);
                }

                return(0);
            }