Exemple #1
0
        // Output methods
        private async Task WritePoints(IEnumerable <LineProtocolPoint> points)
        {
            // TODO: [TESTS] (CsvMetricOutput.WritePoints) Add tests
            var filePath      = GenerateCsvFilePath();
            var fileDirectory = _path.GetDirectoryName(filePath);

            if (!_directory.Exists(fileDirectory))
            {
                _directory.CreateDirectory(fileDirectory);
            }

            var fileExists = _file.Exists(filePath);

            await using var writer = new StreamWriter(filePath, true, Encoding.UTF8);
            await using var csv    = new CsvWriter(writer, CultureInfo.InvariantCulture);
            csv.Configuration.RegisterClassMap <LineProtocolPointMap>();
            csv.Configuration.ShouldQuote = (s, context) => true;

            if (!fileExists)
            {
                csv.WriteHeader <LineProtocolPoint>();
                await csv.NextRecordAsync();
            }

            foreach (var point in points)
            {
                csv.WriteRecord(point);
                await csv.NextRecordAsync();
            }
        }
        public void SaveJsonFile(string path, string json, bool format = false)
        {
            // TODO: [TESTS] (FileSystemHelper.SaveJsonFile) Add tests
            if (format)
            {
                if (!_jsonHelper.TryDeserializeObject(json, out object parsed))
                {
                    throw new Exception("Invalid JSON");
                }

                json = _jsonHelper.SerializeObject(parsed, true);
            }

            if (_file.Exists(path))
            {
                _file.Delete(path);
            }

            var directoryPath = _path.GetDirectoryName(path);

            if (!_directory.Exists(directoryPath))
            {
                _directory.CreateDirectory(directoryPath);
            }

            _file.WriteAllText(path, json);
        }
Exemple #3
0
    private DnsEntriesConfig LoadConfiguration(DnsUpdaterConfig config)
    {
        var configDir = _path.GetDirectoryName(config.ConfigFile);

        // Ensure that the configuration directory exists
        if (!_directory.Exists(configDir))
        {
            _logger.LogInformation("Creating configuration directory: {path}", configDir);
            _directory.CreateDirectory(configDir);
        }

        // Ensure that we have a configuration file to work with
        if (!_file.Exists(config.ConfigFile))
        {
            _logger.LogInformation("Generating sample configuration file: {path}", config.ConfigFile);
            var sampleJson = _jsonHelper.SerializeObject(GenerateSampleConfig(), true);
            _file.WriteAllText(config.ConfigFile, sampleJson);
        }

        // Load, parse and return our configuration
        var rawConfig = _file.ReadAllText(config.ConfigFile);

        return(_jsonHelper.DeserializeObject <DnsEntriesConfig>(rawConfig));
    }