From c39c2dec3bcba902c4257669fe7e379cb40e1935 Mon Sep 17 00:00:00 2001 From: STAM Date: Thu, 14 Jan 2021 19:33:14 +0300 Subject: [PATCH] 1.0.0.6 --- src/atlassian-downloader/.editorconfig | 4 + src/atlassian-downloader/Program.cs | 92 +++++++++++++------ src/atlassian-downloader/appSettings.json | 24 +++++ .../atlassian-downloader.csproj | 16 +++- .../atlassian-downloader.sln | 7 +- 5 files changed, 114 insertions(+), 29 deletions(-) create mode 100644 src/atlassian-downloader/.editorconfig create mode 100644 src/atlassian-downloader/appSettings.json diff --git a/src/atlassian-downloader/.editorconfig b/src/atlassian-downloader/.editorconfig new file mode 100644 index 0000000..8f0432e --- /dev/null +++ b/src/atlassian-downloader/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CS1591: Missing XML comment for publicly visible type or member +dotnet_diagnostic.CS1591.severity = none diff --git a/src/atlassian-downloader/Program.cs b/src/atlassian-downloader/Program.cs index 1a4b2ea..b482058 100644 --- a/src/atlassian-downloader/Program.cs +++ b/src/atlassian-downloader/Program.cs @@ -1,20 +1,26 @@ -using System; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Serilog; +using System; using System.IO; using System.Linq; using System.Net.Http; using System.Text.Json; +using System.Threading; using System.Threading.Tasks; -/* - -o, --output-folder "path" --> set output folder --v, --version --> show version --h, --help --> show help --l, --list, --links --> show all links to output --f, -feed, --feed-url, -u, --url --> use json link -*/ - namespace EpicMorg.Atlassian.Downloader { - class Program { + class Program : IHostedService { + + private readonly ILogger logger; + private readonly Arguments arguments; + + public Program(ILogger logger,Arguments arguments) { + this.logger = logger; + this.arguments = arguments; + } /// /// /// @@ -22,8 +28,35 @@ namespace EpicMorg.Atlassian.Downloader { /// Show all download links from feed without downloading /// Override URIs to import. /// - static async Task Main(string outputDir = "atlassian", bool list = false, Uri[] customFeed=null) { - + static async Task Main(string outputDir = "atlassian", bool list = false, Uri[] customFeed = null) => await + Host + .CreateDefaultBuilder() + .ConfigureHostConfiguration(configHost => configHost.AddEnvironmentVariables()) + .ConfigureAppConfiguration((ctx, configuration) => { + configuration + .SetBasePath(Environment.CurrentDirectory) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddJsonFile($"appsettings.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables(); + }) + .ConfigureServices((ctx, services) => { + services + .AddOptions() + .AddLogging(builder => { + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(ctx.Configuration) + .CreateLogger(); + builder.AddSerilog(dispose: true); + }); + services.AddHostedService(); + services.AddSingleton(new Arguments(outputDir, list, customFeed)); + }) + .RunConsoleAsync(); + + public record Arguments(string outputDir = "atlassian", bool list = false, Uri[] customFeed = null); + + public async Task StartAsync(CancellationToken cancellationToken) { + var appTitle = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; var appVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; var appStartupDate = DateTime.Now; @@ -31,9 +64,9 @@ namespace EpicMorg.Atlassian.Downloader { #if DEBUG appBuildType = "[Debug]"; #endif - - var feedUrls = customFeed != null - ? customFeed.Select(a=>a.ToString()).ToArray() + + var feedUrls = arguments.customFeed != null + ? arguments.customFeed.Select(a => a.ToString()).ToArray() : new[] { "https://my.atlassian.com/download/feeds/archived/bamboo.json", "https://my.atlassian.com/download/feeds/archived/confluence.json", @@ -63,12 +96,12 @@ namespace EpicMorg.Atlassian.Downloader { }; Console.Title = $"{appTitle} {appVersion} {appBuildType}"; - Console.WriteLine($"Task started at {appStartupDate}."); + logger.LogInformation($"Task started at {appStartupDate}."); var client = new HttpClient(); foreach (var feedUrl in feedUrls) { - var feedDir = Path.Combine(outputDir, feedUrl[(feedUrl.LastIndexOf('/') + 1)..(feedUrl.LastIndexOf('.'))]); + var feedDir = Path.Combine(arguments.outputDir, feedUrl[(feedUrl.LastIndexOf('/') + 1)..(feedUrl.LastIndexOf('.'))]); var atlassianJson = await client.GetStringAsync(feedUrl); var callString = "downloads("; var json = atlassianJson[callString.Length..^1]; @@ -76,14 +109,14 @@ namespace EpicMorg.Atlassian.Downloader { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }); var versionsProg = parsed.GroupBy(a => a.Version).ToDictionary(a => a.Key, a => a.ToArray()); - if (list) { + if (arguments.list) { foreach (var versionProg in versionsProg) { foreach (var file in versionProg.Value) { Console.WriteLine(file.ZipUrl); } } } else { - Console.WriteLine($"[INFO] Download from JSON \"{feedUrl}\" started at {appStartupDate}."); + logger.LogInformation($"Download from JSON \"{feedUrl}\" started at {appStartupDate}."); foreach (var versionProg in versionsProg) { var directory = Path.Combine(feedDir, versionProg.Key); if (!Directory.Exists(directory)) { @@ -100,23 +133,28 @@ namespace EpicMorg.Atlassian.Downloader { using var outputStream = File.OpenWrite(outputFile); using var request = await client.GetStreamAsync(file.ZipUrl).ConfigureAwait(false); await request.CopyToAsync(outputStream).ConfigureAwait(false); - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine($"[INFO] File \"{file.ZipUrl}\" successfully downloaded to \"{outputFile}\"."); - Console.ResetColor(); + //Console.ForegroundColor = ConsoleColor.Green; + logger.LogInformation($"File \"{file.ZipUrl}\" successfully downloaded to \"{outputFile}\"."); + // Console.ResetColor(); } else { - Console.ForegroundColor = ConsoleColor.Yellow; - Console.WriteLine($"[WARN] File \"{outputFile}\" already exists. Download from \"{file.ZipUrl}\" skipped."); - Console.ResetColor(); + // Console.ForegroundColor = ConsoleColor.Yellow; + logger.LogWarning($"File \"{outputFile}\" already exists. Download from \"{file.ZipUrl}\" skipped."); + // Console.ResetColor(); } } } - Console.WriteLine($"[SUCCESS] All files from \"{feedUrl}\" successfully downloaded."); + logger.LogCritical($"All files from \"{feedUrl}\" successfully downloaded."); } } - Console.WriteLine($"Download complete at {appStartupDate}."); + logger.LogCritical($"Download complete at {appStartupDate}."); } + + + public Task StopAsync(CancellationToken cancellationToken) { + throw new NotImplementedException(); + } } public partial class ResponseArray { diff --git a/src/atlassian-downloader/appSettings.json b/src/atlassian-downloader/appSettings.json new file mode 100644 index 0000000..89e38d3 --- /dev/null +++ b/src/atlassian-downloader/appSettings.json @@ -0,0 +1,24 @@ +{ + "Serilog": { + "MinimumLevel": "Verbose", + "WriteTo": [ + "Console", + { + "Name": "Logger", + "Args": { + "configureLogger": { + "WriteTo": [ + { + "Name": "RollingFile", + "Args": { + "pathFormat": "log.{Date}.log", + "retainedFileCountLimit": 5 + } + } + ] + } + } + } + ] + } +} \ No newline at end of file diff --git a/src/atlassian-downloader/atlassian-downloader.csproj b/src/atlassian-downloader/atlassian-downloader.csproj index 069c0d1..9c19004 100644 --- a/src/atlassian-downloader/atlassian-downloader.csproj +++ b/src/atlassian-downloader/atlassian-downloader.csproj @@ -1,4 +1,4 @@ - + Exe @@ -26,6 +26,14 @@ + + + + + + + + True @@ -33,4 +41,10 @@ + + + Always + + + diff --git a/src/atlassian-downloader/atlassian-downloader.sln b/src/atlassian-downloader/atlassian-downloader.sln index 47e2779..b618202 100644 --- a/src/atlassian-downloader/atlassian-downloader.sln +++ b/src/atlassian-downloader/atlassian-downloader.sln @@ -3,7 +3,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30803.129 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AtlassianDownloader", "atlassian-downloader.csproj", "{9C7EA014-5883-4FCD-BF1D-DC561F8958DD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "atlassian-downloader", "atlassian-downloader.csproj", "{9C7EA014-5883-4FCD-BF1D-DC561F8958DD}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{119D41DA-BD17-42D2-8AC7-806C3B68223D}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution