repomgr/Program.cs

151 lines
3.8 KiB
C#
Raw Permalink Normal View History

2019-07-05 00:36:10 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
2020-06-25 22:25:11 +02:00
using Microsoft.Extensions.Hosting;
2019-07-05 00:36:10 +02:00
using Microsoft.Extensions.Logging;
2020-06-25 22:25:11 +02:00
namespace repomgr {
public static class Program {
public static RepoMgr Repo;
2019-10-10 13:14:04 +02:00
2020-06-25 22:25:11 +02:00
public static void Main(string[] args) {
2020-07-22 12:38:14 +02:00
if (args.Length < 2 || args[1].Equals("help"))
PrintHelp();
2020-06-25 22:25:11 +02:00
if (File.Exists("/tmp/repomgr.lock") && args[1] != "daemon") {
Console.WriteLine("/tmp/repomgr.lock exists, delete it if you are sure no other process is running");
Environment.Exit(1);
}
else if (args[1] != "daemon") {
File.Create("/tmp/repomgr.lock");
}
2019-10-10 13:14:04 +02:00
2020-06-25 22:25:11 +02:00
Repo = new RepoMgr(args[0]);
if (File.Exists(Path.Combine(args[0], "repomgr.index.json")))
Repo.ReadIndex();
2020-07-22 12:38:14 +02:00
2020-06-25 22:25:11 +02:00
switch (args[1]) {
case "daemon":
CreateHostBuilder(args).Build().Run();
break;
case "init":
if (args.Length != 4)
PrintHelp();
try {
Repo.Init(args[2], args[3]);
}
catch (Exception e) {
Console.WriteLine("Init failed with error: " + e);
}
2019-10-10 13:14:04 +02:00
2020-06-25 22:25:11 +02:00
break;
case "add":
2020-07-22 12:19:10 +02:00
if (args.Length < 3)
2020-06-25 22:25:11 +02:00
PrintHelp();
try {
2020-07-22 12:19:10 +02:00
foreach (var package in args[Range.StartAt(2)]) {
Repo.Add(package);
}
2020-06-25 22:25:11 +02:00
}
catch (Exception e) {
Console.WriteLine("Add failed with error: " + e);
}
2019-10-10 13:14:04 +02:00
2020-07-22 12:19:10 +02:00
break;
case "autoadd":
if (args.Length < 3)
PrintHelp();
try {
foreach (var package in args[Range.StartAt(2)]) {
Repo.Add(package);
}
foreach (var package in args[Range.StartAt(2)]) {
Repo.Build(package);
}
}
catch (Exception e) {
Console.WriteLine("AutoAdd failed with error: " + e);
}
2020-06-25 22:25:11 +02:00
break;
case "update":
2020-07-22 12:19:10 +02:00
if (args.Length < 3)
2020-06-25 22:25:11 +02:00
PrintHelp();
try {
2020-07-22 12:19:10 +02:00
foreach (var package in args[Range.StartAt(2)].Where(p => p != "-f")) {
Repo.Build(package, args.Contains("-f"), args.Contains("--skip-checks"));
2020-06-25 22:25:11 +02:00
}
}
catch (Exception e) {
2020-07-22 12:19:10 +02:00
Console.WriteLine("Update failed with error: " + e);
2020-06-25 22:25:11 +02:00
}
2019-10-10 13:14:04 +02:00
2020-06-25 22:25:11 +02:00
break;
case "update-all":
if (args.Length != 2)
PrintHelp();
try {
Repo.BuildAll();
}
catch (Exception e) {
2020-07-22 12:19:10 +02:00
Console.WriteLine("UpdateAll failed with error: " + e);
2020-06-25 22:25:11 +02:00
}
2019-10-10 13:14:04 +02:00
2020-06-25 22:25:11 +02:00
break;
case "remove":
2020-07-22 12:19:10 +02:00
if (args.Length < 3)
2020-06-25 22:25:11 +02:00
PrintHelp();
try {
2020-07-22 12:19:10 +02:00
foreach (var package in args[Range.StartAt(2)]) {
Repo.Remove(package);
}
2020-06-25 22:25:11 +02:00
}
catch (Exception e) {
Console.WriteLine("Remove failed with error: " + e);
}
2019-10-10 13:14:04 +02:00
2020-06-25 22:25:11 +02:00
break;
case "list":
if (args.Length != 2)
PrintHelp();
try {
Repo.List();
}
catch (Exception e) {
Console.WriteLine("List failed with error " + e);
}
2019-07-05 00:36:10 +02:00
2020-06-25 22:25:11 +02:00
break;
default:
PrintHelp();
break;
}
2019-07-05 00:36:10 +02:00
2020-06-25 22:25:11 +02:00
if (File.Exists("/tmp/repomgr.lock"))
File.Delete("/tmp/repomgr.lock");
}
private static void PrintHelp() {
Console.WriteLine("Usage:");
Console.WriteLine("repomgr <data-path> init <repo-path> <reponame>");
Console.WriteLine("repomgr <data-path> list");
2020-07-22 12:19:10 +02:00
Console.WriteLine("repomgr <data-path> add <package> [...]");
Console.WriteLine("repomgr <data-path> autoadd <package> [...]");
Console.WriteLine("repomgr <data-path> remove <package> [...]");
Console.WriteLine("repomgr <data-path> update <package> [...] [-f] [--skip-checks]");
2020-06-25 22:25:11 +02:00
Console.WriteLine("repomgr <data-path> update-all");
Console.WriteLine("repomgr <data-path> daemon");
2020-07-22 12:38:14 +02:00
if (File.Exists("/tmp/repomgr.lock"))
File.Delete("/tmp/repomgr.lock");
Environment.Exit(0);
2020-06-25 22:25:11 +02:00
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
2019-07-05 00:36:10 +02:00
}