Example #1
0
        private bool UpdateOption(
            bool verbose,
            string dir,
            string option,
            Func <ConfigurationOption, bool> updateOption)
        {
            try
            {
                if (verbose)
                {
                    Logger.EnableVerbose();
                }

                if (!PlugInConfigurationValidation.ValidateElementName(option))
                {
                    Logger.Error(
                        "The option name is invalid '{0}'- " + PlugInConfigurationValidation.ValidCharactersMessage,
                        option);
                    return(false);
                }

                string file = GetFileName(dir);
                using var fileStream = File.Open(file, FileMode.Open, FileAccess.ReadWrite);

                var logger = new BridgeLogger();
                var existingConfiguration = PlugInConfigurationSerializer.ReadFromStream(fileStream, logger);
                if (existingConfiguration == null)
                {
                    return(false);
                }

                var options      = new HashSet <ConfigurationOption>(existingConfiguration.Options);
                var targetOption = options.FirstOrDefault(o => StringComparer.InvariantCulture.Equals(o.Name, option));
                if (targetOption == null)
                {
                    Logger.Error($"Option {option} was not found.");
                    return(false);
                }

                if (!updateOption(targetOption))
                {
                    return(false);
                }

                fileStream.Position = 0;

                var configuration = new PlugInConfiguration(
                    existingConfiguration.PlugInName,
                    existingConfiguration.Version,
                    options);

                PlugInConfigurationSerializer.WriteToStream(configuration, fileStream, logger);
            }
            catch (Exception e)
            {
                _parsingException = e;
            }

            return(true);
        }
Example #2
0
        private void ProcessesCommandAddOption(
            bool verbose,
            string dir,
            string name,
            string description)
        {
            try
            {
                if (verbose)
                {
                    Logger.EnableVerbose();
                }

                if (!PlugInConfigurationValidation.ValidateElementName(name))
                {
                    Logger.Error(
                        "The name is invalid '{0}'- " + PlugInConfigurationValidation.ValidCharactersMessage,
                        name);
                    return;
                }

                string file = GetFileName(dir);
                using var fileStream = File.Open(file, FileMode.Open, FileAccess.ReadWrite);

                var logger = new BridgeLogger();
                var existingConfiguration = PlugInConfigurationSerializer.ReadFromStream(fileStream, logger);
                if (existingConfiguration == null)
                {
                    return;
                }

                ConfigurationOption newOption = new ConfigurationOption(name, description);
                var options = new HashSet <ConfigurationOption>(existingConfiguration.Options);
                if (!options.Add(newOption))
                {
                    Logger.Error("Option {0} already exists.", name);
                    return;
                }

                fileStream.Position = 0;

                var configuration = new PlugInConfiguration(
                    existingConfiguration.PlugInName,
                    existingConfiguration.Version,
                    options);

                PlugInConfigurationSerializer.WriteToStream(configuration, fileStream, logger);

                Logger.Information($"Added option {name}.");
            }
            catch (Exception e)
            {
                _parsingException = e;
            }
        }
Example #3
0
        private void ProcessesCommandDelOption(
            bool verbose,
            string dir,
            string name)
        {
            try
            {
                if (verbose)
                {
                    Logger.EnableVerbose();
                }

                string file = GetFileName(dir);
                using var fileStream = File.Open(file, FileMode.Open, FileAccess.ReadWrite);

                var logger = new BridgeLogger();
                var existingConfiguration = PlugInConfigurationSerializer.ReadFromStream(fileStream, logger);
                if (existingConfiguration == null)
                {
                    return;
                }

                HashSet <ConfigurationOption> options = new HashSet <ConfigurationOption>(existingConfiguration.Options);
                var targetOption = options.FirstOrDefault(o => StringComparer.InvariantCulture.Equals(o.Name, name));
                if (targetOption == null)
                {
                    Logger.Warning("Option {0} was not found.", name);
                    return;
                }

                if (!options.Remove(targetOption))
                {
                    Debug.Assert(false);
                    Logger.Error("Failed to remove option {0}.", name);
                    return;
                }

                fileStream.Position = 0;

                var configuration = new PlugInConfiguration(
                    existingConfiguration.PlugInName,
                    existingConfiguration.Version,
                    options);

                PlugInConfigurationSerializer.WriteToStream(configuration, fileStream, logger);

                Logger.Information($"Deleted option {name}.");
            }
            catch (Exception e)
            {
                _parsingException = e;
            }
        }
Example #4
0
        private void ProcessCommandCreate(
            bool verbose,
            string dir,
            bool overwrite,
            string name,
            SemanticVersion version)
        {
            try
            {
                if (verbose)
                {
                    Logger.EnableVerbose();
                }

                string file = GetFileName(dir, false);
                if (File.Exists(file))
                {
                    if (!overwrite)
                    {
                        Logger.Error($"File already exists: {file}. Use option '-o' to overwrite.");
                        return;
                    }
                }
                else if (string.IsNullOrWhiteSpace(file))
                {
                    Logger.Error($"Configuration file name is invalid: '{file}'.");
                    return;
                }

                var configuration = new PlugInConfiguration(name, version, new HashSet <ConfigurationOption>());

                using var stream = File.Create(file);
                PlugInConfigurationSerializer.WriteToStream(configuration, stream, new BridgeLogger());
                stream.Flush(true);

                Logger.Information($"New configuration logged to {file}.");
            }
            catch (Exception e)
            {
                _parsingException = e;
            }
        }