Exemplo n.º 1
0
        public static SyncOptions CreateFromRealsyncFile(string filename, ILogger logger)
        {
            if (!File.Exists(filename))
            {
                throw new SyncException($"File is not exists {filename}");
            }

            var syncOptions = new SyncOptions();

            // local = D:/Work/Project
            var optionRegex = new Regex("^(\\S+)\\s*=\\s*(.*)$", RegexOptions.Compiled);

            var lines = File.ReadAllLines(filename);

            foreach (var line in lines)
            {
                var trimmedLine = line.TrimStart();
                // skip comment

                if (trimmedLine.StartsWith("#") || string.IsNullOrWhiteSpace(trimmedLine))
                {
                    continue;
                }

                var match = optionRegex.Match(trimmedLine);
                if (!match.Success)
                {
                    throw new SyncException($"Invalid line: {line}");
                }

                var key   = match.Groups[1].Value.ToLower();
                var value = match.Groups[2].Value;

                switch (key)
                {
                case "local":
                    // local path may be relative to directory of filename
                    syncOptions.SourcePath = Path.GetFullPath(value, Path.GetFullPath(Path.GetDirectoryName(filename) ?? Environment.CurrentDirectory));
                    break;

                case "remote":
                    syncOptions.DestinationPath = value;
                    break;

                case "user":
                    syncOptions.UserName = value;
                    break;

                case "host":
                    if (Uri.TryCreate($"tcp://{value}", UriKind.Absolute, out var uri))
                    {
                        syncOptions.Host = uri.Host;
                        syncOptions.Port = uri.Port < 0 ? DefaultPort : uri.Port;
                    }
                    else
                    {
                        syncOptions.Host = value;
                    }
                    break;

                case "exclude":
                    syncOptions.ExcludeList.Add(value);
                    break;

                case "nosound":
                    break;

                default:
                    logger.Log($"Unknown realsync option: {key}");
                    break;
                }
            }

            if (!syncOptions.IsFilled)
            {
                throw new SyncException("Not enough options from realsync file");
            }

            return(syncOptions);
        }
Exemplo n.º 2
0
        public static SyncOptions CreateFromRealsyncFile(string filename)
        {
            if (!File.Exists(filename))
            {
                throw new SyncException($"File is not exists {filename}");
            }

            var syncOptions = new SyncOptions();

            // local = D:/Work/SEDv1-MSK-3
            var optionRegex = new Regex("^(\\S+)\\s*=\\s*(.*)$", RegexOptions.Compiled);

            var lines = File.ReadAllLines(filename);

            foreach (var line in lines)
            {
                var trimmedLine = line.TrimStart();
                // skip comment

                if (trimmedLine.StartsWith("#") || string.IsNullOrWhiteSpace(trimmedLine))
                {
                    continue;
                }

                var match = optionRegex.Match(trimmedLine);
                if (!match.Success)
                {
                    throw new SyncException($"Invalid line: {line}");
                }

                var key   = match.Groups[1].Value.ToLower();
                var value = match.Groups[2].Value;

                switch (key)
                {
                case "local":
                    syncOptions.SourcePath = value;
                    break;

                case "remote":
                    syncOptions.DestinationPath = value;
                    break;

                case "user":
                    syncOptions.UserName = value;
                    break;

                case "host":
                    syncOptions.Host = value;
                    break;

                case "exclude":
                    syncOptions.ExcludeList.Add(value);
                    break;

                case "nosound":
                    break;

                default:
                    throw new SyncException($"Unknown realsync option: {key}");
                }
            }

            if (!syncOptions.IsFilled)
            {
                throw new SyncException("Not enough options from realsync file");
            }

            return(syncOptions);
        }