This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
UpTool2/UpToolCLI/ReposManagement.cs

92 lines
3.5 KiB
C#
Raw Normal View History

2020-05-16 17:59:44 +02:00
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Linq;
using System.Xml.Linq;
using UpToolLib.Tool;
namespace UpToolCLI
{
public class ReposManagement
{
public static void RegisterCommands(RootCommand rootCommand)
{
rootCommand.AddCommand(new Command("list-repo", "Lists current repositories")
{
Handler = CommandHandler.Create(ListRepo)
});
Command addRepo = new Command("add-repo", "Adds a repository")
{
new Argument<string>("name", "The new repositories name"),
new Argument<string>("link", "A link to the repositories XML")
2020-05-16 17:59:44 +02:00
};
addRepo.Handler = CommandHandler.Create<string, string>(AddRepo);
rootCommand.AddCommand(addRepo);
Command removeRepo = new Command("remove-repo", "Removes a repository")
{
new Argument<string>("name", "The repositories name")
2020-05-16 17:59:44 +02:00
};
removeRepo.Handler = CommandHandler.Create<string>(RemoveRepo);
rootCommand.AddCommand(removeRepo);
}
private static void ListRepo()
{
XDocument doc = XDocument.Load(PathTool.InfoXml);
XElement repos = doc.Element("meta").Element("Repos");
Console.WriteLine("Current repos:");
Console.WriteLine(repos.Elements("Repo").ToStringTable(new[]
{
"Name", "Link"
},
u => u.Element("Name").Value,
u => u.Element("Link").Value));
}
private static void AddRepo(string name, string link)
{
XDocument doc = XDocument.Load(PathTool.InfoXml);
XElement repos = doc.Element("meta").Element("Repos");
repos.Add(new XElement("Repo", new XElement("Name", name),
new XElement("Link", link)));
doc.Save(PathTool.InfoXml);
Console.WriteLine("Added repo. Remember to update the cache using \"uptool update\"");
}
private static void RemoveRepo(string name)
{
XDocument doc = XDocument.Load(PathTool.InfoXml);
XElement repos = doc.Element("meta").Element("Repos");
XElement[] sRepos = repos.Elements("Repo")
.Where(s => s.Element("Name").Value.ToLower().StartsWith(name.ToLower())).ToArray();
switch (sRepos.Length)
{
case 0:
Console.WriteLine("No repo was found that matches your input!");
return;
case 1:
break;
default:
Console.WriteLine("Found multiple repos that match your input:");
Console.WriteLine(sRepos.ToStringTable(new[]
{
"Name", "Link"
},
u => u.Element("Name").Value,
u => u.Element("Link").Value));
if (!Program.Functions.YesNoDialog("Are you sure you want to delete them all?", false))
2020-05-16 17:59:44 +02:00
return;
break;
}
foreach (XElement t in sRepos)
2020-05-16 17:59:44 +02:00
{
Console.WriteLine($"Removing {t.Element("Name").Value}");
t.Remove();
2020-05-16 17:59:44 +02:00
}
doc.Save(PathTool.InfoXml);
Console.WriteLine("Removed repo. Remember to update the cache using \"uptool update\"");
}
}
}