Exemplo n.º 1
0
        static public void KeyValue(PropertyFile.Type type, String sourceFile, String destinationFile, String transformFile, List <KeyValuePair <String, String> > settings, bool createIfNotFound = false, bool overwrite = true, Clients clients = null)
        {
            bool       tempFileUsed = false;
            ZephyrFile source       = Utilities.GetZephyrFile(sourceFile, clients);
            ZephyrFile destination  = Utilities.GetZephyrFile(destinationFile, clients);
            ZephyrFile transform    = Utilities.GetZephyrFile(transformFile, clients);

            if (!overwrite)
            {
                if (destination == null)
                {
                    throw new Exception($"Destination File Is Not Provided, and Overwrite Is Set To [{overwrite}].");
                }
                else if (destination.Exists)
                {
                    throw new Exception($"Destination File [{destinationFile}] Already Exists.");
                }
            }

            io.Stream sourceStream      = source?.Open(AccessType.Read);
            io.Stream destinationStream = destination?.Open(AccessType.Write);
            io.Stream transformStream   = transform?.Open(AccessType.Read);

            if (destinationStream == null)
            {
                String tempFileName = $"{source.FullName}_tmpout";
                destination = Utilities.GetZephyrFile(tempFileName, clients);
                destination.Create(overwrite);
                destinationStream = destination?.Open(AccessType.Write);
                tempFileUsed      = true;
            }

            KeyValue(type, sourceStream, destinationStream, transformStream, settings, createIfNotFound);

            if (tempFileUsed)
            {
                sourceStream.Close();
                destinationStream.Close();
                source.Close();
                destination.Close();

                source.Delete();
                destination.MoveTo(source);
            }
        }
        static public void KeyValue(PropertyFile.Type type, String sourceFile, String destinationFile, SettingFileType transformFile, List <SettingType> settings, bool isDryRun = false)
        {
            PropertyFile props = new PropertyFile(type, sourceFile);

            if (transformFile != null)
            {
                if (!String.IsNullOrWhiteSpace(transformFile.Value))
                {
                    String[] lines = File.ReadAllLines(transformFile.Value);

                    foreach (String line in lines)
                    {
                        char[]   delims = { ',' };
                        String[] values = line.Split(delims);

                        String section = null;
                        String key     = null;
                        String value   = null;

                        if (values.Length == 2)
                        {
                            key   = values[0].Trim();
                            value = values[1].Trim();
                        }
                        else if (values.Length >= 3)
                        {
                            section = values[0].Trim();
                            key     = values[1].Trim();
                            value   = values[2].Trim();
                        }
                        else
                        {
                            continue;
                        }

                        if (!String.IsNullOrWhiteSpace(section))
                        {
                            if (section.StartsWith(@""""))
                            {
                                section = section.Substring(1, section.Length - 2);
                            }
                        }

                        if (!String.IsNullOrWhiteSpace(key))
                        {
                            if (key.StartsWith(@""""))
                            {
                                key = key.Substring(1, key.Length - 2);
                            }
                        }

                        if (!String.IsNullOrWhiteSpace(value))
                        {
                            if (value.Trim().StartsWith(@""""))
                            {
                                value = value.Substring(1, value.Length - 2);
                            }
                        }

                        if (transformFile._HasEncryptedValues && !isDryRun)
                        {
                            Cipher cipher   = new Cipher(config.Default.PassPhrase, config.Default.SaltValue, config.Default.InitVector);
                            String newValue = cipher.Decrypt(value);
                            if (!newValue.StartsWith("UNABLE TO DECRYPT"))
                            {
                                value = newValue;
                            }
                        }

                        if (props.Exists(section, key))
                        {
                            props.SetProperty(section, key, value);
                        }
                        else if (transformFile._CreateIfNotFound)
                        {
                            props.AddProperty(section, key, value);
                        }
                    }
                }
            }

            if (settings != null)
            {
                foreach (SettingType setting in settings)
                {
                    String value = setting.Value.Value;
                    if (setting.Value._IsEncrypted && !isDryRun)
                    {
                        Cipher cipher   = new Cipher(config.Default.PassPhrase, config.Default.SaltValue, config.Default.InitVector);
                        String newValue = cipher.Decrypt(value);
                        if (!newValue.StartsWith("UNABLE TO DECRYPT"))
                        {
                            value = newValue;
                        }
                    }

                    if (props.Exists(setting.Section, setting.Key))
                    {
                        props.SetProperty(setting.Section, setting.Key, value);
                    }
                    else if (setting._CreateIfNotFound)
                    {
                        props.AddProperty(setting.Section, setting.Key, value);
                    }
                }
            }

            if (!isDryRun)
            {
                props.Save(destinationFile);
            }
        }
Exemplo n.º 3
0
        static public void KeyValue(PropertyFile.Type type, io.Stream sourceStream, io.Stream destinationStream, io.Stream transformStream, List <KeyValuePair <String, String> > settings, bool createIfNotFound = false)
        {
            PropertyFile props = new PropertyFile(type, sourceStream);

            if (transformStream != null)
            {
                using (io.StreamReader reader = new io.StreamReader(transformStream))
                {
                    String line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        char[]   delims = { ',' };
                        String[] values = line.Split(delims);

                        String section = null;
                        String key     = null;
                        String value   = null;

                        if (values.Length == 2)
                        {
                            key   = values[0].Trim();
                            value = values[1].Trim();
                        }
                        else if (values.Length >= 3)
                        {
                            section = values[0].Trim();
                            key     = values[1].Trim();
                            value   = values[2].Trim();
                        }
                        else
                        {
                            continue;
                        }

                        if (!String.IsNullOrWhiteSpace(section))
                        {
                            if (section.StartsWith(@""""))
                            {
                                section = section.Substring(1, section.Length - 2);
                            }
                        }

                        if (!String.IsNullOrWhiteSpace(key))
                        {
                            if (key.StartsWith(@""""))
                            {
                                key = key.Substring(1, key.Length - 2);
                            }
                        }

                        if (!String.IsNullOrWhiteSpace(value))
                        {
                            if (value.Trim().StartsWith(@""""))
                            {
                                value = value.Substring(1, value.Length - 2);
                            }
                        }

                        if (props.Exists(section, key))
                        {
                            props.SetProperty(section, key, value);
                        }
                        else if (createIfNotFound)
                        {
                            props.AddProperty(section, key, value);
                        }
                    }
                }
            }

            if (settings != null)
            {
                foreach (KeyValuePair <String, String> setting in settings)
                {
                    String section = String.Empty;
                    String key     = setting.Key;
                    String value   = setting.Value;

                    System.Text.RegularExpressions.Match match = Regex.Match(setting.Key, @"^\[(.*?)\]\s*:\s*(.*?)\s*$", RegexOptions.IgnoreCase);
                    if (match.Success)
                    {
                        section = match.Groups[1].Value;
                        key     = match.Groups[2].Value;
                    }

                    if (props.Exists(section, key))
                    {
                        props.SetProperty(section, key, value);
                    }
                    else if (createIfNotFound)
                    {
                        props.AddProperty(section, key, value);
                    }
                }
            }

            if (destinationStream == null)
            {
                props.Save(sourceStream);
            }
            else
            {
                props.Save(destinationStream);
            }
        }