Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                ReplicatorContext context = null;

                try
                {
                    context = ToReplicatorContext(args);
                }
                catch
                {
                    Console.WriteLine("You must at least specify -LocalPath and -RemotePath arguments.");
                    Console.WriteLine("For more information please visit https://sbs20.github.io/syncotron/");
                    return;
                }

                GlobalContext.Properties["LogFilename"] = context.LogFileInfo.FullName;
                log4net.Config.XmlConfigurator.Configure();

                using (Replicator replicator = new Replicator(context))
                {
                    log.Info("=======================================================");
                    log.InfoFormat("Starting syncotron ({0})", replicator.Version);
                    log.InfoFormat("Current db: {0}", context.LocalStorageFileInfo.FullName);
                    log.InfoFormat("CommandType: {0}", context.CommandType.ToString());
                    log.InfoFormat("Direction: {0}", context.SyncDirection.ToString());
                    log.InfoFormat("ScanMode: {0}", context.ScanMode.ToString());

                    replicator.ActionStart += (s, a) =>
                    {
                        log.InfoFormat("{0} [{1}]", a.ToString(), FileSizeFormatter.Format(a.Size));
                    };

                    replicator.ActionComplete += (s, a) =>
                    {
                    };

                    replicator.AnalysisComplete += (s, e) =>
                    {
                        log.InfoFormat("Distinct files: {0}", replicator.DistinctFileCount);
                        log.InfoFormat("Local files: {0}", replicator.LocalFileCount);
                        log.InfoFormat("Remote files: {0}", replicator.RemoteFileCount);
                        log.InfoFormat("Actions found: {0}", replicator.ActionCount);
                    };

                    replicator.Finish += (s, e) =>
                    {
                        log.InfoFormat("Actions completed: {0} / {1}", replicator.ActionsCompleteCount, replicator.ActionCount);
                        log.InfoFormat("Download: {0:0.00}mb (rate: {1:0.00}mb/s)", replicator.DownloadedMeg, replicator.DownloadRate);
                        log.InfoFormat("Upload: {0:0.00}mb (rate {1:0.00}mb/s)", replicator.UploadedMeg, replicator.UploadRate);
                        log.InfoFormat("Duration: {0}", replicator.Duration);
                    };

                    replicator.Exception += (s, e) =>
                    {
                        if (e is TimeoutException)
                        {
                            log.Warn(e.Message);
                        }
                        else if (e is SyncotronException)
                        {
                            log.Error(e.Message);
                        }
                        else
                        {
                            log.Error(e);
                        }
                    };

                    if (!context.CloudService.IsAuthorised)
                    {
                        Uri url = context.CloudService.StartAuthorisation();
                        Console.WriteLine("You have not yet authorised syncotron with your cloud service");
                        Console.WriteLine("Please navigate here and log in");
                        Console.WriteLine();
                        Console.WriteLine(url);
                        Console.WriteLine();
                        Console.WriteLine("Then paste the result back in here and press <enter>");
                        string response = Console.ReadLine();
                        try
                        {
                            context.CloudService.FinishAuthorisation(response);
                        }
                        catch (Exception ex)
                        {
                            log.Error(ex);
                            return;
                        }
                    }

                    log.InfoFormat("Current user: {0}", replicator.Context.CloudService.CurrentAccountEmail);
                    log.InfoFormat("Local path: {0}", replicator.Context.LocalPath);
                    log.InfoFormat("Remote path: {0}", replicator.Context.RemotePath);

                    Task replicatorStart = replicator.StartAsync();
                    replicatorStart.Wait();
                }

                if (context.IsDebug)
                {
                    Console.WriteLine("Finished. Press <enter> to finish");
                    Console.ReadLine();
                }
            }
            catch (AnotherInstanceIsRunningException)
            {
                log.Info("Syncotron is already running");
            }
            catch (Exception ex)
            {
                // This is really the error handling of last resort
                log.Error(ex);
            }
        }