Пример #1
0
        public Task Set(string file, string key, string value, CancellationToken token)
        {
            file.VerifyNotEmpty(nameof(file));
            key.VerifyNotEmpty(nameof(key));
            value.VerifyNotEmpty(nameof(value));

            using IDisposable scope = _logger.BeginScope(new { Command = nameof(Set), File = file, Key = key, Value = value });

            PropertyFile db = PropertyFile.ReadFromFile(file, true);

            db.Properties[key] = value;
            db.WriteToFile();

            _logger.LogInformation($"Set property {key}={value} from database {db.File}...");

            return(Task.CompletedTask);
        }
Пример #2
0
        public Task Publish(string file, string secretId, CancellationToken token)
        {
            file.VerifyNotEmpty(nameof(file));

            secretId
            .VerifyNotEmpty(nameof(secretId))
            .VerifyAssert(x => x.All(y => char.IsLetterOrDigit(y) || y == '-' || y == '.'), $"Invalid secret ID");

            using IDisposable scope = _logger.BeginScope(new { Command = nameof(Publish), File = file, SecretId = secretId });

            PropertyFile db = PropertyFile.ReadFromFile(file, true);

            new PropertySecret(db.Properties).WriteToSecret(secretId);

            _logger.LogInformation($"Publishing database \"{file}\" to secret Id=\"{secretId}\".");
            return(Task.CompletedTask);
        }
Пример #3
0
        public Task List(string file, CancellationToken token)
        {
            file.VerifyNotEmpty(nameof(file));

            using IDisposable scope = _logger.BeginScope(new { Command = nameof(List), File = file });

            PropertyFile db = PropertyFile.ReadFromFile(file, true);

            string line = db.Properties
                          .Select((x, i) => $"({i}) {x.Key}=\"{x.Value}\"")
                          .Prepend($"Listing properties from database {db.File}...")
                          .Prepend("")
                          .Aggregate(string.Empty, (a, x) => a += x + Environment.NewLine);

            _logger.LogInformation(line);

            return(Task.CompletedTask);
        }
Пример #4
0
        public Task Delete(string file, string key, CancellationToken token)
        {
            file.VerifyNotEmpty(nameof(file));
            key.VerifyNotEmpty(nameof(key));

            using IDisposable scope = _logger.BeginScope(new { Command = nameof(Delete), File = file, Key = key });

            PropertyFile db = PropertyFile.ReadFromFile(file, true);

            _logger.LogInformation($"Delete property {key} from database {file}...");

            bool removed = db.Properties.Remove(key);

            if (removed)
            {
                db.WriteToFile(file);
            }

            _logger.LogInformation($"Property {(removed ? "was removed" : "was not found")}");
            return(Task.CompletedTask);
        }
Пример #5
0
        public Task Get(string file, string key, CancellationToken token)
        {
            file.VerifyNotEmpty(nameof(file));
            key.VerifyNotEmpty(nameof(key));

            using IDisposable scope = _logger.BeginScope(new { Command = nameof(Get), File = file, Key = key });

            PropertyFile db = PropertyFile.ReadFromFile(file, true);

            _logger.LogInformation($"Get property {key} from database {db.File}...");

            if (!db.Properties.TryGetValue(key, out string?value))
            {
                _logger.LogInformation($"Key {key} not found");
            }
            else
            {
                _logger.LogInformation($"Property {key}={value}");
            }

            return(Task.CompletedTask);
        }