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; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace repomgr { public static class Program { public static RepoMgr Repo; public static void Main(string[] args) { if (args.Length < 2 || args[1].Equals("help")) PrintHelp(); 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"); } Repo = new RepoMgr(args[0]); if (File.Exists(Path.Combine(args[0], "repomgr.index.json"))) Repo.ReadIndex(); 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); } break; case "add": if (args.Length < 3) PrintHelp(); try { foreach (var package in args[Range.StartAt(2)]) { Repo.Add(package); } } catch (Exception e) { Console.WriteLine("Add failed with error: " + e); } 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); } break; case "update": if (args.Length < 3) PrintHelp(); try { foreach (var package in args[Range.StartAt(2)].Where(p => p != "-f")) { Repo.Build(package, args.Contains("-f"), args.Contains("--skip-checks")); } } catch (Exception e) { Console.WriteLine("Update failed with error: " + e); } break; case "update-all": if (args.Length != 2) PrintHelp(); try { Repo.BuildAll(); } catch (Exception e) { Console.WriteLine("UpdateAll failed with error: " + e); } break; case "remove": if (args.Length < 3) PrintHelp(); try { foreach (var package in args[Range.StartAt(2)]) { Repo.Remove(package); } } catch (Exception e) { Console.WriteLine("Remove failed with error: " + e); } break; case "list": if (args.Length != 2) PrintHelp(); try { Repo.List(); } catch (Exception e) { Console.WriteLine("List failed with error " + e); } break; default: PrintHelp(); break; } if (File.Exists("/tmp/repomgr.lock")) File.Delete("/tmp/repomgr.lock"); } private static void PrintHelp() { Console.WriteLine("Usage:"); Console.WriteLine("repomgr init "); Console.WriteLine("repomgr list"); Console.WriteLine("repomgr add [...]"); Console.WriteLine("repomgr autoadd [...]"); Console.WriteLine("repomgr remove [...]"); Console.WriteLine("repomgr update [...] [-f] [--skip-checks]"); Console.WriteLine("repomgr update-all"); Console.WriteLine("repomgr daemon"); if (File.Exists("/tmp/repomgr.lock")) File.Delete("/tmp/repomgr.lock"); Environment.Exit(0); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); } }