* support of args https://github.com/EpicMorg/atlassian-downloader/issues/5
This commit is contained in:
STAM 2021-01-14 18:46:07 +03:00
parent 5a7836ec54
commit 90568e975e

View File

@ -15,11 +15,14 @@ using System.Threading.Tasks;
namespace EpicMorg.Atlassian.Downloader { namespace EpicMorg.Atlassian.Downloader {
class Program { class Program {
/// <summary>
/// <param name="intOption">An option whose argument is parsed as an int</param> ///
/// <param name="boolOption">An option whose argument is parsed as a bool</param> /// </summary>
/// <param name="fileOption">An option whose argument is parsed as a FileInfo</param> /// <param name="outputDir">Override output directory to download</param>
static async Task Main(string[] args) { /// <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) {
var appTitle = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; var appTitle = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
var appVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; var appVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
@ -29,9 +32,9 @@ namespace EpicMorg.Atlassian.Downloader {
appBuildType = "[Debug]"; appBuildType = "[Debug]";
#endif #endif
var outputDir = "atlassian"; var feedUrls = customFeed != null
var feedUrls = ? customFeed.Select(a=>a.ToString()).ToArray()
new[] { : new[] {
"https://my.atlassian.com/download/feeds/archived/bamboo.json", "https://my.atlassian.com/download/feeds/archived/bamboo.json",
"https://my.atlassian.com/download/feeds/archived/confluence.json", "https://my.atlassian.com/download/feeds/archived/confluence.json",
"https://my.atlassian.com/download/feeds/archived/crowd.json", "https://my.atlassian.com/download/feeds/archived/crowd.json",
@ -60,9 +63,10 @@ namespace EpicMorg.Atlassian.Downloader {
}; };
Console.Title = $"{appTitle} {appVersion} {appBuildType}"; Console.Title = $"{appTitle} {appVersion} {appBuildType}";
Console.WriteLine($"Download started at {appStartupDate}."); Console.WriteLine($"Task started at {appStartupDate}.");
var client = new HttpClient(); var client = new HttpClient();
foreach (var feedUrl in feedUrls) { foreach (var feedUrl in feedUrls) {
var feedDir = Path.Combine(outputDir, feedUrl[(feedUrl.LastIndexOf('/') + 1)..(feedUrl.LastIndexOf('.'))]); var feedDir = Path.Combine(outputDir, feedUrl[(feedUrl.LastIndexOf('/') + 1)..(feedUrl.LastIndexOf('.'))]);
var atlassianJson = await client.GetStringAsync(feedUrl); var atlassianJson = await client.GetStringAsync(feedUrl);
@ -71,14 +75,21 @@ namespace EpicMorg.Atlassian.Downloader {
var parsed = JsonSerializer.Deserialize<ResponseArray[]>(json, new JsonSerializerOptions { var parsed = JsonSerializer.Deserialize<ResponseArray[]>(json, new JsonSerializerOptions {
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
}); });
var versions = parsed.GroupBy(a => a.Version).ToDictionary(a => a.Key, a => a.ToArray()); var versionsProg = parsed.GroupBy(a => a.Version).ToDictionary(a => a.Key, a => a.ToArray());
if (list) {
foreach (var version in versions) { foreach (var versionProg in versionsProg) {
var directory = Path.Combine(feedDir, version.Key); foreach (var file in versionProg.Value) {
Console.WriteLine(file.ZipUrl);
}
}
} else {
Console.WriteLine($"[INFO] Download from JSON \"{feedUrl}\" started at {appStartupDate}.");
foreach (var versionProg in versionsProg) {
var directory = Path.Combine(feedDir, versionProg.Key);
if (!Directory.Exists(directory)) { if (!Directory.Exists(directory)) {
Directory.CreateDirectory(directory); Directory.CreateDirectory(directory);
} }
foreach (var file in version.Value) { foreach (var file in versionProg.Value) {
if (file.ZipUrl == null) { continue; } if (file.ZipUrl == null) { continue; }
var serverPath = file.ZipUrl.PathAndQuery; var serverPath = file.ZipUrl.PathAndQuery;
var outputFile = Path.Combine(directory, serverPath[(serverPath.LastIndexOf("/") + 1)..]); var outputFile = Path.Combine(directory, serverPath[(serverPath.LastIndexOf("/") + 1)..]);
@ -101,7 +112,10 @@ namespace EpicMorg.Atlassian.Downloader {
} }
Console.WriteLine($"[SUCCESS] All files from \"{feedUrl}\" successfully downloaded."); Console.WriteLine($"[SUCCESS] All files from \"{feedUrl}\" successfully downloaded.");
} }
}
Console.WriteLine($"Download complete at {appStartupDate}."); Console.WriteLine($"Download complete at {appStartupDate}.");
} }
} }