Example #1
0
        private async Task <int> ExecuteAsync(Uri address, string token, bool all)
        {
            using var client = ConsulHelper.CreateConsulClient(address, token);
            Console.WriteLine($"Diff started. Address: {client.Config?.Address}");

            return(await CompareFiles(client, includeIdenticalResults : all));
        }
Example #2
0
        private async Task <int> ExecuteAsync(Uri address, string token, string file)
        {
            using var client = ConsulHelper.CreateConsulClient(address, token);
            Console.WriteLine($"Diff started. Address: {client.Config?.Address}");

            try
            {
                var pair = await client.KV.Get(file);

                if (pair?.Response == null)
                {
                    await Console.Error.WriteLineAsync($"Cannot find key on consul: {file}");

                    return(1);
                }

                return(await CompareFileContents(pair.Response));
            }
            catch (Exception ex)
            {
                await Console.Error.WriteLineAsync($"Cannot compare file: {ex.Message}");

                return(1);
            }
        }
Example #3
0
        private async Task <int> ExecuteAsync(Uri address, string token)
        {
            using var client = ConsulHelper.CreateConsulClient(address, token);
            Console.WriteLine($"Push started. Address: {client?.Config?.Address}");

            var usedFiles = Common.GetLocalFiles();

            var txnOps = new List <KVTxnOp>();

            foreach (var filePath in usedFiles)
            {
                var consulPath = Path.GetRelativePath(Common.BaseDirectoryFullPath, filePath).ToUnixPath();

                var textContent = await File.ReadAllTextAsync(filePath, Encoding.UTF8);

                txnOps.Add(new KVTxnOp(consulPath, KVTxnVerb.Set)
                {
                    Value = Encoding.UTF8.GetBytes(textContent)
                });

                Console.WriteLine($"Key: {consulPath}");
            }

            Console.WriteLine($"{txnOps.Count} key(s) prepared. Trying to push...");

            foreach (var txnOpsChunk in txnOps.ChunkBy(Common.ConsulTransactionMaximumOperationCount))
            {
                var pushResponse = await client.KV.Txn(txnOpsChunk);

                if (pushResponse.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    Console.WriteLine($"{pushResponse.Response.Results.Count} key(s) pushed.");
                }
                else
                {
                    Console.WriteLine($"{pushResponse.Response.Results.Count} key(s) pushed. ");
                    Console.WriteLine($"{pushResponse.Response.Errors.Count} key(s) couldn't be pushed due to errors: ");

                    foreach (var errorMessage in pushResponse.Response.Errors.Select(x => x.What).Distinct())
                    {
                        Console.WriteLine($"{errorMessage}");
                    }
                }
            }

            return(0);
        }
Example #4
0
        private async Task <int> ExecuteAsync(Uri address, string token)
        {
            using var client = ConsulHelper.CreateConsulClient(address, token);
            Console.WriteLine($"Fetch started. Address: {client.Config?.Address}");

            var nonFolderPairs = await Common.GetNonFolderPairsAsync(client);

            if (nonFolderPairs == null)
            {
                return(1);
            }

            var fetchResults = new List <FetchResult>();

            foreach (var nonFolderPair in nonFolderPairs)
            {
                var fetchResult = new FetchResult
                {
                    Key = nonFolderPair.Key
                };
                fetchResults.Add(fetchResult);

                try
                {
                    var fileName = Path.Combine(Common.BaseDirectory, nonFolderPair.Key);

                    var content = nonFolderPair.Value == null
                        ? string.Empty
                        : Encoding.UTF8.GetString(nonFolderPair.Value);

                    Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                    await File.WriteAllTextAsync(fileName, content);

                    fetchResult.IsSucceeded = true;
                }
                catch (IOException ex)
                {
                    fetchResult.IsSucceeded  = false;
                    fetchResult.ErrorMessage = $"Key couldn't be fetched. Make sure there is no Key and Folder pair with the same name in the same directory. Error: {ex.Message}";
                }
                catch (Exception ex)
                {
                    fetchResult.IsSucceeded  = false;
                    fetchResult.ErrorMessage = $"Key couldn't be fetched. Error: {ex.Message}";
                }
            }

            if (fetchResults.All(f => f.IsSucceeded))
            {
                Console.WriteLine("All keys successfully fetched.");
                return(0);
            }

            var unSuccessfulResults = fetchResults.Where(fr => !fr.IsSucceeded).ToList();
            await Console.Error.WriteLineAsync($"{unSuccessfulResults.Count} / {fetchResults.Count} key(s) cannot be fetched");

            foreach (var fetchResult in unSuccessfulResults)
            {
                await Console.Error.WriteLineAsync($"{fetchResult.Key}: {fetchResult.ErrorMessage}");
            }

            return(1);
        }