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
JFronny 0eccafa9ea Various CLI improvements
- Show dialog before adding default repo
- Prevent edge-case where --basic is used later in a CLI command
- Fix RemoveRepo in basic mode
- Use default instead of true in basic YesNoDialog
2020-08-31 20:22:37 +02:00

92 lines
3.5 KiB
C#

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")
};
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")
};
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))
return;
break;
}
foreach (XElement t in sRepos)
{
Console.WriteLine($"Removing {t.Element("Name").Value}");
t.Remove();
}
doc.Save(PathTool.InfoXml);
Console.WriteLine("Removed repo. Remember to update the cache using \"uptool update\"");
}
}
}