This commit is contained in:
STAM 2021-01-14 19:33:14 +03:00
parent 90568e975e
commit c39c2dec3b
5 changed files with 114 additions and 29 deletions

View File

@ -0,0 +1,4 @@
[*.cs]
# CS1591: Missing XML comment for publicly visible type or member
dotnet_diagnostic.CS1591.severity = none

View File

@ -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<Program> logger;
private readonly Arguments arguments;
public Program(ILogger<Program> logger,Arguments arguments) {
this.logger = logger;
this.arguments = arguments;
}
/// <summary>
///
/// </summary>
@ -22,8 +28,35 @@ namespace EpicMorg.Atlassian.Downloader {
/// <param name="list">Show all download links from feed without downloading</param>
/// <param name="customFeed">Override URIs to import.</param>
/// <returns></returns>
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<Program>();
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 {

View File

@ -0,0 +1,24 @@
{
"Serilog": {
"MinimumLevel": "Verbose",
"WriteTo": [
"Console",
{
"Name": "Logger",
"Args": {
"configureLogger": {
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "log.{Date}.log",
"retainedFileCountLimit": 5
}
}
]
}
}
}
]
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
@ -26,6 +26,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.2-dev-10284" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0-dev-00264" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0-dev-00839" />
<PackageReference Include="Serilog" Version="2.10.1-dev-01265" />
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.3.0-alpha.20574.7" />
<None Include="favicon.png">
<Pack>True</Pack>
@ -33,4 +41,10 @@
</None>
</ItemGroup>
<ItemGroup>
<None Update="appSettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -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