This is a C# code example that interacts with Consul to export, import, and clone configurations.
- .NET Core SDK
- NuGet Package: Consul
- Clone this repository.
- Navigate to the project directory.
- Build the project using the command
dotnet build
. - Run the program using the command
dotnet run
.
The program offers the following options:
Exports configurations from Consul to a JSON file.
Console.WriteLine("Enter the settings prefix to export: ");
var prefixToExport = Console.ReadLine();
Console.WriteLine("Enter the file path to save the settings. ex: c:/temp/my-export.json ");
var exportFilePath = Console.ReadLine();
await ExportConfigurations(consulClient, prefixToExport, exportFilePath);
Imports configurations from a JSON file to Consul.
Console.WriteLine("Enter the file path of the settings to import. ex: c:/temp/my-export.json");
var importFilePath = Console.ReadLine();
Console.WriteLine("Enter the new prefix for the imported settings: ");
var newPrefixForImport = Console.ReadLine();
await ImportConfigurations(consulClient, importFilePath, newPrefixForImport);
Clones the configuration structure from one prefix to another.
Console.WriteLine("Enter the prefix you want to clone: ");
var prefix = Console.ReadLine();
var keysEValues = await FindKeys(consulClient, prefix);
Console.WriteLine("Enter the NEW prefix where you want to create the clone: ");
var newPrefix = Console.ReadLine();
foreach (var par in keysEValues)
{
await CreateKey(consulClient, $"{newPrefix}/{par.Key}", par.Value);
}
- Make sure you have the proper permissions to access Consul.
- Make sure you have a running instance of Consul before using this program.
- Replace the default Consul address and token values as needed in the
CreateConsulClient()
method.
Developed by Meigston.
For testing purposes, there's a docker-compose file that will spin up a Consul instance with predefined configuration. This allows testing the application by performing an export or importing a new configuration.