2020-03-24 20:53:23 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.CommandLine;
|
|
|
|
|
using System.CommandLine.Invocation;
|
2020-04-08 16:05:09 +02:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
2020-03-24 20:53:23 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using UpToolLib;
|
|
|
|
|
using UpToolLib.DataStructures;
|
|
|
|
|
using UpToolLib.Tool;
|
|
|
|
|
|
|
|
|
|
namespace UpToolCLI
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
public static class Program
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
|
|
|
|
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-04-08 16:05:09 +02:00
|
|
|
|
XmlTool.FixXml();
|
|
|
|
|
ExternalFunctionalityManager.Init(new UtLibFunctions());
|
2020-03-24 22:43:48 +01:00
|
|
|
|
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")
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app or the file name"),
|
|
|
|
|
new Option<bool>(new[] {"--force", "-f"}, "Overwrites older files")
|
2020-03-24 22:43:48 +01:00
|
|
|
|
};
|
2020-04-08 16:05:09 +02:00
|
|
|
|
install.Handler = CommandHandler.Create<string, bool>(Install);
|
2020-03-24 22:43:48 +01:00
|
|
|
|
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")
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"),
|
|
|
|
|
new Option<bool>(new[] {"--force", "-f"}, "Overwrites older files")
|
2020-03-24 22:43:48 +01:00
|
|
|
|
};
|
2020-04-08 16:05:09 +02:00
|
|
|
|
upgrade.Handler = CommandHandler.Create<string, bool>(Upgrade);
|
2020-03-24 22:43:48 +01:00
|
|
|
|
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")
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"),
|
|
|
|
|
new Option<bool>(new[] {"--force", "-f"}, "Overwrites older files")
|
2020-03-24 22:43:48 +01:00
|
|
|
|
};
|
2020-04-08 16:05:09 +02:00
|
|
|
|
reinstall.Handler = CommandHandler.Create<string, bool>(Reinstall);
|
2020-03-24 22:43:48 +01:00
|
|
|
|
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")
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app")
|
2020-03-24 22:43:48 +01:00
|
|
|
|
};
|
2020-04-08 16:05:09 +02:00
|
|
|
|
remove.Handler = CommandHandler.Create<string>(Remove);
|
2020-03-24 22:43:48 +01:00
|
|
|
|
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")
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app")
|
2020-03-24 22:43:48 +01:00
|
|
|
|
};
|
2020-04-08 16:05:09 +02:00
|
|
|
|
purge.Handler = CommandHandler.Create<string>(Purge);
|
2020-03-24 22:43:48 +01:00
|
|
|
|
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")
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app")
|
2020-03-24 22:43:48 +01:00
|
|
|
|
};
|
2020-04-08 16:05:09 +02:00
|
|
|
|
search.Handler = CommandHandler.Create<string>(Search);
|
2020-03-24 22:43:48 +01:00
|
|
|
|
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")
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app")
|
2020-03-24 22:43:48 +01:00
|
|
|
|
};
|
2020-04-08 16:05:09 +02:00
|
|
|
|
show.Handler = CommandHandler.Create<string>(Show);
|
2020-03-24 22:43:48 +01:00
|
|
|
|
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")
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"),
|
|
|
|
|
new Option<string>(new[] {"--waitForExit", "-wait"}, "Waits until the program quits")
|
2020-03-24 23:14:40 +01:00
|
|
|
|
};
|
2020-04-08 16:05:09 +02:00
|
|
|
|
start.Handler = CommandHandler.Create<string, bool>(Start);
|
2020-03-24 23:14:40 +01:00
|
|
|
|
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();
|
2020-04-07 21:26:16 +02:00
|
|
|
|
RepoManagement.GetReposFromDisk();
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
IEnumerable<App> tmp = GlobalVariables.Apps.Where(s =>
|
2020-04-08 16:05:09 +02:00
|
|
|
|
(s.Value.Status & Status.Updatable) == Status.Updatable).Select(s => s.Value);
|
2020-04-07 21:26:16 +02:00
|
|
|
|
IEnumerable<App> apps = tmp as App[] ?? tmp.ToArray();
|
|
|
|
|
int updatableCount = apps.Count();
|
|
|
|
|
Console.WriteLine(updatableCount == 0
|
|
|
|
|
? "All up-to-date"
|
|
|
|
|
: $@"Found {updatableCount} Updates:
|
|
|
|
|
{string.Join(Environment.NewLine, apps.Select(s => $"- {s.Name} ({s.Version})"))}");
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void List()
|
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
2020-03-24 22:43:48 +01:00
|
|
|
|
foreach (KeyValuePair<Guid, App> app in GlobalVariables.Apps.Where(s =>
|
2020-04-08 16:05:09 +02:00
|
|
|
|
(s.Value.Status & Status.Installed) == Status.Installed))
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
Console.BackgroundColor = (app.Value.Status & Status.Local) == Status.Local ? ConsoleColor.DarkRed :
|
|
|
|
|
(app.Value.Status & Status.Updatable) == Status.Updatable ? ConsoleColor.DarkGreen :
|
2020-03-24 22:43:48 +01:00
|
|
|
|
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 =>
|
2020-04-08 16:05:09 +02:00
|
|
|
|
(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)
|
|
|
|
|
Console.WriteLine("Package not found.");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App tmp = apps.First();
|
2020-04-08 16:05:09 +02:00
|
|
|
|
if ((tmp.Status & Status.Updatable) == Status.Updatable)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
Console.WriteLine("Package is up-to-date");
|
|
|
|
|
}
|
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)
|
|
|
|
|
Console.WriteLine("Package not found.");
|
|
|
|
|
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)
|
|
|
|
|
Console.WriteLine("Package not found.");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App tmp = apps.First();
|
2020-04-08 16:05:09 +02:00
|
|
|
|
if ((tmp.Status & Status.Installed) == Status.Installed)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Removing {tmp.Name}");
|
|
|
|
|
AppExtras.Remove(tmp, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Console.WriteLine("Package is not installed");
|
|
|
|
|
}
|
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)
|
|
|
|
|
Console.WriteLine("Package not found.");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App tmp = apps.First();
|
2020-04-08 16:05:09 +02:00
|
|
|
|
if ((tmp.Status & Status.Installed) == Status.Installed)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Purgeing {tmp.Name}");
|
|
|
|
|
AppExtras.Remove(tmp, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Console.WriteLine("Package is not installed");
|
|
|
|
|
}
|
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-04-08 16:05:09 +02:00
|
|
|
|
if (File.Exists(identifier))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Name:");
|
|
|
|
|
string name = Console.ReadLine();
|
|
|
|
|
AppInstall.InstallZip(identifier, new App(name, "Locally installed package, removal only", GlobalVariables.MinimumVer, "", true, "",
|
|
|
|
|
Guid.NewGuid(), Color.Red, "", false, ""), force);
|
|
|
|
|
Console.WriteLine($"Successfully installed \"{name}\"");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Package not found.");
|
|
|
|
|
Console.WriteLine(identifier);
|
|
|
|
|
}
|
2020-03-24 22:43:48 +01:00
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App tmp = apps.First();
|
2020-04-08 16:05:09 +02:00
|
|
|
|
if ((tmp.Status & Status.Installed) == Status.Installed)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Console.WriteLine("Package is already installed");
|
|
|
|
|
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();
|
2020-04-08 16:05:09 +02:00
|
|
|
|
if (tmp.Runnable)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Starting {tmp.Name}");
|
|
|
|
|
System.Diagnostics.Process tmp1 = AppExtras.RunApp(tmp);
|
|
|
|
|
if (waitForExit)
|
|
|
|
|
tmp1.WaitForExit();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Console.WriteLine($"{tmp.Name} is not runnable");
|
2020-03-24 23:14:40 +01:00
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("Done!");
|
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|