2020-03-24 20:53:23 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.CommandLine;
|
|
|
|
|
using System.CommandLine.Invocation;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UpToolLib;
|
|
|
|
|
using UpToolLib.DataStructures;
|
|
|
|
|
using UpToolLib.Tool;
|
|
|
|
|
|
|
|
|
|
namespace UpToolCLI
|
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
|
|
|
|
public static int Main(string[] args)
|
|
|
|
|
{
|
2020-03-24 22:43:48 +01:00
|
|
|
|
MutexLock.Lock();
|
|
|
|
|
try
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
2020-03-24 22:43:48 +01:00
|
|
|
|
ExternalFunctionalityManager.Init(new UTLibFunctions());
|
|
|
|
|
RootCommand rootCommand = new RootCommand();
|
|
|
|
|
rootCommand.AddCommand(new Command("update", "Updates the cache")
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create(Update)
|
|
|
|
|
});
|
2020-03-24 20:53:23 +01:00
|
|
|
|
|
2020-03-24 22:43:48 +01:00
|
|
|
|
Command install = new Command("install", "Install a package")
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create<string, bool>(Install)
|
|
|
|
|
};
|
|
|
|
|
install.AddOption(new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"));
|
|
|
|
|
install.AddOption(new Option<bool>(new[] {"--force", "-f"}, "Overwrites older files"));
|
|
|
|
|
rootCommand.AddCommand(install);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
|
2020-03-24 22:43:48 +01:00
|
|
|
|
Command upgrade = new Command("upgrade", "Upgrade a package")
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create<string, bool>(Upgrade)
|
|
|
|
|
};
|
|
|
|
|
upgrade.AddOption(new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"));
|
|
|
|
|
upgrade.AddOption(new Option<bool>(new[] {"--force", "-f"}, "Overwrites older files"));
|
|
|
|
|
rootCommand.AddCommand(upgrade);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
|
2020-03-24 22:43:48 +01:00
|
|
|
|
Command reinstall = new Command("reinstall", "Reinstall a package")
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create<string, bool>(Reinstall)
|
|
|
|
|
};
|
|
|
|
|
reinstall.AddOption(new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"));
|
|
|
|
|
reinstall.AddOption(new Option<bool>(new[] {"--force", "-f"}, "Overwrites older files"));
|
|
|
|
|
rootCommand.AddCommand(reinstall);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
|
2020-03-24 22:43:48 +01:00
|
|
|
|
Command remove = new Command("remove", "Remove a package")
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create<string>(Remove)
|
|
|
|
|
};
|
|
|
|
|
remove.AddOption(new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"));
|
|
|
|
|
rootCommand.AddCommand(remove);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
|
2020-03-24 22:43:48 +01:00
|
|
|
|
Command purge = new Command("purge", "Completely remove a package")
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create<string>(Purge)
|
|
|
|
|
};
|
|
|
|
|
purge.AddOption(new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"));
|
|
|
|
|
rootCommand.AddCommand(purge);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
|
2020-03-24 22:43:48 +01:00
|
|
|
|
rootCommand.AddCommand(new Command("list", "Lists installed packages")
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create(List)
|
|
|
|
|
});
|
2020-03-24 20:53:23 +01:00
|
|
|
|
|
2020-03-24 22:43:48 +01:00
|
|
|
|
rootCommand.AddCommand(new Command("dist-upgrade", "Upgrades all packages")
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create(DistUpgrade)
|
|
|
|
|
});
|
2020-03-24 20:53:23 +01:00
|
|
|
|
|
2020-03-24 22:43:48 +01:00
|
|
|
|
Command search = new Command("search", "Search for packages")
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create<string>(Search)
|
|
|
|
|
};
|
|
|
|
|
search.AddOption(new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"));
|
|
|
|
|
rootCommand.AddCommand(search);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
|
2020-03-24 22:43:48 +01:00
|
|
|
|
Command show = new Command("show", "Shows package info")
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create<string>(Show)
|
|
|
|
|
};
|
|
|
|
|
show.AddOption(new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"));
|
|
|
|
|
rootCommand.AddCommand(show);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
|
2020-03-24 23:14:40 +01:00
|
|
|
|
Command start = new Command("start", "Starts an app")
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create<string>(Show)
|
|
|
|
|
};
|
|
|
|
|
start.AddOption(new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"));
|
|
|
|
|
rootCommand.AddCommand(start);
|
|
|
|
|
|
2020-03-24 22:43:48 +01:00
|
|
|
|
return rootCommand.InvokeAsync(args).Result;
|
|
|
|
|
}
|
|
|
|
|
finally
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
2020-03-24 22:43:48 +01:00
|
|
|
|
MutexLock.Unlock();
|
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Update()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Fetching Repos...");
|
|
|
|
|
RepoManagement.FetchRepos();
|
|
|
|
|
Console.WriteLine("Done!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void List()
|
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
2020-03-24 22:43:48 +01:00
|
|
|
|
foreach (KeyValuePair<Guid, App> app in GlobalVariables.Apps.Where(s =>
|
|
|
|
|
(s.Value.status & Status.Installed) == Status.Installed))
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
2020-03-24 22:43:48 +01:00
|
|
|
|
Console.BackgroundColor = (app.Value.status & Status.Local) == Status.Local ? ConsoleColor.DarkRed :
|
|
|
|
|
(app.Value.status & Status.Updatable) == Status.Updatable ? ConsoleColor.DarkGreen :
|
|
|
|
|
ConsoleColor.Black;
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Console.ForegroundColor = ConsoleColor.White;
|
2020-03-24 22:43:48 +01:00
|
|
|
|
Console.WriteLine($"{app.Value.Name} ({app.Key})");
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DistUpgrade()
|
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
2020-03-24 22:43:48 +01:00
|
|
|
|
foreach (KeyValuePair<Guid, App> app in GlobalVariables.Apps.Where(s =>
|
|
|
|
|
(s.Value.status & Status.Updatable) == Status.Updatable))
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Updating {app.Value.Name}");
|
2020-03-24 21:27:41 +01:00
|
|
|
|
AppExtras.Update(app.Value, false);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
2020-03-24 21:27:41 +01:00
|
|
|
|
Console.WriteLine("Done!");
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Show(string identifier)
|
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
|
|
|
|
App[] apps = AppExtras.FindApps(identifier);
|
|
|
|
|
if (apps.Length == 0)
|
|
|
|
|
Console.WriteLine("Package not found.");
|
|
|
|
|
else
|
|
|
|
|
Console.WriteLine(apps.First());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Search(string identifier)
|
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
|
|
|
|
App[] apps = AppExtras.FindApps(identifier);
|
|
|
|
|
Console.WriteLine($"Found {apps.Length} app(s)");
|
|
|
|
|
for (int i = 0; i < apps.Length; i++)
|
2020-03-24 22:43:48 +01:00
|
|
|
|
Console.WriteLine($"{apps[i].Name} ({apps[i].Id})");
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:27:41 +01:00
|
|
|
|
private static void Upgrade(string identifier, bool force)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
|
|
|
|
App[] apps = AppExtras.FindApps(identifier);
|
|
|
|
|
if (apps.Length == 0)
|
2020-03-24 22:43:48 +01:00
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Console.WriteLine("Package not found.");
|
2020-03-24 22:43:48 +01:00
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App tmp = apps.First();
|
|
|
|
|
if ((tmp.status & Status.Updatable) == Status.Updatable)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Upgrading {tmp.Name}");
|
2020-03-24 21:27:41 +01:00
|
|
|
|
AppExtras.Update(tmp, force);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
2020-03-24 22:43:48 +01:00
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Console.WriteLine("Package is up-to-date");
|
2020-03-24 22:43:48 +01:00
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
2020-03-24 21:27:41 +01:00
|
|
|
|
Console.WriteLine("Done!");
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:27:41 +01:00
|
|
|
|
private static void Reinstall(string identifier, bool force)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
|
|
|
|
App[] apps = AppExtras.FindApps(identifier);
|
|
|
|
|
if (apps.Length == 0)
|
2020-03-24 22:43:48 +01:00
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Console.WriteLine("Package not found.");
|
2020-03-24 22:43:48 +01:00
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App tmp = apps.First();
|
|
|
|
|
Console.WriteLine($"Reinstalling {tmp.Name}");
|
2020-03-24 21:27:41 +01:00
|
|
|
|
AppExtras.Update(tmp, force);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
2020-03-24 21:27:41 +01:00
|
|
|
|
Console.WriteLine("Done!");
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Remove(string identifier)
|
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
|
|
|
|
App[] apps = AppExtras.FindApps(identifier);
|
|
|
|
|
if (apps.Length == 0)
|
2020-03-24 22:43:48 +01:00
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Console.WriteLine("Package not found.");
|
2020-03-24 22:43:48 +01:00
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App tmp = apps.First();
|
|
|
|
|
if ((tmp.status & Status.Installed) == Status.Installed)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Removing {tmp.Name}");
|
|
|
|
|
AppExtras.Remove(tmp, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-03-24 22:43:48 +01:00
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Console.WriteLine("Package is not installed");
|
2020-03-24 22:43:48 +01:00
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
2020-03-24 21:27:41 +01:00
|
|
|
|
Console.WriteLine("Done!");
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Purge(string identifier)
|
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
|
|
|
|
App[] apps = AppExtras.FindApps(identifier);
|
|
|
|
|
if (apps.Length == 0)
|
2020-03-24 22:43:48 +01:00
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Console.WriteLine("Package not found.");
|
2020-03-24 22:43:48 +01:00
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App tmp = apps.First();
|
|
|
|
|
if ((tmp.status & Status.Installed) == Status.Installed)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Purgeing {tmp.Name}");
|
|
|
|
|
AppExtras.Remove(tmp, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-03-24 22:43:48 +01:00
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Console.WriteLine("Package is not installed");
|
2020-03-24 22:43:48 +01:00
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
2020-03-24 21:27:41 +01:00
|
|
|
|
Console.WriteLine("Done!");
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:27:41 +01:00
|
|
|
|
private static void Install(string identifier, bool force)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
|
|
|
|
App[] apps = AppExtras.FindApps(identifier);
|
|
|
|
|
if (apps.Length == 0)
|
2020-03-24 22:43:48 +01:00
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Console.WriteLine("Package not found.");
|
2020-03-24 22:43:48 +01:00
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App tmp = apps.First();
|
|
|
|
|
if ((tmp.status & Status.Installed) == Status.Installed)
|
2020-03-24 22:43:48 +01:00
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Console.WriteLine("Package is already installed");
|
2020-03-24 22:43:48 +01:00
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Installing {tmp.Name}");
|
2020-03-24 21:27:41 +01:00
|
|
|
|
AppInstall.Install(tmp, true);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-24 21:27:41 +01:00
|
|
|
|
Console.WriteLine("Done!");
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
2020-03-24 23:14:40 +01:00
|
|
|
|
|
|
|
|
|
private static void Start(string identifier, bool waitForExit)
|
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
|
|
|
|
App[] apps = AppExtras.FindApps(identifier);
|
|
|
|
|
if (apps.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Package not found.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App tmp = apps.First();
|
|
|
|
|
Console.WriteLine($"Starting {tmp.Name}");
|
|
|
|
|
System.Diagnostics.Process tmp1 = AppExtras.RunApp(tmp);
|
|
|
|
|
if (waitForExit)
|
|
|
|
|
tmp1.WaitForExit();
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("Done!");
|
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|