using System; using System.IO; using Serilog; using SyncEngine.Configuration; using SyncEngine.Sync; namespace SyncEngine { internal static class Program { private static int Main(string[] args) { var envLoad = EnvSettings.LoadFromStandardLocations(); var exeDir = EnvSettings.ExeDirectory(); var logDir = Path.Combine(exeDir, "logs"); Directory.CreateDirectory(logDir); Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() .WriteTo.Console() .WriteTo.File(Path.Combine(logDir, "sync-.log"), rollingInterval: RollingInterval.Day) .CreateLogger(); try { using (var instanceLock = SyncInstanceLock.TryAcquire()) { if (instanceLock == null) { Log.Warning("Another SyncEngine instance is already running; exiting."); return 3; } var mode = args.Length > 0 ? args[0] : "all"; if (!envLoad.Found) { Log.Error("No .env file found beside {ExeDir} or at the repo root.", exeDir); Log.Error("Copy config\\.env.example to .env in the repo root, then build (copies .env to output) or place .env beside SyncEngine.exe."); return 2; } Log.Information("Loaded configuration from: {Paths}", string.Join("; ", envLoad.LoadedPaths)); var options = SyncOptions.LoadFromEnvironment(); SyncOptionsValidator.Validate(options, mode, envLoad.Found); Log.Information("SyncEngine starting mode={Mode} dryRun={DryRun}", mode, options.DryRun); var orchestrator = new SyncOrchestrator(options, Log.Logger); return orchestrator.Run(mode); } } catch (ConfigurationException ex) { Log.Error("Configuration error:\n{Message}", ex.Message); Log.Error("Edit your .env file (see .env.example) and try again."); return 2; } catch (Exception ex) { Log.Fatal("{Message}", SyncErrorFormatter.Describe(ex)); Log.Debug(ex, "Unhandled exception"); return 1; } finally { Log.CloseAndFlush(); } } } }